Module: Her::Model::ORM::ClassMethods
- Defined in:
- lib/her/model/orm.rb
Instance Method Summary collapse
-
#build(attributes = {}) ⇒ Object
Build a new resource with the given attributes.
-
#default_scope(block = nil) ⇒ Object
Define the default scope for the model.
-
#destroy_existing(id, params = {}) ⇒ Object
Destroy an existing resource.
-
#method_for(action = nil, method = nil) ⇒ Object
Return or change the HTTP method used to create or update records.
-
#save_existing(id, params) ⇒ Object
Save an existing resource and return it.
-
#scope(name, code) ⇒ Object
Create a new chainable scope.
Instance Method Details
#build(attributes = {}) ⇒ Object
Build a new resource with the given attributes. If the request_new_object_on_build flag is set, the new object is requested via API.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 |
# File 'lib/her/model/orm.rb', line 258 def build(attributes = {}) params = attributes return new(params) unless request_new_object_on_build? path = build_request_path(params.merge(primary_key => 'new')) method = method_for(:new) resource = nil request(params.merge(:_method => method, :_path => path)) do |parsed_data, response| if response.success? resource = new_from_parsed_data(parsed_data) end end resource end |
#default_scope(block = nil) ⇒ Object
Define the default scope for the model
199 200 201 202 203 |
# File 'lib/her/model/orm.rb', line 199 def default_scope(block = nil) @_her_default_scope ||= (!respond_to?(:default_scope) && superclass.respond_to?(:default_scope)) ? superclass.default_scope : scoped @_her_default_scope = @_her_default_scope.instance_exec(&block) unless block.nil? @_her_default_scope end |
#destroy_existing(id, params = {}) ⇒ Object
Destroy an existing resource
231 232 233 234 235 236 237 238 239 240 |
# File 'lib/her/model/orm.rb', line 231 def destroy_existing(id, params = {}) request(params.merge(:_method => method_for(:destroy), :_path => build_request_path(params.merge(primary_key => id)))) do |parsed_data, response| data = parse(parsed_data[:data]) = parsed_data[:metadata] response_errors = parsed_data[:errors] record = new(data.merge(:_destroyed => response.success?, :metadata => )) record.response_errors = response_errors record end end |
#method_for(action = nil, method = nil) ⇒ Object
Return or change the HTTP method used to create or update records
246 247 248 249 250 251 252 253 254 |
# File 'lib/her/model/orm.rb', line 246 def method_for(action = nil, method = nil) @method_for ||= (superclass.respond_to?(:method_for) ? superclass.method_for : {}) return @method_for if action.nil? action = action.to_s.downcase.to_sym return @method_for[action] if method.nil? @method_for[action] = method.to_s.downcase.to_sym end |
#save_existing(id, params) ⇒ Object
Save an existing resource and return it
220 221 222 223 224 |
# File 'lib/her/model/orm.rb', line 220 def save_existing(id, params) resource = new(params.merge(primary_key => id)) resource.save resource end |
#scope(name, code) ⇒ Object
Create a new chainable scope
173 174 175 176 177 178 179 180 181 |
# File 'lib/her/model/orm.rb', line 173 def scope(name, code) # Add the scope method to the class (class << self; self end).send(:define_method, name) do |*args| instance_exec(*args, &code) end # Add the scope method to the default/blank relation scoped.define_singleton_method(name) { |*args| instance_exec(*args, &code) } end |