Class: Turntabler::Resource

Inherits:
Object
  • Object
show all
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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Assertions

assert_valid_keys, assert_valid_values

Methods included from DigestHelpers

#digest

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

#idString, 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.

Examples:

# Define a "name" attribute that maps to a Turntable "name" attribute
attribute :name

# Define an "id" attribute that maps to a Turntable "_id" attribute
attribute :id, :_id

# Define an "user_id" attribute that maps to both a Turntable "user_id" and "userid" attribute
attribute :user_id, :user_id, :userid

# Define a "time" attribute that maps to a Turntable "time" attribute
# and converts the value to a Time object
attribute :time do |value|
  Time.at(value)
end

# Define a "created_at" attribute that maps to a Turntable "time" attribute
# and converts the value to a Time object
attribute :created_at, :time do |value|
  Time.at(value)
end

# Define a "friends" attribute that does *not* get loaded from Turntable
# when accessed
attribute :friends, :load => false

Raises:

  • (ArgumentError)

    if an invalid option is specified



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)
  options = turntable_names.last.is_a?(Hash) ? turntable_names.pop : {}
  assert_valid_keys(options, :load)
  options = {:load => true}.merge(options)

  # Reader
  define_method(name) do
    load if instance_variable_get("@#{name}").nil? && !loaded? && options[: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

#hashFixnum

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

#loadtrue 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_variablesArray<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