Class: LazyRecord::Relation
- Inherits:
-
Object
- Object
- LazyRecord::Relation
- 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
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
Instance Method Summary collapse
- #<<(other) ⇒ Object
- #[](index) ⇒ Object
- #collection_argument_error ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(klass:, collection: []) ⇒ Relation
constructor
A new instance of Relation.
- #inspect ⇒ Object
- #klass_argument_error ⇒ Object
- #last ⇒ Object
- #self_extend_scopes_module ⇒ Object
- #where(condition = nil) ⇒ Object
Constructor Details
#initialize(klass:, collection: []) ⇒ 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
#klass ⇒ Object (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
32 33 34 35 36 |
# File 'lib/lazy_record/relation.rb', line 32 def <<(other) = "object must be of type #{klass}" raise ArgumentError, 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_error ⇒ Object
23 24 25 26 |
# File 'lib/lazy_record/relation.rb', line 23 def collection_argument_error = "Argument must be a collection of #{klass.to_s.tableize}" raise ArgumentError, end |
#each(&block) ⇒ Object
60 61 62 |
# File 'lib/lazy_record/relation.rb', line 60 def each(&block) all.each(&block) end |
#empty? ⇒ Boolean
72 73 74 |
# File 'lib/lazy_record/relation.rb', line 72 def empty? all.empty? end |
#inspect ⇒ Object
38 39 40 |
# File 'lib/lazy_record/relation.rb', line 38 def inspect "\#<#{klass}Relation [#{all.map(&:inspect).join(', ')}]>" end |
#klass_argument_error ⇒ Object
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 |
#last ⇒ Object
68 69 70 |
# File 'lib/lazy_record/relation.rb', line 68 def last self[-1] end |
#self_extend_scopes_module ⇒ Object
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 |