lib/eco/api/common/loaders/parser.rb in eco-helpers-3.0.18 vs lib/eco/api/common/loaders/parser.rb in eco-helpers-3.0.19

- old
+ new

@@ -19,10 +19,11 @@ match = input_attrs & attrs miss = attrs - match return [] if miss.empty? return attrs if match.empty? return miss if type == :all + [] end end class << self @@ -36,10 +37,11 @@ unless value msg = "You should specify the 'attribute' this parser/serializer, " msg << "#{self.class}, is linked to" return @attribute || (raise msg) end + name value @attribute = value end # Some parsers require dependencies to do their job. @@ -60,19 +62,21 @@ # @param phase [Symbol] the phase when this parser should be active. # Must be one of [`:internal`, `:final`] def parsing_phase(phase = nil) @parsing_phase ||= :internal return @parsing_phase unless phase + @parsing_phase = phase end # Define or get the `phase` that the `serializer` kicks in. # @param phase [Symbol] the phase when this serializer should be active. # Must be one of [`:person,` `:final`, `:internal`] def serializing_phase(phase = nil) @serializing_phase ||= :person return @serializing_phase unless phase + @serializing_phase = phase end # Helper to build the `active_when` condition. def active_when_any(*attrs) @@ -84,10 +88,11 @@ end # Helper to build the `active_when` condition. def active_when_all(*attrs) @active_when_attrs = RequiredAttrs.new(attribute, :all, attrs) + @active_when = proc do |source_data| keys = data_keys(source_data) attrs.all? {|key| keys.include?(key)} end end @@ -147,10 +152,14 @@ self.class.attribute end private + def options + ASSETS.cli.options + end + def _define_parser(attr_parser) if (active_when = self.class.active_when) attr_parser.def_parser( self.class.parsing_phase, active_when: active_when, @@ -164,9 +173,10 @@ end end def _define_serializer(attr_parser) return unless respond_to?(:serializer, true) + attr_parser.def_serializer( self.class.serializing_phase, &method(:serializer) ) end