class Her::Model::Relation

Attributes

params[RW]

@private

Public Class Methods

new(parent) click to toggle source

@private

# File lib/her/model/relation.rb, line 8
def initialize(parent)
  @parent = parent
  @params = {}
end

Public Instance Methods

all(params = {})
Alias for: where
apply_to(attributes) click to toggle source

@private

# File lib/her/model/relation.rb, line 14
def apply_to(attributes)
  @params.merge(attributes)
end
build(attributes = {}) click to toggle source

Build a new resource

# File lib/her/model/relation.rb, line 19
def build(attributes = {})
  @parent.build(@params.merge(attributes))
end
clear_fetch_cache!() click to toggle source

@private

# File lib/her/model/relation.rb, line 159
def clear_fetch_cache!
  instance_variable_set(:@_fetch, nil)
end
create(attributes = {}) click to toggle source

Create a resource and return it

@example

@user = User.create(:fullname => "Tobias Fünke")
# Called via POST "/users/1" with `&fullname=Tobias+Fünke`

@example

@user = User.where(:email => "tobias@bluth.com").create(:fullname => "Tobias Fünke")
# Called via POST "/users/1" with `&email=tobias@bluth.com&fullname=Tobias+Fünke`
# File lib/her/model/relation.rb, line 121
def create(attributes = {})
  attributes ||= {}
  resource = @parent.new(@params.merge(attributes))
  resource.save

  resource
end
fetch() click to toggle source

Fetch a collection of resources

@private

# File lib/her/model/relation.rb, line 66
def fetch
  @_fetch ||= begin
    path = @parent.build_request_path(@params)
    method = @parent.method_for(:find)
    @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, response|
      @parent.new_collection(parsed_data)
    end
  end
end
find(*ids) click to toggle source

Fetch specific resource(s) by their ID

@example

@user = User.find(1)
# Fetched via GET "/users/1"

@example

@users = User.find([1, 2])
# Fetched via GET "/users/1" and GET "/users/2"
# File lib/her/model/relation.rb, line 85
def find(*ids)
  params = @params.merge(ids.last.is_a?(Hash) ? ids.pop : {})
  ids = Array(params[@parent.primary_key]) if params.key?(@parent.primary_key)

  results = ids.flatten.compact.uniq.map do |id|
    resource = nil
    request_params = params.merge(
      :_method => @parent.method_for(:find),
      :_path => @parent.build_request_path(params.merge(@parent.primary_key => id))
    )

    @parent.request(request_params) do |parsed_data, response|
      if response.success?
        resource = @parent.new_from_parsed_data(parsed_data)
        resource.instance_variable_set(:@changed_attributes, {})
        resource.run_callbacks :find
      else
        return nil
      end
    end

    resource
  end

  ids.length > 1 || ids.first.kind_of?(Array) ? results : results.first
end
first_or_create(attributes = {}) click to toggle source

Fetch a resource and create it if it's not found

@example

@user = User.where(:email => "remi@example.com").find_or_create

# Returns the first item of the collection if present:
# GET "/users?email=remi@example.com"

# If collection is empty:
# POST /users with `email=remi@example.com`
# File lib/her/model/relation.rb, line 139
def first_or_create(attributes = {})
  fetch.first || create(attributes)
end
first_or_initialize(attributes = {}) click to toggle source

Fetch a resource and build it if it's not found

@example

@user = User.where(:email => "remi@example.com").find_or_initialize

# Returns the first item of the collection if present:
# GET "/users?email=remi@example.com"

# If collection is empty:
@user.email # => "remi@example.com"
@user.new? # => true
# File lib/her/model/relation.rb, line 154
def first_or_initialize(attributes = {})
  fetch.first || build(attributes)
end
kind_of?(thing) click to toggle source

@private

# File lib/her/model/relation.rb, line 59
def kind_of?(thing)
  fetch.kind_of?(thing)
end
method_missing(method, *args, &blk) click to toggle source

Bubble all methods to the fetched collection

@private

# File lib/her/model/relation.rb, line 44
def method_missing(method, *args, &blk)
  fetch.send(method, *args, &blk)
end
nil?() click to toggle source

@private

# File lib/her/model/relation.rb, line 54
def nil?
  fetch.nil?
end
respond_to?(method, *args) click to toggle source

@private

Calls superclass method
# File lib/her/model/relation.rb, line 49
def respond_to?(method, *args)
  super || fetch.respond_to?(method, *args)
end
where(params = {}) click to toggle source

Add a query string parameter

@example

@users = User.all
# Fetched via GET "/users"

@example

@users = User.where(:approved => 1).all
# Fetched via GET "/users?approved=1"
# File lib/her/model/relation.rb, line 32
def where(params = {})
  return self if params.blank? && !@_fetch.nil?
  self.clone.tap do |r|
    r.params = r.params.merge(params)
    r.clear_fetch_cache!
  end
end
Also aliased as: all