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, :readonly]
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

#extending, #limit, #order, #readonly, #where

Methods included from FinderMethods

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

Methods included from SpawnMethods

#apply_finder_options, #merge

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_scopedObject 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

#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

#readonly_valueObject

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/ninja_model/relation.rb', line 64

def blank?
  empty?
end

#empty?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/ninja_model/relation.rb', line 68

def empty?
  size.zero?
end

#inspectObject



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_createObject



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

#scopingObject



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

#sizeObject



60
61
62
# File 'lib/ninja_model/relation.rb', line 60

def size
  to_a.length
end

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