class Her::Model::Relation

Attributes

params[RW]

@private

parent[W]

Public Class Methods

new(parent) click to toggle source

@private

# File lib/her/model/relation.rb, line 10
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 16
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 21
def build(attributes = {})
  @parent.build(@params.merge(attributes))
end
clear_fetch_cache!() click to toggle source

@private

# File lib/her/model/relation.rb, line 204
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 166
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 68
def fetch
  @_fetch ||= begin
    path = @parent.build_request_path(@parent.collection_path, @params)
    method = @parent.method_for(:find)
    @parent.request(@params.merge(:_method => method, :_path => path)) do |parsed_data, _|
      @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 87
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.run_callbacks :find
      else
        return nil
      end
    end

    resource
  end

  ids.length > 1 || ids.first.is_a?(Array) ? results : results.first
end
find_by(params) click to toggle source

Fetch first resource with the given attributes.

If no resource is found, returns nil.

@example

@user = User.find_by(name: "Tobias", age: 42)
# Called via GET "/users?name=Tobias&age=42"
# File lib/her/model/relation.rb, line 120
def find_by(params)
  where(params).first
end
find_or_create_by(attributes) click to toggle source

Fetch first resource with the given attributes, or create a resource with the attributes if one is not found.

@example

@user = User.find_or_create_by(email: "remi@example.com")

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

# If collection is empty:
# POST /users with `email=remi@example.com`
@user.email # => "remi@example.com"
@user.new? # => false
# File lib/her/model/relation.rb, line 137
def find_or_create_by(attributes)
  find_by(attributes) || create(attributes)
end
find_or_initialize_by(attributes) click to toggle source

Fetch first resource with the given attributes, or initialize a resource with the attributes if one is not found.

@example

@user = User.find_or_initialize_by(email: "remi@example.com")

# Returns the first item in the collection if present:
# Called via 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 153
def find_or_initialize_by(attributes)
  find_by(attributes) || build(attributes)
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 184
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 199
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 61
def kind_of?(thing)
  fetch.is_a?(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 46
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 56
def nil?
  fetch.nil?
end
respond_to?(method, *args) click to toggle source

@private

Calls superclass method
# File lib/her/model/relation.rb, line 51
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 34
def where(params = {})
  return self if params.blank? && !@_fetch.nil?
  clone.tap do |r|
    r.params = r.params.merge(params)
    r.clear_fetch_cache!
  end
end
Also aliased as: all