Module: Arkenstone::Document::InstanceMethods
- Defined in:
- lib/arkenstone/document.rb
Instance Attribute Summary collapse
-
#arkenstone_attributes ⇒ Object
The convention is for all Documents to have an id.
-
#arkenstone_server_errors ⇒ Object
The convention is for all Documents to have an id.
-
#id ⇒ Object
The convention is for all Documents to have an id.
Instance Method Summary collapse
-
#attributes ⇒ Object
Easy access to all of the attributes defined for this Document.
-
#attributes=(options) ⇒ Object
Set attributes for a Document.
-
#class_url ⇒ Object
The full RESTful URL for a Document.
-
#destroy ⇒ Object
Sends a DELETE request to the ‘instance_url`.
-
#dup ⇒ Object
Creates a deep dupe of the document with the id set to nil.
-
#has_validation_method? ⇒ Boolean
Checks if there is a ‘valid?` method.
-
#http_response(url, method = :post) ⇒ Object
Sends a network request with the ‘attributes` as the body.
-
#instance_url ⇒ Object
Retrieves a RESTful URL for an instance, in this case by tacking an id onto the end of the ‘arkenstone_url`.
-
#new_record? ⇒ Boolean
Returns true if this is a new object that has not been saved yet.
-
#post_document_data ⇒ Object
Save via POST.
-
#put_document_data ⇒ Object
Save via PUT.
-
#reload ⇒ Object
Reloading the document fetches the document again by it’s id.
-
#save ⇒ Object
(also: #save!)
If this is a new Document, create it with a POST request, otherwise update it with a PUT.
-
#saveable_attributes ⇒ Object
Runs any encoding hooks on the attributes if present.
-
#to_json(options = {}) ⇒ Object
Serializes the attributes to json.
-
#update_attribute(key, value) ⇒ Object
Update a single attribute.
-
#update_attributes(new_attributes) ⇒ Object
Update multiple attributes at once.
Instance Attribute Details
#arkenstone_attributes ⇒ Object
The convention is for all Documents to have an id.
36 37 38 |
# File 'lib/arkenstone/document.rb', line 36 def arkenstone_attributes @arkenstone_attributes end |
#arkenstone_server_errors ⇒ Object
The convention is for all Documents to have an id.
36 37 38 |
# File 'lib/arkenstone/document.rb', line 36 def arkenstone_server_errors @arkenstone_server_errors end |
#id ⇒ Object
The convention is for all Documents to have an id.
36 37 38 |
# File 'lib/arkenstone/document.rb', line 36 def id @id end |
Instance Method Details
#attributes ⇒ Object
Easy access to all of the attributes defined for this Document.
39 40 41 42 43 44 45 |
# File 'lib/arkenstone/document.rb', line 39 def attributes new_hash = {} self.class.arkenstone_attributes.each do |key| new_hash[key.to_sym] = send(key.to_s) end new_hash end |
#attributes=(options) ⇒ Object
Set attributes for a Document. If a key in the ‘options` hash is not present in the attributes list, it is ignored.
48 49 50 51 52 53 54 |
# File 'lib/arkenstone/document.rb', line 48 def attributes=() .each do |key, value| setter = "#{key}=" send(setter.to_sym, value) if respond_to? setter end attributes end |
#class_url ⇒ Object
The full RESTful URL for a Document.
114 115 116 |
# File 'lib/arkenstone/document.rb', line 114 def class_url full_url(self.class.arkenstone_url) end |
#destroy ⇒ Object
Sends a DELETE request to the ‘instance_url`.
129 130 131 132 |
# File 'lib/arkenstone/document.rb', line 129 def destroy resp = http_response instance_url, :delete Arkenstone::Network.response_is_success resp end |
#dup ⇒ Object
Creates a deep dupe of the document with the id set to nil
154 155 156 157 158 |
# File 'lib/arkenstone/document.rb', line 154 def dup duped = super duped.id = nil duped end |
#has_validation_method? ⇒ Boolean
Checks if there is a ‘valid?` method.
97 98 99 |
# File 'lib/arkenstone/document.rb', line 97 def has_validation_method? self.class.method_defined? :valid? end |
#http_response(url, method = :post) ⇒ Object
Sends a network request with the ‘attributes` as the body.
135 136 137 138 139 |
# File 'lib/arkenstone/document.rb', line 135 def http_response(url, method = :post) response = self.class.send_request url, method, saveable_attributes self.arkenstone_server_errors = JSON.parse(response.body) if response.code == '500' response end |
#instance_url ⇒ Object
Retrieves a RESTful URL for an instance, in this case by tacking an id onto the end of the ‘arkenstone_url`. Example:
# arkenstone_url
http://example.com/users
# instance_url
http://example.com/users/100
109 110 111 |
# File 'lib/arkenstone/document.rb', line 109 def instance_url "#{full_url(self.class.arkenstone_url)}#{id}" end |
#new_record? ⇒ Boolean
Returns true if this is a new object that has not been saved yet.
57 58 59 |
# File 'lib/arkenstone/document.rb', line 57 def new_record? id.nil? end |
#post_document_data ⇒ Object
Save via POST.
119 120 121 |
# File 'lib/arkenstone/document.rb', line 119 def post_document_data http_response class_url, :post end |
#put_document_data ⇒ Object
Save via PUT.
124 125 126 |
# File 'lib/arkenstone/document.rb', line 124 def put_document_data http_response instance_url, :put end |
#reload ⇒ Object
Reloading the document fetches the document again by it’s id
76 77 78 79 80 |
# File 'lib/arkenstone/document.rb', line 76 def reload reloaded_self = self.class.find(id) self.attributes = reloaded_self.attributes self end |
#save ⇒ Object Also known as: save!
If this is a new Document, create it with a POST request, otherwise update it with a PUT. Returns whether the server response was successful or not.
67 68 69 70 71 72 73 |
# File 'lib/arkenstone/document.rb', line 67 def save self.class.check_for_url if respond_to?(:timestampable) response = new_record? ? post_document_data : put_document_data self.attributes = JSON.parse(response.body) Arkenstone::Network.response_is_success response end |
#saveable_attributes ⇒ Object
Runs any encoding hooks on the attributes if present.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/arkenstone/document.rb', line 142 def saveable_attributes return attributes unless Arkenstone::Hook.has_hooks? self.class attrs = {} Arkenstone::Hook.all_hooks_for_class(self.class).each do |hook| new_attrs = hook.encode_attributes(attributes) attrs.merge! new_attrs unless new_attrs.nil? end attrs.empty? ? attributes : attrs end |
#to_json(options = {}) ⇒ Object
Serializes the attributes to json.
62 63 64 |
# File 'lib/arkenstone/document.rb', line 62 def to_json( = {}) attributes.to_json() end |
#update_attribute(key, value) ⇒ Object
Update a single attribute. Performs validation (by calling ‘update_attributes`).
85 86 87 88 |
# File 'lib/arkenstone/document.rb', line 85 def update_attribute(key, value) hash = { key.to_sym => value } update_attributes hash end |
#update_attributes(new_attributes) ⇒ Object
Update multiple attributes at once. Performs validation (if that is setup for this document).
91 92 93 94 |
# File 'lib/arkenstone/document.rb', line 91 def update_attributes(new_attributes) attributes.merge! new_attributes save end |