module Her::Model::ORM::ClassMethods

Public Instance Methods

build(attributes = {}) click to toggle source

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.

# 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
default_scope(block=nil) click to toggle source

Define the default scope for the model

@example

class User
  include Her::Model

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

User.all # Called via GET "/users?admin=1"
User.new.admin # => 1
# 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
destroy_existing(id, params={}) click to toggle source

Destroy an existing resource

@example

User.destroy_existing(1)
# Called via DELETE "/users/1"
# 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
method_for(action = nil, method = nil) click to toggle source

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

@param [Symbol, String] action The behavior in question (`:create` or `:update`) @param [Symbol, String] method The HTTP method to use (`'PUT'`, `:post`, etc.)

# 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
save_existing(id, params) click to toggle source

Save an existing resource and return it

@example

@user = User.save_existing(1, { :fullname => "Tobias Fünke" })
# Called via PUT "/users/1"
# File lib/her/model/orm.rb, line 148
def save_existing(id, params)
  resource = new(params.merge(primary_key => id))
  resource.save
  resource
end
scope(name, code) click to toggle source

Create a new chainable scope

@example

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"
# 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
scoped() click to toggle source

@private

# File lib/her/model/orm.rb, line 113
def scoped
  @_her_default_scope || blank_relation
end

Private Instance Methods

blank_relation() click to toggle source

@private

# File lib/her/model/orm.rb, line 199
def blank_relation
  @blank_relation ||= Relation.new(self)
end