Module: Arkenstone::Document::ClassMethods
- Defined in:
- lib/arkenstone/document.rb
Instance Attribute Summary collapse
-
#arkenstone_attributes ⇒ Object
Returns the value of attribute arkenstone_attributes.
-
#arkenstone_hooks ⇒ Object
Returns the value of attribute arkenstone_hooks.
-
#arkenstone_inherit_hooks ⇒ Object
Returns the value of attribute arkenstone_inherit_hooks.
-
#arkenstone_url ⇒ Object
Returns the value of attribute arkenstone_url.
Instance Method Summary collapse
-
#add_hook(hook) ⇒ Object
Hooks.
-
#all ⇒ Object
Calls the ‘arkenstone_url` expecting to receive a json array of properties to deserialize into a list of objects.
-
#attributes(*options) ⇒ Object
Sets the attributes for an Arkenstone Document.
-
#build(options) ⇒ Object
Constructs a new instance with the provided attributes.
-
#check_for_url ⇒ Object
You can use Arkenstone without defining a ‘url`, but you won’t be able to save a model without one.
-
#create(options) ⇒ Object
Creates and saves a single instance with the attribute values provided.
- #ensure_parseable_is_array(to_parse) ⇒ Object
-
#find(id) ⇒ Object
Performs a GET request to the instance url with the supplied id.
-
#inherit_hooks(val: true) ⇒ Object
Hooks are applied only to the class they are added to.
-
#parse_all(to_parse) ⇒ Object
Builds a list of objects with attributes set from a JSON string or an array.
-
#url(new_url) ⇒ Object
Sets the root url used for generating RESTful requests.
Instance Attribute Details
#arkenstone_attributes ⇒ Object
Returns the value of attribute arkenstone_attributes.
162 163 164 |
# File 'lib/arkenstone/document.rb', line 162 def arkenstone_attributes @arkenstone_attributes end |
#arkenstone_hooks ⇒ Object
Returns the value of attribute arkenstone_hooks.
162 163 164 |
# File 'lib/arkenstone/document.rb', line 162 def arkenstone_hooks @arkenstone_hooks end |
#arkenstone_inherit_hooks ⇒ Object
Returns the value of attribute arkenstone_inherit_hooks.
162 163 164 |
# File 'lib/arkenstone/document.rb', line 162 def arkenstone_inherit_hooks @arkenstone_inherit_hooks end |
#arkenstone_url ⇒ Object
Returns the value of attribute arkenstone_url.
162 163 164 |
# File 'lib/arkenstone/document.rb', line 162 def arkenstone_url @arkenstone_url end |
Instance Method Details
#add_hook(hook) ⇒ Object
Hooks
Hooks are used to allow you to call arbitrary code at various points in the object lifecycle. For example, if you need to massage some property names before they are sent off to the ‘url`, you can do that with a hook. A hook should extend `Arkenstone::Hook` and then override the method you want to hook into. There are three types of hooks:
-
‘before_request` - Called before the request is sent to the web service. Passes in the request environment (an `Arkenstone::Environment`) as a parameter.
-
‘after_complete` - Called after the request has been successfully completed. Passes in a Net::HTTPResponse as a parameter.
-
‘on_error` - Called if the response returned an error. Passes in a Net::HTTPResponse as a parameter.
Example:
class ErrorLogger < Arkenstone::Hook
def on_error(response)
# log the error here
end
end
class User
include Arkenstone::Document
url 'http://example.com/users'
add_hook ErrorLogger.new
end
190 191 192 193 |
# File 'lib/arkenstone/document.rb', line 190 def add_hook(hook) self.arkenstone_hooks = [] if arkenstone_hooks.nil? arkenstone_hooks << hook end |
#all ⇒ Object
Calls the ‘arkenstone_url` expecting to receive a json array of properties to deserialize into a list of objects.
282 283 284 285 286 |
# File 'lib/arkenstone/document.rb', line 282 def all check_for_url response = send_request arkenstone_url, :get parse_all response.body end |
#attributes(*options) ⇒ Object
Sets the attributes for an Arkenstone Document. These become ‘attr_accessors` on instances.
223 224 225 226 227 228 229 |
# File 'lib/arkenstone/document.rb', line 223 def attributes(*) self.arkenstone_attributes = .each do |option| send(:attr_accessor, option) end arkenstone_attributes end |
#build(options) ⇒ Object
Constructs a new instance with the provided attributes.
237 238 239 240 241 242 243 |
# File 'lib/arkenstone/document.rb', line 237 def build() document = new document.attributes = Hash().select do |key, _value| document.respond_to? :"#{key}=" end document end |
#check_for_url ⇒ Object
You can use Arkenstone without defining a ‘url`, but you won’t be able to save a model without one. This raises an error if the url is not defined.
232 233 234 |
# File 'lib/arkenstone/document.rb', line 232 def check_for_url raise NoUrlError.new, NoUrlError. if arkenstone_url.nil? end |
#create(options) ⇒ Object
Creates and saves a single instance with the attribute values provided.
265 266 267 268 269 |
# File 'lib/arkenstone/document.rb', line 265 def create() document = build() document.save document end |
#ensure_parseable_is_array(to_parse) ⇒ Object
259 260 261 262 |
# File 'lib/arkenstone/document.rb', line 259 def ensure_parseable_is_array(to_parse) to_parse = [to_parse] if to_parse.is_a? Hash to_parse end |
#find(id) ⇒ Object
Performs a GET request to the instance url with the supplied id. Builds an instance with the response.
272 273 274 275 276 277 278 279 |
# File 'lib/arkenstone/document.rb', line 272 def find(id) check_for_url url = full_url(arkenstone_url) + id.to_s response = send_request url, :get return nil unless Arkenstone::Network.response_is_success response build JSON.parse(response.body) end |
#inherit_hooks(val: true) ⇒ Object
Hooks are applied only to the class they are added to. This can cause a problem if you have a base class and want to use the same hooks for subclasses. If you want to use the same hooks as a parent class, use ‘inherit_hooks`. This will tell Arkenstone to walk up the inheritance chain and call all of the hooks it can find. Example:
class ErrorLogger < Arkenstone::Hook
def on_error(response)
# log the error here
end
end
class BaseModel
include Arkenstone::Document
add_hook ErrorLogger.new
add_hook SomeOtherHook.new
end
class User < BaseModel
url 'http://example.com/users'
inherit_hooks
end
This will use the hooks defined for ‘BaseModel` and any defined for `User` too.
218 219 220 |
# File 'lib/arkenstone/document.rb', line 218 def inherit_hooks(val: true) self.arkenstone_inherit_hooks = val end |
#parse_all(to_parse) ⇒ Object
Builds a list of objects with attributes set from a JSON string or an array.
246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/arkenstone/document.rb', line 246 def parse_all(to_parse) return [] if to_parse.nil? || to_parse.empty? tree = if to_parse.is_a? String JSON.parse to_parse else to_parse end tree = ensure_parseable_is_array tree documents = tree.map { |document| build document } Arkenstone::QueryList.new documents end |
#url(new_url) ⇒ Object
Sets the root url used for generating RESTful requests.
165 166 167 |
# File 'lib/arkenstone/document.rb', line 165 def url(new_url) self.arkenstone_url = new_url end |