Class: FreeAgent::Entity

Inherits:
Object
  • Object
show all
Defined in:
lib/free_agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, attributes = {}) ⇒ Entity

Returns a new instance of Entity.



151
152
153
154
# File 'lib/free_agent.rb', line 151

def initialize(resource, attributes = {})
  @resource = resource
  @attributes = attributes.to_mash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object (private)



190
191
192
# File 'lib/free_agent.rb', line 190

def method_missing(*args)
  @attributes.send(*args)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



149
150
151
# File 'lib/free_agent.rb', line 149

def attributes
  @attributes
end

Class Method Details

.belongs_to(thing, *args) ⇒ Object



138
139
140
141
142
143
# File 'lib/free_agent.rb', line 138

def self.belongs_to(thing, *args)
  define_method(thing) do

  end
  # NOOP right now.
end

.has_many(things) ⇒ Object



132
133
134
135
136
# File 'lib/free_agent.rb', line 132

def self.has_many(things)
  define_method(things) do
    Collection.new(@resource["/#{things}"], :entity => things.to_s.singularize)
  end
end

.xml_nameObject



145
146
147
# File 'lib/free_agent.rb', line 145

def self.xml_name
  name.split("::")[1..-1].join("::").underscore
end

Instance Method Details

#destroyObject



180
181
182
# File 'lib/free_agent.rb', line 180

def destroy
  @resource.delete
end

#idObject



156
157
158
# File 'lib/free_agent.rb', line 156

def id
  @attributes.id
end

#reloadObject



164
165
166
167
168
# File 'lib/free_agent.rb', line 164

def reload
  returning(self) do
    @attributes = Crack::XML.parse(@resource.get)[xml_name].to_mash
  end
end

#saveObject



175
176
177
178
# File 'lib/free_agent.rb', line 175

def save
  @resource.put(attributes.to_xml(:root => xml_name),
    :content_type =>'application/xml', :accept => 'application/xml')
end

#update(new_attributes) ⇒ Object



170
171
172
173
# File 'lib/free_agent.rb', line 170

def update(new_attributes)
  attributes.merge!(new_attributes)
  save
end

#urlObject



160
161
162
# File 'lib/free_agent.rb', line 160

def url
  @resource.url
end