Class: Turntabler::Resource
- Inherits:
-
Object
- Object
- Turntabler::Resource
- Extended by:
- Assertions
- Includes:
- Assertions, DigestHelpers
- Defined in:
- lib/turntabler/resource.rb
Overview
Represents an object that’s been created using content from Turntable. This encapsulates responsibilities such as reading and writing attributes.
By default all Turntable resources have a :id attribute defined.
Direct Known Subclasses
Avatar, Boot, Message, Playlist, Preferences, Room, Snag, Song, Sticker, StickerPlacement, User, Vote
Instance Attribute Summary collapse
-
#id ⇒ String, Fixnum
readonly
The unique identifier for this resource.
Class Method Summary collapse
-
.attribute(name, *turntable_names, &block) ⇒ Object
private
Defines a new Turntable attribute on this class.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Determines whether this resource is equal to another based on their unique identifiers.
-
#attributes=(attributes) ⇒ Object
private
Attempts to set attributes on the object only if they’ve been explicitly defined by the class.
-
#hash ⇒ Fixnum
Generates a hash for this resource based on the unique identifier.
-
#initialize(client, attributes = {}, *args) ⇒ Resource
constructor
private
Initializes this resources with the given attributes.
-
#load ⇒ true
(also: #reload)
Loads the attributes for this resource from Turntable.
-
#loaded? ⇒ Boolean
Determines whether the current resource has been loaded from Turntable.
-
#pretty_print(q) ⇒ String
private
Forces this object to use PP’s implementation of inspection.
-
#pretty_print_instance_variables ⇒ Array<Symbol>
private
Defines the instance variables that should be printed when inspecting this object.
Methods included from Assertions
assert_valid_keys, assert_valid_values
Methods included from DigestHelpers
Constructor Details
#initialize(client, attributes = {}, *args) ⇒ Resource
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initializes this resources with the given attributes. This will continue to call the superclass’s constructor with any additional arguments that get specified.
97 98 99 100 101 102 |
# File 'lib/turntabler/resource.rb', line 97 def initialize(client, attributes = {}, *args) @loaded = false @client = client self.attributes = attributes super(*args) end |
Instance Attribute Details
#id ⇒ String, Fixnum (readonly)
The unique identifier for this resource
90 |
# File 'lib/turntabler/resource.rb', line 90 attribute :id, :_id, :load => false |
Class Method Details
.attribute(name, *turntable_names, &block) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines a new Turntable attribute on this class. By default, the name of the attribute is assumed to be the same name that Turntable specifies in its API. If the names are different, this can be overridden on a per-attribute basis.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/turntabler/resource.rb', line 56 def attribute(name, *turntable_names, &block) = turntable_names.last.is_a?(Hash) ? turntable_names.pop : {} assert_valid_keys(, :load) = {:load => true}.merge() # Reader define_method(name) do load if instance_variable_get("@#{name}").nil? && !loaded? && [:load] instance_variable_get("@#{name}") end # Query define_method("#{name}?") do !!__send__(name) end # Typecasting block ||= lambda {|value| value} define_method("typecast_#{name}", &block) protected :"typecast_#{name}" # Attribute name conversion turntable_names = [name] if turntable_names.empty? turntable_names.each do |turntable_name| define_method("#{turntable_name}=") do |value| instance_variable_set("@#{name}", value.nil? ? nil : __send__("typecast_#{name}", value)) end protected :"#{turntable_name}=" end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Determines whether this resource is equal to another based on their unique identifiers.
162 163 164 165 166 167 168 |
# File 'lib/turntabler/resource.rb', line 162 def ==(other) if other && other.respond_to?(:id) && other.id other.id == id else false end end |
#attributes=(attributes) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Attempts to set attributes on the object only if they’ve been explicitly defined by the class. Note that this will also attempt to interpret any “metadata” properties as additional attributes.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/turntabler/resource.rb', line 126 def attributes=(attributes) if attributes attributes.each do |attribute, value| attribute = attribute.to_s if attribute == 'metadata' self.attributes = value else __send__("#{attribute}=", value) if respond_to?("#{attribute}=") end end end end |
#hash ⇒ Fixnum
Generates a hash for this resource based on the unique identifier
174 175 176 |
# File 'lib/turntabler/resource.rb', line 174 def hash id.hash end |
#load ⇒ true Also known as: reload
Loads the attributes for this resource from Turntable. By default this is a no-op and just marks the resource as loaded.
108 109 110 |
# File 'lib/turntabler/resource.rb', line 108 def load @loaded = true end |
#loaded? ⇒ Boolean
Determines whether the current resource has been loaded from Turntable.
116 117 118 |
# File 'lib/turntabler/resource.rb', line 116 def loaded? @loaded end |
#pretty_print(q) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Forces this object to use PP’s implementation of inspection.
143 144 145 |
# File 'lib/turntabler/resource.rb', line 143 def pretty_print(q) q.pp_object(self) end |
#pretty_print_instance_variables ⇒ Array<Symbol>
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Defines the instance variables that should be printed when inspecting this object. This ignores the @client and @loaded variables.
153 154 155 |
# File 'lib/turntabler/resource.rb', line 153 def pretty_print_instance_variables (instance_variables - [:'@client', :'@loaded']).sort end |