Class: ActiveAny::Relation

Inherits:
Object
  • Object
show all
Includes:
Delegation, FinderMethods, QueryMethods, Enumerable
Defined in:
lib/active_any/relation.rb,
lib/active_any/relation/merger.rb,
lib/active_any/relation/order_clause.rb,
lib/active_any/relation/where_clause.rb,
lib/active_any/relation/query_methods.rb,
lib/active_any/relation/finder_methods.rb

Defined Under Namespace

Modules: FinderMethods, QueryMethods Classes: HashMerger, ImmutableRelation, Merger, OrderClause, WhereClause

Constant Summary collapse

MULTI_VALUE_METHODS =
%i[group includes join].freeze
SINGLE_VALUE_METHODS =
%i[limit].freeze
CLAUSE_METHODS =
%i[where order].freeze
VALUE_METHODS =
(MULTI_VALUE_METHODS + SINGLE_VALUE_METHODS + CLAUSE_METHODS).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from QueryMethods

#get_value, #group, #group!, #includes, #includes!, #limit, #limit!, #order, #order!, #reverse_order, #reverse_order!, #set_value, #take, #values, #where, #where!

Methods included from FinderMethods

#find_by, #first, #last

Constructor Details

#initialize(klass) ⇒ Relation

Returns a new instance of Relation.



31
32
33
34
35
36
# File 'lib/active_any/relation.rb', line 31

def initialize(klass)
  @klass = klass
  @records = []
  @loaded = false
  @values = {}
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



12
13
14
# File 'lib/active_any/relation.rb', line 12

def klass
  @klass
end

#loadedObject (readonly)

Returns the value of attribute loaded.



12
13
14
# File 'lib/active_any/relation.rb', line 12

def loaded
  @loaded
end

Class Method Details

.create(klass, *args) ⇒ Object



23
24
25
# File 'lib/active_any/relation.rb', line 23

def self.create(klass, *args)
  relation_class_for(klass).new(klass, *args)
end

.relation_class_for(klass) ⇒ Object



27
28
29
# File 'lib/active_any/relation.rb', line 27

def self.relation_class_for(klass)
  klass.relation_delegate_class(self)
end

Instance Method Details

#eager_loading?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/active_any/relation.rb', line 90

def eager_loading?
  false
end

#initialize_copyObject



69
70
71
72
73
# File 'lib/active_any/relation.rb', line 69

def initialize_copy(*)
  @values = @values.dup
  reset
  super
end

#inspectObject



101
102
103
104
105
106
107
108
# File 'lib/active_any/relation.rb', line 101

def inspect
  subject = loaded? ? records : self
  entries = subject.take([limit_value, 11].compact.min).map!(&:inspect)

  entries[10] = '...' if entries.size == 11

  "#<#{self.class.name} [#{entries.join(', ')}]>"
end

#loadObject



85
86
87
88
# File 'lib/active_any/relation.rb', line 85

def load
  exec_query unless loaded
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/active_any/relation.rb', line 81

def loaded?
  @loaded
end

#merge(other) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/active_any/relation.rb', line 42

def merge(other)
  if other.is_a?(Array)
    records & other
  elsif other
    spawn.merge!(other)
  else
    raise ArgumentError, "invalid argument: #{other.inspect}."
  end
end

#merge!(other) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_any/relation.rb', line 52

def merge!(other) # :nodoc:
  if other.is_a?(::Hash)
    Relation::HashMerger.new(self, other).merge
  elsif other.is_a?(Relation)
    Relation::Merger.new(self, other).merge
  elsif other.respond_to?(:to_proc)
    instance_exec(&other)
  else
    raise ArgumentError, "#{other.inspect} is not an ActiveAny::Relation"
  end
end

#recordsObject



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

def records
  load
  @records
end

#resetObject



75
76
77
78
79
# File 'lib/active_any/relation.rb', line 75

def reset
  @loaded = nil
  @records = [].freeze
  self
end

#scopingObject



94
95
96
97
98
99
# File 'lib/active_any/relation.rb', line 94

def scoping
  # previous, klass.current_scope = klass.current_scope, self
  yield
  # ensure
  # klass.current_scope = previous
end

#to_aObject



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

def to_a
  records.dup
end