lib/eco/language/models/collection.rb in eco-helpers-0.6.5 vs lib/eco/language/models/collection.rb in eco-helpers-0.6.6

- old
+ new

@@ -4,10 +4,38 @@ class Collection < Array ATTR_PRESENCE_METHODS = ["present", "empty", "present_all?", "present_some?"] ATTR_COLLECTION_METHODS = ["exclude", "remove", "attr", "attr?", "attrs", "unique_attrs", "contains"] + ATTR_PRESENCE_METHODS + class << self + + def attr_collection (*attrs) + block = ->(method) { attrs_create_method(attrs, method) } + ATTR_COLLECTION_METHODS.each(&block) + end + + def attr_presence (*attrs) + block = ->(method) { attrs_create_method(attrs, method) } + ATTR_PRESENCE_METHODS.each(&block) + end + + 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 + + end + #attr_reader :kclass def initialize(data = [], klass:, factory: nil, handy: Eco::Common::Language.new) raise "Raise klass required, given: #{klass}" if !klass @klass = klass @@ -119,34 +147,9 @@ end def to_h(attr) return {} if !attr self.to_a.group_by { |object| object.method(attr).call } - end - - # class level methods - def self.attr_collection (*attrs) - block = ->(method) { attrs_create_method(attrs, method) } - ATTR_COLLECTION_METHODS.each(&block) - end - - def self.attr_presence (*attrs) - block = ->(method) { attrs_create_method(attrs, method) } - ATTR_PRESENCE_METHODS.each(&block) - end - - def self.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 protected def on_change