lib/eco/language/models/collection.rb in eco-helpers-2.6.4 vs lib/eco/language/models/collection.rb in eco-helpers-2.7.0
- old
+ new
@@ -1,16 +1,16 @@
+# rubocop:disable Style/OptionalBooleanParameter
module Eco
module Language
module Models
class Collection
include Enumerable
- BASIC_METHODS = ["present", "empty", "present_all?", "present_some?"]
- EXTENDED_METHODS = BASIC_METHODS + ["exclude", "remove", "attr", "attr?", "attrs", "unique_attrs", "contains"]
+ BASIC_METHODS = %w[present empty present_all? present_some?].freeze
+ EXTENDED_METHODS = BASIC_METHODS + %w[exclude remove attr attr? attrs unique_attrs contains]
class << self
-
def attr_presence(*attrs)
block = ->(method) { attrs_create_method(attrs, method) }
BASIC_METHODS.each(&block)
end
@@ -25,20 +25,19 @@
if method.include?("attr")
attr_method = method.sub("attr", attr)
else
attr_method = "#{attr}_#{method}"
end
- define_method attr_method do |*args|
+ define_method attr_method do |*args|
send(method, attr, *args)
end
end
end
-
end
def initialize(data = [], klass:, factory: nil, handy: Eco::Assets::Language.new)
- raise "Raise klass required, given: #{klass}" if !klass
+ raise "Raise klass required, given: #{klass}" unless klass
@klass = klass
@factory = factory
@handy = handy
@items = to_klass(data)
end
@@ -50,11 +49,11 @@
def new
newFrom to_a
end
- def newFrom(data)
+ def newFrom(data) # rubocop:disable Naming/MethodName
self.class.new(data, klass: @klass, factory: @factory)
end
def merge(data)
data = data.to_a unless data.is_a?(Array)
@@ -64,31 +63,31 @@
def length
count
end
def empty?
- count == 0
+ count.zero?
end
def each(&block)
return to_enum(:each) unless block
@items.each(&block)
end
- def <(value)
+ def <(other)
@items.clear
- self << value
+ self << other
end
- def <<(value)
- @items.concat(into_a(value))
+ def <<(other)
+ @items.concat(into_a(other))
on_change
self
end
def update(&block)
- newFrom self.map(&block)
+ newFrom map(&block)
end
def delete!(value)
self < @items - into_a(value)
end
@@ -105,11 +104,11 @@
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|
+ end.then do |matching|
newFrom matching
end
end
def attr?(attr, value = true, modifier = default_modifier)
@@ -143,11 +142,11 @@
end
# @!endgroup
# @!group `attr` presence methods
def present(attr, flag = true)
- block = ->(o) { attr_value_present?(o, attr) == !!flag }
+ block = ->(o) { attr_value_present?(o, attr) == !!flag } # rubocop:disable Style/DoubleNegation
newFrom select(&block)
end
def empty(attr, flag = true)
present(attr, !flag)
@@ -156,11 +155,11 @@
def present_all?(attr, flag = true)
present(attr, flag).length == length
end
def present_some?(attr, flag = true)
- present(attr, flag).length > 0
+ present(attr, flag).length.positive?
end
# @!endgroup
protected
@@ -175,27 +174,25 @@
private
def attr_value(obj, attr)
return nil unless obj && attr
- case
- when obj.is_a?(Hash)
+ if obj.is_a?(Hash)
obj[attr]
- when obj.respond_to?(attr.to_sym)
+ elsif obj.respond_to?(attr.to_sym)
obj.send(attr)
end
end
def attr_value_present?(obj, attr)
- return false unless value = attr_value(obj, attr)
- case
- when value.is_a?(Enumerable)
+ return false unless (value = attr_value(obj, attr))
+ if value.is_a?(Enumerable)
value.count > 1
- when value.is_a?(String)
+ elsif value.is_a?(String)
!value.strip.empty?
else
- !!value
+ !value.nil?
end
end
def match?(*args)
@handy.match?(*args)
@@ -212,10 +209,11 @@
end
def boolean?(value)
value == !!value
end
-
end
end
end
end
+
+# rubocop:enable Style/OptionalBooleanParameter