开发者

Rename ActiveResource properties

开发者 https://www.devze.com 2023-02-25 14:10 出处:网络
I am consuming JSON data from a third party API, doing a little bit of processing on that data and then sending the models to the client as JSON. The keys for the incoming data are not named very well

I am consuming JSON data from a third party API, doing a little bit of processing on that data and then sending the models to the client as JSON. The keys for the incoming data are not named very well. Some of them are acronyms, some just seem to be random characters. For example:

{
  aikd: "some value"
  lrdf: 1 // I guess this is the ID
}

I am creating a rails ActiveResource model to wrap this resource, but would not like to access 开发者_运维问答these properties through model.lrdf as its not obvious what lrdf really is! Instead, I would like some way to alias these properties to another property that is named better. Something so that I can say model.id = 1 and have that automatically set lrdf to 1 or puts model.id and have that automatically return 1. Also, when I call model.to_json to send the model to the client, I dont want my javascript to have to understand these odd naming conventions.

I tried

alias id lrdf

but that gave me an error saying method lrdf did not exist.

The other option is to just wrap the properties:

def id
  lrdf
end

This works, but when I call model.to_json, I see lrdf as the keys again.

Has anyone done anything like this before? What do you recommend?


Have you tried with some before_save magic? Maybe you could define attr_accessible :ldrf, and then, in your before_save filter, assign ldrf to your id field. Haven't tried it, but I think it should works.

attr_accessible :ldrf

before_save :map_attributes

protected
  def map_attributes
    {:ldrf=>:id}.each do |key, value|
      self.send("#{value}=", self.send(key))
    end
  end

Let me know!


You could try creating a formatter module based on ActiveResource::Formats::JsonFormat and override decode(). If you had to update the data, you'd have to override encode() also. Look at your local gems/activeresource-N.N.N/lib/active_resource/formats/json_format.rb to see what the original json formatter does.

If your model's name is Model and your formatter is CleanupFormatter, just do Model.format = CleanupFormatter.

module CleanupFormatter
  include ::ActiveResource::Formats::JsonFormat
  extend self
  # Set a constant for the mapping.
  # I'm pretty sure these should be strings. If not, try symbols.
  MAP = [['lrdf', 'id']]

  def decode(json)
    orig_hash = super
    new_hash = {}
    MAP.each {|old_name, new_name| new_hash[new_name] = orig_hash.delete(old_name) }
    # Comment the next line if you don't want to carry over fields missing from MAP
    new_hash.merge!(orig_hash)
    new_hash
  end
end

This doesn't involve aliasing as you asked, but I think it helps to isolate the gibberish names from your model, which would never have to know those original names existed. And "to_json" will display the readable names.

0

精彩评论

暂无评论...
验证码 换一张
取 消