Class: LazyRecord::Relation

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lazy_record/relation.rb

Overview

Collection of LazyRecord objects that are bound to a single class. The Relation inherits scope methods from the class it is bound to.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass:, collection: []) ⇒ Relation

Returns a new instance of Relation.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/lazy_record/relation.rb', line 11

def initialize(klass:, collection: [])
  klass = klass.call if klass.is_a? Proc
  klass_argument_error unless klass.is_a?(Class)
  @klass = klass
  @all   = []
  self_extend_scopes_module
  collection.each do |object|
    @all << object && next if object.is_a?(klass)
    collection_argument_error
  end
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/lazy_record/relation.rb', line 9

def klass
  @klass
end

Instance Method Details

#<<(other) ⇒ Object

Raises:

  • (ArgumentError)


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

def <<(other)
  message = "object must be of type #{klass}"
  raise ArgumentError, message unless other.is_a?(klass)
  all << other unless all.include?(other)
end

#[](index) ⇒ Object



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

def [](index)
  all[index]
end

#collection_argument_errorObject

Raises:

  • (ArgumentError)


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

def collection_argument_error
  message = "Argument must be a collection of #{klass.to_s.tableize}"
  raise ArgumentError, message
end

#each(&block) ⇒ Object



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

def each(&block)
  all.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/lazy_record/relation.rb', line 72

def empty?
  all.empty?
end

#inspectObject



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

def inspect
  "\#<#{klass}Relation [#{all.map(&:inspect).join(', ')}]>"
end

#klass_argument_errorObject

Raises:

  • (ArgumentError)


28
29
30
# File 'lib/lazy_record/relation.rb', line 28

def klass_argument_error
  raise ArgumentError, '`klass` keyword argument must be a class'
end

#lastObject



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

def last
  self[-1]
end

#self_extend_scopes_moduleObject



82
83
84
85
86
# File 'lib/lazy_record/relation.rb', line 82

def self_extend_scopes_module
  return unless klass.const_defined?(Scopes::SCOPE_MODULE_NAME,
                                     _search_ancestors = false)
  extend(klass.const_get(Scopes::SCOPE_MODULE_NAME))
end

#where(condition = nil) ⇒ Object



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

def where(condition = nil)
  result = all.select do |instance|
    if condition.is_a? Hash
      select_by_hash_condition(condition, instance)
    elsif block_given?
      yield instance
    end
  end
  self.class.new(klass: klass, collection: result)
end