Module: Her::Model::ORM::ClassMethods
- Defined in:
- lib/castle-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.
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/castle-her/model/orm.rb', line 183 def build(attributes = {}) params = attributes return self.new(params) unless self.request_new_object_on_build? path = self.build_request_path(params.merge(self.primary_key => 'new')) method = self.method_for(:new) resource = nil self.request(params.merge(:_method => method, :_path => path)) do |parsed_data, response| if response.success? resource = self.new_from_parsed_data(parsed_data) end end resource end |
#default_scope(block = nil) ⇒ Object
Define the default scope for the model
130 131 132 133 134 |
# File 'lib/castle-her/model/orm.rb', line 130 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
161 162 163 164 165 |
# File 'lib/castle-her/model/orm.rb', line 161 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| new(parse(parsed_data[:data]).merge(:_destroyed => true)) end end |
#method_for(action = nil, method = nil) ⇒ Object
Return or change the HTTP method used to create or update records
171 172 173 174 175 176 177 178 179 |
# File 'lib/castle-her/model/orm.rb', line 171 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
150 151 152 153 154 |
# File 'lib/castle-her/model/orm.rb', line 150 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
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/castle-her/model/orm.rb', line 102 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 Relation class Relation.instance_eval do define_method(name) { |*args| instance_exec(*args, &code) } end end |