class Flickr::Attribute
This class stores the information about attributes. It stores the name, locations and type, and it is responsible for retrieving the attribute values from Flickr’s JSON response, and optionally coercing them to the right type (for example, time can be represented in JSON only as a string, and here we convert it to actual instance of Ruby’s ‘Time` class).
@private
Constants
- COERCIONS
Attributes
locations[R]
name[R]
type[R]
Public Class Methods
new(name, type)
click to toggle source
# File lib/flickr/attributes.rb, line 60 def initialize(name, type) @name, @type = name, type @locations = [] end
Public Instance Methods
add_locations(locations)
click to toggle source
# File lib/flickr/attributes.rb, line 65 def add_locations(locations) @locations = locations + @locations end
value(object)
click to toggle source
# File lib/flickr/attributes.rb, line 69 def value(object) value = find_value(object) coerce(value, object, @type) end
Private Instance Methods
coerce(value, object, type)
click to toggle source
It coerces the found attribute value into a given type. For example, “boolean” type is represented in JSON as an integer (1/0), so values of this type need to be coerced the appropriate true/false values.
# File lib/flickr/attributes.rb, line 97 def coerce(value, object, type) return value if value.nil? if type.is_a?(Enumerable) objects = value.map { |e| coerce(e, object, type.first) } if type.respond_to?(:find_by) return type.class.new({}).populate(objects) else return type.class.new(objects) end elsif type.ancestors.include? Flickr::Object return type.new(value, object.access_token) else COERCIONS.fetch(type).each do |coercion| begin return coercion.call(value) rescue end end end value end
find_value(context)
click to toggle source
Finds attribute value in the JSON response by looking at the given locations.
# File lib/flickr/attributes.rb, line 79 def find_value(context) locations.each do |location| begin value = context.instance_exec(&location) next if value.nil? return value rescue end end nil end