Class: Her::Model::Relation

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

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



44
45
46
# File 'lib/castle-her/model/relation.rb', line 44

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

Instance Method Details

#build(attributes = {}) ⇒ Object

Build a new resource



19
20
21
# File 'lib/castle-her/model/relation.rb', line 19

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`


121
122
123
124
125
126
127
# File 'lib/castle-her/model/relation.rb', line 121

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"


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/castle-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 = {}) ⇒ 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]`


139
140
141
# File 'lib/castle-her/model/relation.rb', line 139

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


154
155
156
# File 'lib/castle-her/model/relation.rb', line 154

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"


32
33
34
35
36
37
38
# File 'lib/castle-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