Class: Eco::Language::Models::Collection
- Inherits:
-
Object
- Object
- Eco::Language::Models::Collection
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(attr, value = true, modifier = default_modifier) ⇒ Object
-
#attr?(attr, value = true, modifier = default_modifier) ⇒ Boolean
-
#attrs(attr) ⇒ Object
-
#contains(attr, value, modifier = default_modifier) ⇒ Object
-
#exclude(attr, value, modifier = default_modifier) ⇒ Object
-
#group_by(attr = nil, &block) ⇒ Object
-
#remove(attr, value, modifier = default_modifier) ⇒ Object
-
#to_h(attr, &block) ⇒ Object
By a specific attr or a block.
-
#unique_attrs(attr) ⇒ Object
`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.
39
40
41
42
43
44
45
46
|
# File 'lib/eco/language/models/collection.rb', line 39
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
35
36
|
# 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
80
81
82
83
|
# File 'lib/eco/language/models/collection.rb', line 80
def <(other)
@items.clear
self << other
end
|
#<<(other) ⇒ Object
85
86
87
88
89
|
# File 'lib/eco/language/models/collection.rb', line 85
def <<(other)
@items.concat(into_a(other))
on_change
self
end
|
#attr(attr, value = true, modifier = default_modifier) ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/eco/language/models/collection.rb', line 109
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
119
120
121
122
123
|
# File 'lib/eco/language/models/collection.rb', line 119
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
129
130
131
|
# File 'lib/eco/language/models/collection.rb', line 129
def attrs(attr)
map { |object| attr_value(object, attr) }
end
|
#contains(attr, value, modifier = default_modifier) ⇒ Object
125
126
127
|
# File 'lib/eco/language/models/collection.rb', line 125
def contains(attr, value, modifier = default_modifier)
self.attr(attr, value, modifier.new.pattern)
end
|
#delete!(value) ⇒ Object
95
96
97
|
# File 'lib/eco/language/models/collection.rb', line 95
def delete!(value)
self < @items - into_a(value)
end
|
#each(&block) ⇒ Object
74
75
76
77
78
|
# File 'lib/eco/language/models/collection.rb', line 74
def each(&block)
return to_enum(:each) unless block
@items.each(&block)
end
|
#empty(attr, flag = true) ⇒ Object
159
160
161
|
# File 'lib/eco/language/models/collection.rb', line 159
def empty(attr, flag = true)
present(attr, !flag)
end
|
#empty? ⇒ Boolean
70
71
72
|
# File 'lib/eco/language/models/collection.rb', line 70
def empty?
count.zero?
end
|
#exclude(attr, value, modifier = default_modifier) ⇒ Object
101
102
103
|
# File 'lib/eco/language/models/collection.rb', line 101
def exclude(attr, value, modifier = default_modifier)
newFrom @items - self.attr(attr, value, modifier)
end
|
#group_by(attr = nil, &block) ⇒ Object
137
138
139
140
141
|
# File 'lib/eco/language/models/collection.rb', line 137
def group_by(attr = nil, &block)
return to_h(attr) if attr
to_a.group_by(&block) if block
end
|
66
67
68
|
# File 'lib/eco/language/models/collection.rb', line 66
def length
count
end
|
#merge(data) ⇒ Object
61
62
63
64
|
# File 'lib/eco/language/models/collection.rb', line 61
def merge(data)
data = data.to_a unless data.is_a?(Array)
newFrom to_a + data
end
|
53
54
55
|
# File 'lib/eco/language/models/collection.rb', line 53
def new
newFrom to_a
end
|
#newFrom(data) ⇒ Object
rubocop:disable Naming/MethodName
57
58
59
|
# File 'lib/eco/language/models/collection.rb', line 57
def newFrom(data)
self.class.new(data, klass: @klass, factory: @factory)
end
|
#present(attr, flag = true) ⇒ Object
154
155
156
157
|
# File 'lib/eco/language/models/collection.rb', line 154
def present(attr, flag = true)
block = ->(o) { attr_value_present?(o, attr) == !!flag }
newFrom select(&block)
end
|
#present_all?(attr, flag = true) ⇒ Boolean
163
164
165
|
# File 'lib/eco/language/models/collection.rb', line 163
def present_all?(attr, flag = true)
present(attr, flag).length == length
end
|
#present_some?(attr, flag = true) ⇒ Boolean
167
168
169
|
# File 'lib/eco/language/models/collection.rb', line 167
def present_some?(attr, flag = true)
present(attr, flag).length.positive?
end
|
#remove(attr, value, modifier = default_modifier) ⇒ Object
105
106
107
|
# File 'lib/eco/language/models/collection.rb', line 105
def remove(attr, value, modifier = default_modifier)
self < exclude(attr, value, modifier)
end
|
49
50
51
|
# File 'lib/eco/language/models/collection.rb', line 49
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
145
146
147
148
149
150
|
# File 'lib/eco/language/models/collection.rb', line 145
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
133
134
135
|
# File 'lib/eco/language/models/collection.rb', line 133
def unique_attrs(attr)
to_h(attr).keys
end
|
#update(&block) ⇒ Object
91
92
93
|
# File 'lib/eco/language/models/collection.rb', line 91
def update(&block)
newFrom map(&block)
end
|