Class: Eco::Language::Models::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/eco/language/models/collection.rb

Constant Summary collapse

BASIC_METHODS =
%w[present empty present_all? present_some?].freeze
EXTENDED_METHODS =
BASIC_METHODS + %w[exclude remove attr attr? attrs unique_attrs contains]

pure collection methods collapse

`attr` dependant methods collapse

`attr` presence methods collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = [], klass:, factory: nil, handy: Eco::Assets::Language.new) ⇒ Collection

Returns a new instance of Collection.



37
38
39
40
41
42
43
# File 'lib/eco/language/models/collection.rb', line 37

def initialize(data = [], klass:, factory: nil, handy: Eco::Assets::Language.new)
  raise "Raise klass required, given: #{klass}" unless klass
  @klass   = klass
  @factory = factory
  @handy   = handy
  @items   = to_klass(data)
end

Class Method Details

.attr_collection(*attrs) ⇒ Object



17
18
19
20
# File 'lib/eco/language/models/collection.rb', line 17

def attr_collection(*attrs)
  block = ->(method) { attrs_create_method(attrs, method) }
  EXTENDED_METHODS.each(&block)
end

.attr_presence(*attrs) ⇒ Object



12
13
14
15
# File 'lib/eco/language/models/collection.rb', line 12

def attr_presence(*attrs)
  block = ->(method) { attrs_create_method(attrs, method) }
  BASIC_METHODS.each(&block)
end

.attrs_create_method(attrs, method) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eco/language/models/collection.rb', line 22

def attrs_create_method(attrs, method)
  attrs.each do |attr|
    attr = attr.to_s
    if method.include?("attr")
      attr_method = method.sub("attr", attr)
    else
      attr_method = "#{attr}_#{method}"
    end
    define_method attr_method do |*args|
      send(method, attr, *args)
    end
  end
end

Instance Method Details

#<(other) ⇒ Object



76
77
78
79
# File 'lib/eco/language/models/collection.rb', line 76

def <(other)
  @items.clear
  self << other
end

#<<(other) ⇒ Object



81
82
83
84
85
# File 'lib/eco/language/models/collection.rb', line 81

def <<(other)
  @items.concat(into_a(other))
  on_change
  self
end

#attr(attr, value = true, modifier = default_modifier) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/eco/language/models/collection.rb', line 105

def attr(attr, value = true, modifier = default_modifier)
  return present(attr, value) if boolean?(value)
  select do |object|
    match?(attr_value(object, attr), value, modifier)
  end.then do |matching|
    newFrom matching
  end
end

#attr?(attr, value = true, modifier = default_modifier) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
# File 'lib/eco/language/models/collection.rb', line 114

def attr?(attr, value = true, modifier = default_modifier)
  return present(attr, value).length == length if boolean?(value)
  match?(attrs(attr), value, modifier.new.reverse)
end

#attrs(attr) ⇒ Object



123
124
125
# File 'lib/eco/language/models/collection.rb', line 123

def attrs(attr)
  map { |object| attr_value(object, attr) }
end

#contains(attr, value, modifier = default_modifier) ⇒ Object



119
120
121
# File 'lib/eco/language/models/collection.rb', line 119

def contains(attr, value, modifier = default_modifier)
  self.attr(attr, value, modifier.new.pattern)
end

#delete!(value) ⇒ Object



91
92
93
# File 'lib/eco/language/models/collection.rb', line 91

def delete!(value)
  self < @items - into_a(value)
end

#each(&block) ⇒ Object



71
72
73
74
# File 'lib/eco/language/models/collection.rb', line 71

def each(&block)
  return to_enum(:each) unless block
  @items.each(&block)
end

#empty(attr, flag = true) ⇒ Object



151
152
153
# File 'lib/eco/language/models/collection.rb', line 151

def empty(attr, flag = true)
  present(attr, !flag)
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/eco/language/models/collection.rb', line 67

def empty?
  count.zero?
end

#exclude(attr, value, modifier = default_modifier) ⇒ Object



97
98
99
# File 'lib/eco/language/models/collection.rb', line 97

def exclude(attr, value, modifier = default_modifier)
  newFrom @items - self.attr(attr, value, modifier)
end

#group_by(attr = nil, &block) ⇒ Object



131
132
133
134
# File 'lib/eco/language/models/collection.rb', line 131

def group_by(attr = nil, &block)
  return to_h(attr) if attr
  to_a.group_by(&block) if block
end

#lengthObject



63
64
65
# File 'lib/eco/language/models/collection.rb', line 63

def length
  count
end

#merge(data) ⇒ Object



58
59
60
61
# File 'lib/eco/language/models/collection.rb', line 58

def merge(data)
  data = data.to_a unless data.is_a?(Array)
  newFrom to_a + data
end

#newObject



50
51
52
# File 'lib/eco/language/models/collection.rb', line 50

def new
  newFrom to_a
end

#newFrom(data) ⇒ Object

rubocop:disable Naming/MethodName



54
55
56
# File 'lib/eco/language/models/collection.rb', line 54

def newFrom(data) # rubocop:disable Naming/MethodName
  self.class.new(data, klass: @klass, factory: @factory)
end

#present(attr, flag = true) ⇒ Object



146
147
148
149
# File 'lib/eco/language/models/collection.rb', line 146

def present(attr, flag = true)
  block = ->(o) { attr_value_present?(o, attr) == !!flag } # rubocop:disable Style/DoubleNegation
  newFrom select(&block)
end

#present_all?(attr, flag = true) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
# File 'lib/eco/language/models/collection.rb', line 155

def present_all?(attr, flag = true)
  present(attr, flag).length == length
end

#present_some?(attr, flag = true) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/eco/language/models/collection.rb', line 159

def present_some?(attr, flag = true)
  present(attr, flag).length.positive?
end

#remove(attr, value, modifier = default_modifier) ⇒ Object



101
102
103
# File 'lib/eco/language/models/collection.rb', line 101

def remove(attr, value, modifier = default_modifier)
  self < exclude(attr, value, modifier)
end

#to_cObject



46
47
48
# File 'lib/eco/language/models/collection.rb', line 46

def to_c
  Collection.new(self, klass: @klass, factory: @factory)
end

#to_h(attr, &block) ⇒ Object

Note:

either one or the other should be present

By a specific attr or a block



138
139
140
141
142
# File 'lib/eco/language/models/collection.rb', line 138

def to_h(attr, &block)
  return to_a.group_by(&block) if block
  raise "And attr or a block are required. Given attr: #{attr}" unless attr
  to_a.group_by { |object| object.method(attr).call }
end

#unique_attrs(attr) ⇒ Object



127
128
129
# File 'lib/eco/language/models/collection.rb', line 127

def unique_attrs(attr)
  to_h(attr).keys
end

#update(&block) ⇒ Object



87
88
89
# File 'lib/eco/language/models/collection.rb', line 87

def update(&block)
  newFrom map(&block)
end