Module: Her::Model::ORM::ClassMethods

Defined in:
lib/her/model/orm.rb

Instance Method Summary collapse

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.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/her/model/orm.rb', line 275

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

Examples:

class User
  include Her::Model

  default_scope lambda { where(:admin => 1) }
enc

User.all # Called via GET "/users?admin=1"
User.new.admin # => 1


209
210
211
212
213
# File 'lib/her/model/orm.rb', line 209

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

Examples:

User.destroy_existing(1)
# Called via DELETE "/users/1"


248
249
250
251
252
253
254
255
256
257
# File 'lib/her/model/orm.rb', line 248

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

Parameters:

  • action (Symbol, String) (defaults to: nil)

    The behavior in question (‘:create` or `:update`)

  • method (Symbol, String) (defaults to: nil)

    The HTTP method to use (‘’PUT’‘, `:post`, etc.)



263
264
265
266
267
268
269
270
271
# File 'lib/her/model/orm.rb', line 263

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

Examples:

@user = User.save_existing(1, { :fullname => "Tobias Fünke" })
# Called via PUT "/users/1"


230
231
232
233
234
# File 'lib/her/model/orm.rb', line 230

def save_existing(id, params)
  save_existing!(id, params)
rescue Her::Errors::ResourceInvalid => e
  e.resource
end

#save_existing!(id, params) ⇒ Object

Similar to .save_existing but raises ResourceInvalid if save fails



237
238
239
240
241
# File 'lib/her/model/orm.rb', line 237

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

Examples:

class User
  include Her::Model

  scope :admins, lambda { where(:admin => 1) }
  scope :page, lambda { |page| where(:page => page) }
enc

User.admins # Called via GET "/users?admin=1"
User.page(2).all # Called via GET "/users?page=2"


183
184
185
186
187
188
189
190
191
# File 'lib/her/model/orm.rb', line 183

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