Class: Her::Model::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/her/model/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

Bubble all methods to the fetched collection



46
47
48
# File 'lib/her/model/relation.rb', line 46

def method_missing(method, *args, &blk)
  fetch.send(method, *args, &blk)
end

Instance Attribute Details

#parent=(value) ⇒ Object (writeonly)

Sets the attribute parent

Parameters:

  • value

    the value to set the attribute parent to.



7
8
9
# File 'lib/her/model/relation.rb', line 7

def parent=(value)
  @parent = value
end

Instance Method Details

#build(attributes = {}) ⇒ Object

Build a new resource



21
22
23
# File 'lib/her/model/relation.rb', line 21

def build(attributes = {})
  @parent.build(@params.merge(attributes))
end

#create(attributes = {}) ⇒ Object

Create a resource and return it

Examples:

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


166
167
168
169
170
171
172
# File 'lib/her/model/relation.rb', line 166

def create(attributes = {})
  attributes ||= {}
  resource = @parent.new(@params.merge(attributes))
  resource.save

  resource
end

#find(*ids) ⇒ Object

Fetch specific resource(s) by their ID

Examples:

@user = User.find(1)
# Fetched via GET "/users/1"
@users = User.find([1, 2])
# Fetched via GET "/users/1" and GET "/users/2"


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object

Fetch first resource with the given attributes.

If no resource is found, returns nil.

Examples:

@user = User.find_by(name: "Tobias", age: 42)
# Called via GET "/users?name=Tobias&age=42"


120
121
122
# File 'lib/her/model/relation.rb', line 120

def find_by(params)
  where(params).first
end

#find_or_create_by(attributes) ⇒ Object

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

Examples:

@user = User.find_or_create_by(email: "[email protected]")

# Returns the first item in the collection if present:
# Called via GET "/[email protected]"

# If collection is empty:
# POST /users with `[email protected]`
@user.email # => "[email protected]"
@user.new? # => false


137
138
139
# 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) ⇒ Object

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

Examples:

@user = User.find_or_initialize_by(email: "[email protected]")

# Returns the first item in the collection if present:
# Called via GET "/[email protected]"

# If collection is empty:
@user.email # => "[email protected]"
@user.new? # => true


153
154
155
# File 'lib/her/model/relation.rb', line 153

def find_or_initialize_by(attributes)
  find_by(attributes) || build(attributes)
end

#first_or_create(attributes = {}) ⇒ Object

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

Examples:

@user = User.where(:email => "[email protected]").find_or_create

# Returns the first item of the collection if present:
# GET "/[email protected]"

# If collection is empty:
# POST /users with `[email protected]`


184
185
186
# File 'lib/her/model/relation.rb', line 184

def first_or_create(attributes = {})
  fetch.first || create(attributes)
end

#first_or_initialize(attributes = {}) ⇒ Object

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

Examples:

@user = User.where(:email => "[email protected]").find_or_initialize

# Returns the first item of the collection if present:
# GET "/[email protected]"

# If collection is empty:
@user.email # => "[email protected]"
@user.new? # => true


199
200
201
# File 'lib/her/model/relation.rb', line 199

def first_or_initialize(attributes = {})
  fetch.first || build(attributes)
end

#where(params = {}) ⇒ Object Also known as: all

Add a query string parameter

Examples:

@users = User.all
# Fetched via GET "/users"
@users = User.where(:approved => 1).all
# Fetched via GET "/users?approved=1"


34
35
36
37
38
39
40
# 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