class Eco::Language::Models::Collection
Constants
- BASIC_METHODS
- EXTENDED_METHODS
Public Class Methods
attr_collection(*attrs)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
new(data = [], klass:, factory: nil, handy: Eco::Assets::Language.new)
click to toggle source
# 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
Public Instance Methods
<(value)
click to toggle source
# File lib/eco/language/models/collection.rb, line 77 def <(value) @items.clear self << value end
<<(value)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/eco/language/models/collection.rb, line 92 def delete!(value) self < @items - into_a(value) end
each(&block)
click to toggle source
# 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)
click to toggle source
# File lib/eco/language/models/collection.rb, line 152 def empty(attr, flag = true) present(attr, !flag) end
empty?()
click to toggle source
# File lib/eco/language/models/collection.rb, line 68 def empty? count == 0 end
exclude(attr, value, modifier = default_modifier)
click to toggle source
@!group `attr` dependant methods
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/eco/language/models/collection.rb, line 64 def length count end
merge(data)
click to toggle source
# 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()
click to toggle source
# File lib/eco/language/models/collection.rb, line 51 def new newFrom to_a end
newFrom(data)
click to toggle source
# 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)
click to toggle source
@!group `attr` presence methods
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/eco/language/models/collection.rb, line 102 def remove(attr, value, modifier = default_modifier) self < exclude(attr, value, modifier) end
to_c()
click to toggle source
@!group pure collection methods
# File lib/eco/language/models/collection.rb, line 47 def to_c Collection.new(self, klass: @klass, factory: @factory) end
to_h(attr, &block)
click to toggle source
By a specific `attr` or a block @note either one or the other should be present
# 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)
click to toggle source
# File lib/eco/language/models/collection.rb, line 128 def unique_attrs(attr) to_h(attr).keys end
update(&block)
click to toggle source
# File lib/eco/language/models/collection.rb, line 88 def update(&block) newFrom self.map(&block) end
Protected Instance Methods
into_a(value)
click to toggle source
# File lib/eco/language/models/collection.rb, line 171 def into_a(value) value = [].push(value) if value.is_a?(Hash) || !value.is_a?(Enumerable) value.to_a end
on_change()
click to toggle source
@!endgroup
# File lib/eco/language/models/collection.rb, line 167 def on_change # function to be overriden by children classes end
Private Instance Methods
attr_value(obj, attr)
click to toggle source
# File lib/eco/language/models/collection.rb, line 178 def attr_value(obj, attr) return nil unless obj && attr case when obj.is_a?(Hash) obj[attr] when obj.respond_to?(attr.to_sym) obj.send(attr) end end
attr_value_present?(obj, attr)
click to toggle source
# File lib/eco/language/models/collection.rb, line 188 def attr_value_present?(obj, attr) return false unless value = attr_value(obj, attr) case when value.is_a?(Enumerable) value.count > 1 when value.is_a?(String) !value.strip.empty? else !!value end end
boolean?(value)
click to toggle source
# File lib/eco/language/models/collection.rb, line 214 def boolean?(value) value == !!value end
default_modifier()
click to toggle source
# File lib/eco/language/models/collection.rb, line 210 def default_modifier Language::MatchModifier.new end
match?(*args)
click to toggle source
# File lib/eco/language/models/collection.rb, line 200 def match?(*args) @handy.match?(*args) end
to_klass(list)
click to toggle source
# File lib/eco/language/models/collection.rb, line 204 def to_klass(list) into_a(list).map do |v| v.is_a?(@klass) ? v : @factory&.new(v) || @klass.new(v) end end