module Her::Model::ORM

This module adds ORM-like capabilities to the model

Public Instance Methods

decrement(attribute, by = 1) click to toggle source

Initializes attribute to zero if nil and substracts the value passed as by (default is 1). The decrement is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based attributes. Returns self.

# File lib/her/model/orm.rb, line 112
def decrement(attribute, by = 1)
  increment(attribute, -by)
end
decrement!(attribute, by = 1) click to toggle source

Wrapper around decrement that saves the resource. Saving is subjected to validation checks. Returns self.

# File lib/her/model/orm.rb, line 118
def decrement!(attribute, by = 1)
  increment!(attribute, -by)
end
destroy(params = {}) click to toggle source

Destroy a resource

@example

@user = User.find(1)
@user.destroy
# Called via DELETE "/users/1"
# File lib/her/model/orm.rb, line 80
def destroy(params = {})
  method = self.class.method_for(:destroy)
  run_callbacks :destroy do
    self.class.request(params.merge(:_method => method, :_path => request_path)) do |parsed_data, response|
      load_from_parsed_data(parsed_data)
      @destroyed = response.success?
    end
  end
  self
end
destroyed?() click to toggle source

Return whether the object has been destroyed

# File lib/her/model/orm.rb, line 21
def destroyed?
  @destroyed == true
end
increment(attribute, by = 1) click to toggle source

Initializes attribute to zero if nil and adds the value passed as by (default is 1). The increment is performed directly on the underlying attribute, no setter is invoked. Only makes sense for number-based attributes. Returns self.

# File lib/her/model/orm.rb, line 95
def increment(attribute, by = 1)
  attributes[attribute] ||= 0
  attributes[attribute] += by
  self
end
increment!(attribute, by = 1) click to toggle source

Wrapper around increment that saves the resource. Saving is subjected to validation checks. Returns self.

# File lib/her/model/orm.rb, line 103
def increment!(attribute, by = 1)
  increment(attribute, by) && save
  self
end
load_from_parsed_data(parsed_data) click to toggle source

Uses parsed response to assign attributes and metadata

@private

# File lib/her/model/orm.rb, line 163
def load_from_parsed_data(parsed_data)
  data = parsed_data[:data]
  assign_attributes(self.class.parse(data)) if data.any?
  @metadata = parsed_data[:metadata]
  @response_errors = parsed_data[:errors]
end
new?() click to toggle source

Return `true` if a resource was not saved yet

# File lib/her/model/orm.rb, line 10
def new?
  id.nil?
end
Also aliased as: new_record?
new_record?()
Alias for: new?
persisted?() click to toggle source

Return `true` if a resource is not `#new?`

# File lib/her/model/orm.rb, line 16
def persisted?
  !new?
end
reload(options = nil) click to toggle source

Refetches the resource

This method finds the resource by its primary key (which could be assigned manually) and modifies the object in-place.

@example

user = User.find(1)
# => #<User(users/1) id=1 name="Tobias Fünke">
user.name = "Oops"
user.reload # Fetched again via GET "/users/1"
# => #<User(users/1) id=1 name="Tobias Fünke">
# File lib/her/model/orm.rb, line 154
def reload(options = nil)
  fresh_object = self.class.find(id)
  assign_attributes(fresh_object.attributes)
  self
end
save() click to toggle source

Save a resource and return `false` if the response is not a successful one or if there are errors in the resource. Otherwise, return the newly updated resource

@example Save a resource after fetching it

@user = User.find(1)
# Fetched via GET "/users/1"
@user.fullname = "Tobias Fünke"
@user.save
# Called via PUT "/users/1"

@example Save a new resource by creating it

@user = User.new({ :fullname => "Tobias Fünke" })
@user.save
# Called via POST "/users"
# File lib/her/model/orm.rb, line 39
def save
  callback = new? ? :create : :update
  method = self.class.method_for(callback)

  run_callbacks :save do
    run_callbacks callback do
      self.class.request(to_params.merge(:_method => method, :_path => request_path)) do |parsed_data, response|
        load_from_parsed_data(parsed_data)
        return false if !response.success? || @response_errors.any?
        changes_applied
      end
    end
  end

  self
end
save!() click to toggle source

Similar to save(), except that ResourceInvalid is raised if the save fails

# File lib/her/model/orm.rb, line 57
def save!
  unless save
    raise Her::Errors::ResourceInvalid, self
  end
  self
end
toggle(attribute) click to toggle source

Assigns to attribute the boolean opposite of attribute?. So if the predicate returns true the attribute will become false. This method toggles directly the underlying value without calling any setter. Returns self.

@example

user = User.first
user.admin? # => false
user.toggle(:admin)
user.admin? # => true
# File lib/her/model/orm.rb, line 132
def toggle(attribute)
  attributes[attribute] = !public_send("#{attribute}?")
  self
end
toggle!(attribute) click to toggle source

Wrapper around toggle that saves the resource. Saving is subjected to validation checks. Returns true if the record could be saved.

# File lib/her/model/orm.rb, line 139
def toggle!(attribute)
  toggle(attribute) && save
end
update_attributes(attributes) click to toggle source

Update a resource and return it

@example

@user = User.find(1)
@user.update_attributes(:name => "Tobias Fünke")
# Called via PUT "/users/1"
# File lib/her/model/orm.rb, line 70
def update_attributes(attributes)
  assign_attributes(attributes) && save
end