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 =
["present", "empty", "present_all?", "present_some?"]
- EXTENDED_METHODS =
BASIC_METHODS + ["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.
38
39
40
41
42
43
44
|
# File 'lib/eco/language/models/collection.rb', line 38
def initialize(data = [], klass:, factory: nil, handy: Eco::Assets::Language.new)
raise "Raise klass required, given: #{klass}" if !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
#<(value) ⇒ Object
77
78
79
80
|
# File 'lib/eco/language/models/collection.rb', line 77
def <(value)
@items.clear
self << value
end
|
#<<(value) ⇒ Object
82
83
84
85
86
|
# File 'lib/eco/language/models/collection.rb', line 82
def <<(value)
@items.concat(into_a(value))
on_change
self
end
|
#attr(attr, value = true, modifier = default_modifier) ⇒ Object
106
107
108
109
110
111
112
113
|
# File 'lib/eco/language/models/collection.rb', line 106
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.yield_self do |matching|
newFrom matching
end
end
|
#attr?(attr, value = true, modifier = default_modifier) ⇒ Boolean
115
116
117
118
|
# File 'lib/eco/language/models/collection.rb', line 115
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
124
125
126
|
# File 'lib/eco/language/models/collection.rb', line 124
def attrs(attr)
map { |object| attr_value(object, attr) }
end
|
#contains(attr, value, modifier = default_modifier) ⇒ Object
120
121
122
|
# File 'lib/eco/language/models/collection.rb', line 120
def contains(attr, value, modifier = default_modifier)
self.attr(attr, value, modifier.new.pattern)
end
|
#delete!(value) ⇒ Object
92
93
94
|
# File 'lib/eco/language/models/collection.rb', line 92
def delete!(value)
self < @items - into_a(value)
end
|
#each(&block) ⇒ Object
72
73
74
75
|
# File 'lib/eco/language/models/collection.rb', line 72
def each(&block)
return to_enum(:each) unless block
@items.each(&block)
end
|
#empty(attr, flag = true) ⇒ Object
152
153
154
|
# File 'lib/eco/language/models/collection.rb', line 152
def empty(attr, flag = true)
present(attr, !flag)
end
|
#empty? ⇒ Boolean
68
69
70
|
# File 'lib/eco/language/models/collection.rb', line 68
def empty?
count == 0
end
|
#exclude(attr, value, modifier = default_modifier) ⇒ Object
98
99
100
|
# File 'lib/eco/language/models/collection.rb', line 98
def exclude(attr, value, modifier = default_modifier)
newFrom @items - self.attr(attr, value, modifier)
end
|
#group_by(attr = nil, &block) ⇒ Object
132
133
134
135
|
# File 'lib/eco/language/models/collection.rb', line 132
def group_by(attr = nil, &block)
return to_h(attr) if attr
to_a.group_by(&block) if block
end
|
#length ⇒ Object
64
65
66
|
# File 'lib/eco/language/models/collection.rb', line 64
def length
count
end
|
#merge(data) ⇒ Object
59
60
61
62
|
# File 'lib/eco/language/models/collection.rb', line 59
def merge(data)
data = data.to_a unless data.is_a?(Array)
newFrom to_a + data
end
|
#new ⇒ Object
51
52
53
|
# File 'lib/eco/language/models/collection.rb', line 51
def new
newFrom to_a
end
|
#newFrom(data) ⇒ Object
55
56
57
|
# File 'lib/eco/language/models/collection.rb', line 55
def newFrom(data)
self.class.new(data, klass: @klass, factory: @factory)
end
|
#present(attr, flag = true) ⇒ Object
147
148
149
150
|
# File 'lib/eco/language/models/collection.rb', line 147
def present(attr, flag = true)
block = ->(o) { attr_value_present?(o, attr) == !!flag }
newFrom select(&block)
end
|
#present_all?(attr, flag = true) ⇒ Boolean
156
157
158
|
# File 'lib/eco/language/models/collection.rb', line 156
def present_all?(attr, flag = true)
present(attr, flag).length == length
end
|
#present_some?(attr, flag = true) ⇒ Boolean
160
161
162
|
# File 'lib/eco/language/models/collection.rb', line 160
def present_some?(attr, flag = true)
present(attr, flag).length > 0
end
|
#remove(attr, value, modifier = default_modifier) ⇒ Object
102
103
104
|
# File 'lib/eco/language/models/collection.rb', line 102
def remove(attr, value, modifier = default_modifier)
self < exclude(attr, value, modifier)
end
|
#to_c ⇒ Object
47
48
49
|
# File 'lib/eco/language/models/collection.rb', line 47
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
139
140
141
142
143
|
# File 'lib/eco/language/models/collection.rb', line 139
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
128
129
130
|
# File 'lib/eco/language/models/collection.rb', line 128
def unique_attrs(attr)
to_h(attr).keys
end
|
#update(&block) ⇒ Object
88
89
90
|
# File 'lib/eco/language/models/collection.rb', line 88
def update(&block)
newFrom self.map(&block)
end
|