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.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/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

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


130
131
132
133
134
# File 'lib/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

Examples:

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


161
162
163
164
165
# File 'lib/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

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.)



171
172
173
174
175
176
177
178
179
# File 'lib/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

Examples:

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


150
151
152
153
154
# File 'lib/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

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"


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/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