lib/eco/api/common/people/person_attribute_parser.rb in eco-helpers-1.0.13 vs lib/eco/api/common/people/person_attribute_parser.rb in eco-helpers-1.0.14

- old
+ new

@@ -24,28 +24,51 @@ # when for the current data to parse. It returns `false` otherwise. def parser_active?(source_data) @active_when.call(source_data) end + # Helper to build the `active_when` condition + def active_when_any?(*attrs) + Proc.new do |source_data| + keys = data_keys(source_data) + attrs.any? {|key| keys.include?(key)} + end + end + + # Helper to build the `active_when` condition + def active_when_all?(*attrs) + Proc.new do |source_data| + keys = data_keys(source_data) + attrs.all? {|key| keys.include?(key)} + end + end + private # by default, an attribute paraser is active if in the entry to parse # the internal attribute is present def attribute_present(active_when) Proc.new do |source_data| - case source_data - when Array - keys = source_data - when Hash - keys = source_data.keys - else - keys = [] - end - - (source_data && keys.include?(self.attr)) || + data_keys(source_data).include?(self.attr) || (active_when && active_when.call(source_data)) end end + + # Helper to obtain the current internal named attributes of the data + # @param source_data [Array<String>, Hash] if `Array` those are already the `keys`, if `Hash` it gets the `keys` + # @return [Array<String>] `keys` of `source_data` + def data_keys(source_data) + case source_data + when Array + keys = source_data + when Hash + keys = source_data.keys + else + keys = [] + end + end + + end end end end end