Class: Agree2::Base
- Inherits:
-
Object
- Object
- Agree2::Base
- Defined in:
- lib/agree2/base.rb
Overview
The superclass of all Agree2 Resource objects.
Instance Attribute Summary collapse
-
#container ⇒ Object
Returns the value of attribute container.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
-
.attr_serializable(*attributes) ⇒ Object
:nodoc:.
-
.collection_name ⇒ Object
:nodoc:.
-
.collection_path ⇒ Object
:nodoc:.
-
.get(container, id) ⇒ Object
Gets an instance of a resource.
-
.instance_path(id) ⇒ Object
:nodoc:.
-
.serializable_attributes ⇒ Object
Returns an array of all the attributes that have been made accessible to mass-assignment.
-
.singular_name ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#attributes ⇒ Object
Get the primary attributes of an object as a hash.
-
#destroy ⇒ Object
Destroys the object from Agree2’s servers.
-
#initialize(container, fields = {}) ⇒ Base
constructor
:nodoc:.
-
#new_record? ⇒ Boolean
Has this record been saved to the server yet?.
-
#path ⇒ Object
Returns the relative path to the object.
-
#reload ⇒ Object
Reloads the object from Agree2’s servers.
-
#save ⇒ Object
Saves the record to the server.
-
#to_param ⇒ Object
The primary key of the object.
-
#to_url ⇒ Object
Returns the full URL for the object.
Constructor Details
#initialize(container, fields = {}) ⇒ Base
:nodoc:
47 48 49 50 51 52 53 54 55 |
# File 'lib/agree2/base.rb', line 47 def initialize(container,fields={})#:nodoc: @container=container @user=(container.is_a?(User) ? container : container.user) if fields.is_a?(Hash) load_attributes(fields) else load_json(fields) end end |
Instance Attribute Details
#container ⇒ Object
Returns the value of attribute container.
45 46 47 |
# File 'lib/agree2/base.rb', line 45 def container @container end |
#user ⇒ Object
Returns the value of attribute user.
45 46 47 |
# File 'lib/agree2/base.rb', line 45 def user @user end |
Class Method Details
.attr_serializable(*attributes) ⇒ Object
:nodoc:
8 9 10 11 12 13 14 |
# File 'lib/agree2/base.rb', line 8 def attr_serializable(*attributes) #:nodoc: attributes.map!{|a|a.to_sym} write_inheritable_attribute("serializable_attributes", Set.new(attributes) + (serializable_attributes || [])) attr_accessor *attributes end |
.collection_name ⇒ Object
:nodoc:
29 30 31 |
# File 'lib/agree2/base.rb', line 29 def collection_name #:nodoc: self.to_s.demodulize.tableize end |
.collection_path ⇒ Object
:nodoc:
21 22 23 |
# File 'lib/agree2/base.rb', line 21 def collection_path #:nodoc: "/#{collection_name}" end |
.get(container, id) ⇒ Object
Gets an instance of a resource
38 39 40 41 |
# File 'lib/agree2/base.rb', line 38 def get(container,id) user=(container.is_a?(User) ? container : container.user) new( container, user.get(container.path+instance_path(id))) end |
.instance_path(id) ⇒ Object
:nodoc:
25 26 27 |
# File 'lib/agree2/base.rb', line 25 def instance_path(id) #:nodoc: "#{collection_path}/#{id}" end |
.serializable_attributes ⇒ Object
Returns an array of all the attributes that have been made accessible to mass-assignment.
17 18 19 |
# File 'lib/agree2/base.rb', line 17 def serializable_attributes # :nodoc: read_inheritable_attribute("serializable_attributes") end |
.singular_name ⇒ Object
:nodoc:
33 34 35 |
# File 'lib/agree2/base.rb', line 33 def singular_name #:nodoc: self.to_s.demodulize.underscore.singularize end |
Instance Method Details
#attributes ⇒ Object
Get the primary attributes of an object as a hash
97 98 99 100 101 102 103 |
# File 'lib/agree2/base.rb', line 97 def attributes self.class.serializable_attributes.inject({}) do |h,field| value=self.send(field) h[field]=value if value h end end |
#destroy ⇒ Object
Destroys the object from Agree2’s servers
68 69 70 |
# File 'lib/agree2/base.rb', line 68 def destroy user.delete(path) end |
#new_record? ⇒ Boolean
Has this record been saved to the server yet?
58 59 60 |
# File 'lib/agree2/base.rb', line 58 def new_record? @from_wire!=true end |
#path ⇒ Object
Returns the relative path to the object
78 79 80 |
# File 'lib/agree2/base.rb', line 78 def path #:nodoc: self.container.path+self.class.instance_path(to_param) end |
#reload ⇒ Object
Reloads the object from Agree2’s servers
63 64 65 |
# File 'lib/agree2/base.rb', line 63 def reload load_json(user.get(path)) end |
#save ⇒ Object
Saves the record to the server
88 89 90 91 92 93 94 |
# File 'lib/agree2/base.rb', line 88 def save if new_record? load_json(@user.post("#{self.container.path}/#{self.class.collection_name}",attributes_for_save)) else load_json(@user.put("#{path}",attributes_for_save)) end end |
#to_param ⇒ Object
The primary key of the object
83 84 85 |
# File 'lib/agree2/base.rb', line 83 def to_param #:nodoc: id end |
#to_url ⇒ Object
Returns the full URL for the object
73 74 75 |
# File 'lib/agree2/base.rb', line 73 def to_url "#{AGREE2_URL}#{path}" end |