lib/eco/api/common/people/person_attribute_parser.rb in eco-helpers-1.5.1 vs lib/eco/api/common/people/person_attribute_parser.rb in eco-helpers-1.5.2
- old
+ new
@@ -10,43 +10,61 @@
# @note
# - additionally, you can declare a callback `active:` to determine if when the
# parser will be active/used.
# - this is important to help avoiding to set values that are not present in the input entry.
# - if you have doubts about using it or not, do not use it.
- # @param active_when [Proc] that expects a list of internal attributes or the internal entry itself
+ # @param phase [Symbol] the phase when this parser should be active.
+ # Must be one of [`:internal`, `:final`]
+ # @param active_when [Proc] that expects a list of internal attributes or the internal entry itself.
# By default, an attribute paraser is active if in the entry to parse the internal attribute is present.
- def def_parser(active_when: nil, &block)
+ def def_parser(phase = :internal, active_when: nil, &block)
@active_when = attribute_present(active_when)
- super(&block)
+ super(phase, &block)
end
- # Determines if for a given source data to parse, this parser should be active or not
- # @return [Boolean] `true` if there's no callback defined or there is and evaluates `true`
- # when for the current data to parse. It returns `false` otherwise.
- def parser_active?(source_data)
- @active_when.call(source_data)
+ # @param phase [Symbol] the phase when this serializer should be active.
+ # Must be one of [`:person,` `:final`, `:internal`]
+ def def_serializer(phase = :person, &block)
+ super(phase, &block)
end
- # Helper to build the `active_when` condition
+ # Determines if for a given source data to parse, this parser should be active or not.
+ # @param phase [Symbol] the phase when this parser should be active.
+ # Must be one of [`:internal`, `:final`]
+ # @return [Boolean] `true` if there's parser defined and `source_data` activates it.
+ def parser_active?(source_data, phase = :any)
+ (phase == :any || parser_category?(phase)) && @active_when.call(source_data)
+ end
+
+ # Determines a serializer should be active or not.
+ # @param phase [Symbol] the phase when this serializer should be active.
+ # Must be one of [`:person,` `:final`, `:internal`]
+ # @return [Boolean] `true` if there's serializer defined.
+ def serializer_active?(phase = :any)
+ (phase == :any) || serializer_category?(phase)
+ 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
+ # 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
+ # 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|
data_keys(source_data).include?(self.attr) ||
(active_when && active_when.call(source_data))
@@ -64,10 +82,9 @@
keys = source_data.keys
else
keys = []
end
end
-
end
end
end
end