Class: NinjaModel::Relation

Inherits:
Object
  • Object
show all
Includes:
FinderMethods, QueryMethods, SpawnMethods
Defined in:
lib/ninja_model/relation.rb

Constant Summary collapse

SINGLE_VALUE_ATTRS =
[:limit, :offset]
MULTI_VALUE_ATTRS =
[:ordering, :predicates]

Constants included from SpawnMethods

SpawnMethods::VALID_FIND_OPTIONS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from QueryMethods

#limit, #order, #where

Methods included from FinderMethods

#all, #exists?, #find, #first

Methods included from SpawnMethods

#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 (protected)



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

#klassObject (readonly)

Returns the value of attribute klass.



11
12
13
# File 'lib/ninja_model/relation.rb', line 11

def klass
  @klass
end

#limit_valueObject

Returns the value of attribute limit_value.



13
14
15
# File 'lib/ninja_model/relation.rb', line 13

def limit_value
  @limit_value
end

#loadedObject (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_valueObject

Returns the value of attribute offset_value.



13
14
15
# File 'lib/ninja_model/relation.rb', line 13

def offset_value
  @offset_value
end

#orderingObject

Returns the value of attribute ordering.



13
14
15
# File 'lib/ninja_model/relation.rb', line 13

def ordering
  @ordering
end

#predicatesObject

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

#inspectObject



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_createObject



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

#scopingObject



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

#sizeObject



57
58
59
# File 'lib/ninja_model/relation.rb', line 57

def size
  to_a.length
end

#to_aObject 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