Class: NinjaModel::Relation
Constant Summary
collapse
- SINGLE_VALUE_ATTRS =
[:limit, :offset]
- MULTI_VALUE_ATTRS =
[:ordering, :predicates]
SpawnMethods::VALID_FIND_OPTIONS
Instance Attribute Summary collapse
Instance Method Summary
collapse
#limit, #order, #where
#all, #exists?, #find, #first
#apply_finder_options, #merge
Constructor Details
#initialize(klass) ⇒ Relation
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/ninja_model/relation.rb', line 20
def initialize(klass)
@klass = klass
@loaded = false
SINGLE_VALUE_ATTRS.each do |v|
instance_variable_set("@#{v}_value".to_sym, nil)
end
MULTI_VALUE_ATTRS.each do |v|
instance_variable_set("@#{v}".to_sym, [])
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/ninja_model/relation.rb', line 87
def method_missing(method, *args, &block)
if Array.method_defined?(method)
to_a.send(method, *args, &block)
elsif @klass.scopes[method]
merge(@klass.send(method, *args, &block))
elsif @klass.respond_to?(method)
scoping { @klass.send(method, *args, &block) }
else
super
end
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
11
12
13
|
# File 'lib/ninja_model/relation.rb', line 11
def klass
@klass
end
|
#limit_value ⇒ Object
Returns the value of attribute limit_value.
13
14
15
|
# File 'lib/ninja_model/relation.rb', line 13
def limit_value
@limit_value
end
|
#loaded ⇒ Object
Also known as:
loaded?
Returns the value of attribute loaded.
11
12
13
|
# File 'lib/ninja_model/relation.rb', line 11
def loaded
@loaded
end
|
#offset_value ⇒ Object
Returns the value of attribute offset_value.
13
14
15
|
# File 'lib/ninja_model/relation.rb', line 13
def offset_value
@offset_value
end
|
#ordering ⇒ Object
Returns the value of attribute ordering.
13
14
15
|
# File 'lib/ninja_model/relation.rb', line 13
def ordering
@ordering
end
|
#predicates ⇒ Object
Returns the value of attribute predicates.
13
14
15
|
# File 'lib/ninja_model/relation.rb', line 13
def predicates
@predicates
end
|
Instance Method Details
#blank? ⇒ Boolean
61
62
63
|
# File 'lib/ninja_model/relation.rb', line 61
def blank?
empty?
end
|
#empty? ⇒ Boolean
65
66
67
|
# File 'lib/ninja_model/relation.rb', line 65
def empty?
size.zero?
end
|
#inspect ⇒ Object
70
71
72
|
# File 'lib/ninja_model/relation.rb', line 70
def inspect
to_a.inspect
end
|
#inspect! ⇒ Object
69
|
# File 'lib/ninja_model/relation.rb', line 69
alias :inspect! :inspect
|
#new(*args, &block) ⇒ Object
Also known as:
build
33
34
35
|
# File 'lib/ninja_model/relation.rb', line 33
def new(*args, &block)
scoping { @klass.new(*args, &block) }
end
|
#scope_for_create ⇒ Object
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/ninja_model/relation.rb', line 74
def scope_for_create
Hash[@predicates.find_all { |w|
w.respond_to?(:meth) && w.meth == :eq
}.map { |where|
[
where.attribute,
where.value
]
}]
end
|
#scoping ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/ninja_model/relation.rb', line 48
def scoping
@klass.scoped_methods << self
begin
yield
ensure
@klass.scoped_methods.pop
end
end
|
#size ⇒ Object
57
58
59
|
# File 'lib/ninja_model/relation.rb', line 57
def size
to_a.length
end
|
#to_a ⇒ Object
Also known as:
to_ary
39
40
41
42
43
44
45
|
# File 'lib/ninja_model/relation.rb', line 39
def to_a
@records ||= begin
records = @klass.adapter.read(self)
@loaded = true
records
end
end
|