Class: Morpheus::Relation

Inherits:
ActiveSupport::BasicObject
Defined in:
lib/morpheus/relation.rb

Instance Method Summary collapse

Constructor Details

#initialize(owner) ⇒ Relation

Returns a new instance of Relation.



8
9
10
11
12
# File 'lib/morpheus/relation.rb', line 8

def initialize(owner)
  @owner  = owner
  @target = nil
  @params = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (private)



52
53
54
55
56
57
58
# File 'lib/morpheus/relation.rb', line 52

def method_missing(m, *args, &block)
  if ::Array.method_defined?(m)
    loaded_target.send(m, *args, &block)
  else
    loaded_target.send(m, *args, &block)
  end
end

Instance Method Details

#allObject



30
31
32
# File 'lib/morpheus/relation.rb', line 30

def all
  loaded_target
end

#classObject



4
5
6
# File 'lib/morpheus/relation.rb', line 4

def class
  ::Morpheus::Relation
end

#limit(amount) ⇒ Object



19
20
21
22
# File 'lib/morpheus/relation.rb', line 19

def limit(amount)
  @params.merge!(:limit => amount)
  self
end

#page(page_number) ⇒ Object



24
25
26
27
28
# File 'lib/morpheus/relation.rb', line 24

def page(page_number)
  offset = (page_number - 1) * @owner.results_per_page
  @params.merge!(:limit => @owner.results_per_page, :offset => offset)
  self
end

#to_aObject



34
35
36
# File 'lib/morpheus/relation.rb', line 34

def to_a
  loaded_target
end

#to_json(*args) ⇒ Object



38
39
40
# File 'lib/morpheus/relation.rb', line 38

def to_json(*args)
  to_a.to_json(*args)
end

#where(attributes) ⇒ Object



14
15
16
17
# File 'lib/morpheus/relation.rb', line 14

def where(attributes)
  @params.merge!(attributes)
  self
end