Class: NinjaModel::Relation
- Inherits:
-
Object
- Object
- NinjaModel::Relation
- Includes:
- FinderMethods, QueryMethods, SpawnMethods
- Defined in:
- lib/ninja_model/relation.rb
Constant Summary collapse
- SINGLE_VALUE_ATTRS =
[:limit, :offset, :readonly]
- MULTI_VALUE_ATTRS =
[:ordering, :predicates]
Constants included from SpawnMethods
SpawnMethods::VALID_FIND_OPTIONS
Instance Attribute Summary collapse
-
#default_scoped ⇒ Object
(also: #default_scoped?)
Returns the value of attribute default_scoped.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#limit_value ⇒ Object
Returns the value of attribute limit_value.
-
#loaded ⇒ Object
(also: #loaded?)
readonly
Returns the value of attribute loaded.
-
#offset_value ⇒ Object
Returns the value of attribute offset_value.
-
#ordering ⇒ Object
Returns the value of attribute ordering.
-
#predicates ⇒ Object
Returns the value of attribute predicates.
-
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
Instance Method Summary collapse
- #blank? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(klass) ⇒ Relation
constructor
A new instance of Relation.
- #inspect ⇒ Object
- #inspect! ⇒ Object
- #new(*args, &block) ⇒ Object (also: #build)
- #scope_for_create ⇒ Object
- #scoping ⇒ Object
- #size ⇒ Object
- #to_a ⇒ Object (also: #to_ary)
Methods included from QueryMethods
#extending, #limit, #order, #readonly, #where
Methods included from FinderMethods
Methods included from SpawnMethods
Constructor Details
#initialize(klass) ⇒ Relation
Returns a new instance of Relation.
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/ninja_model/relation.rb', line 22 def initialize(klass) @klass = klass @loaded = false @default_scoped = 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 (protected)
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ninja_model/relation.rb', line 90 def method_missing(method, *args, &block) if Array.method_defined?(method) to_a.send(method, *args, &block) elsif @klass.singleton_class.respond_to?(method) merge(@klass.singleton_class.send(method, *args, &block)) elsif @klass.respond_to?(method) scoping { @klass.send(method, *args, &block) } else super end end |
Instance Attribute Details
#default_scoped ⇒ Object Also known as: default_scoped?
Returns the value of attribute default_scoped.
14 15 16 |
# File 'lib/ninja_model/relation.rb', line 14 def default_scoped @default_scoped end |
#klass ⇒ Object (readonly)
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 (readonly) 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 |
#readonly_value ⇒ Object
Returns the value of attribute readonly_value.
13 14 15 |
# File 'lib/ninja_model/relation.rb', line 13 def readonly_value @readonly_value end |
Instance Method Details
#blank? ⇒ Boolean
64 65 66 |
# File 'lib/ninja_model/relation.rb', line 64 def blank? empty? end |
#empty? ⇒ Boolean
68 69 70 |
# File 'lib/ninja_model/relation.rb', line 68 def empty? size.zero? end |
#inspect ⇒ Object
73 74 75 |
# File 'lib/ninja_model/relation.rb', line 73 def inspect to_a.inspect end |
#inspect! ⇒ Object
72 |
# File 'lib/ninja_model/relation.rb', line 72 alias :inspect! :inspect |
#new(*args, &block) ⇒ Object Also known as: build
36 37 38 |
# File 'lib/ninja_model/relation.rb', line 36 def new(*args, &block) scoping { @klass.new(*args, &block) } end |
#scope_for_create ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ninja_model/relation.rb', line 77 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
51 52 53 54 55 56 57 58 |
# File 'lib/ninja_model/relation.rb', line 51 def scoping @klass.scoped_methods << self begin yield ensure @klass.scoped_methods.pop end end |
#size ⇒ Object
60 61 62 |
# File 'lib/ninja_model/relation.rb', line 60 def size to_a.length end |
#to_a ⇒ Object Also known as: to_ary
42 43 44 45 46 47 48 |
# File 'lib/ninja_model/relation.rb', line 42 def to_a @records ||= begin records = @klass.adapter.read(self) @loaded = true records end end |