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

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

Instance Method Summary (collapse)

Instance Method Details

- (Object) build(attributes = {})

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.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/her/model/orm.rb', line 181

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

- (Object) default_scope(block = nil)

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


128
129
130
131
132
# File 'lib/her/model/orm.rb', line 128

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

- (Object) destroy_existing(id, params = {})

Destroy an existing resource

Examples:

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


159
160
161
162
163
# File 'lib/her/model/orm.rb', line 159

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

- (Object) method_for(action = nil, method = nil)

Return or change the HTTP method used to create or update records



169
170
171
172
173
174
175
176
177
# File 'lib/her/model/orm.rb', line 169

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

- (Object) save_existing(id, params)

Save an existing resource and return it

Examples:

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


148
149
150
151
152
# File 'lib/her/model/orm.rb', line 148

def save_existing(id, params)
  resource = new(params.merge(primary_key => id))
  resource.save
  resource
end

- (Object) scope(name, code)

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"


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/her/model/orm.rb', line 100

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