class Flickr::Object
Every Flickr
object inherits from this class. It provides interface for defining attributes and some helper methods.
Attributes
access_token[R]
@private
Public Class Methods
attribute(name, type)
click to toggle source
Overriding Flickr::Attributes#attribute
to add a default location. This means that ‘:<attribute>` will always first be searched in `@“”>attributes`.
@!macro [attach] attribute
@attribute [r] $1 @return [$2]
@private
Calls superclass method
# File lib/flickr/object.rb, line 29 def self.attribute(name, type) new_attribute = super new_attribute.add_locations([-> { @attributes[name.to_s] }]) end
new(attributes, access_token = [])
click to toggle source
@private
# File lib/flickr/object.rb, line 37 def initialize(attributes, access_token = []) raise ArgumentError, "attributes should not be nil" if attributes.nil? @attributes = attributes @access_token = access_token end
Public Instance Methods
==(other)
click to toggle source
Compares by ID (if that object has an ID), otherwise compares by attributes.
# File lib/flickr/object.rb, line 69 def ==(other) if not other.is_a?(Flickr::Object) raise ArgumentError "can't compare Flickr::Object with #{other.class}" end if [self, other].all? { |object| object.respond_to?(:id) && object.id } self.id == other.id else self.attributes == other.attributes end end
Also aliased as: eql?
[](key)
click to toggle source
Shorthand for accessing the raw hash of attributes returned from Flickr
.
@see attributes
# File lib/flickr/object.rb, line 62 def [](key) @attributes[key] end
inspect()
click to toggle source
Displays all the attributes and their values.
# File lib/flickr/object.rb, line 85 def inspect attribute_values = self.class.attributes .inject({}) { |hash, attribute| hash.update(attribute.name => send(attribute.name)) } .reject { |name, value| value.nil? or (value.respond_to?(:empty?) and value.empty?) } attributes = attribute_values .map { |name, value| "#{name}=#{value.inspect}" } .join(" ") class_name = self.class.name hex_code = "0x#{(object_id >> 1).to_s(16)}" "#<#{class_name}:#{hex_code} #{attributes}>" end
matches?(attributes)
click to toggle source
Tests if the object matches a hash of attributes. Supports nesting (see the example).
@param attributes [Hash] @return [Boolean] @example
photo.matches?(owner: {username: "janko"})
# File lib/flickr/object.rb, line 108 def matches?(attributes) attributes.all? do |name, value| if send(name).is_a?(Flickr::Object) and value.is_a?(Hash) send(name).matches?(value) else send(name) == value end end end
update(attributes)
click to toggle source
@private
# File lib/flickr/object.rb, line 121 def update(attributes) @attributes.update(attributes) self end
Private Instance Methods
api()
click to toggle source
# File lib/flickr/object.rb, line 128 def api api_name = self.class.name.match(/^Flickr::Object::/).post_match api_class = Flickr::Api.const_get(api_name) api_class.new(@access_token) end