Sha256: 0f4a5d6a41d44789d645f9cd2297791bd80f9f0b50925d6ea3005edc767ad726

Contents?: true

Size: 935 Bytes

Versions: 5

Compression:

Stored size: 935 Bytes

Contents

module Format
  class Modifier
    def initialize(yml, helper)
      @debug = yml.fetch(:debug, false)
      @helper = helper
    end

    def apply(raw_value, modifiers)
      [modifiers].flatten.compact.reduce(raw_value) do |value, modifier|
        method_name, params = modifier_props(modifier).values_at(:name, :params)

        modify_value(value, method_name, params)
      end
    end

    private

    attr_reader :helper, :debug

    def modifier_props(modifier)
      modifier.is_a?(String) ? { name: modifier } : modifier
    end

    def modify_value(value, method, params)
      args = [value]
      if params.present?
        args = params.is_a?(Array) ? [value, *params] : [value, **params]
      end

      value.try(method, *params) || helper.send(method, *args)
    rescue StandardError => error
      raise error unless debug

      "Error invoking '#{method}' with (#{args.join(',')}): #{error}"
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
xml_data_extractor-0.5.0 lib/src/format/modifier.rb
xml_data_extractor-0.4.0 lib/src/format/modifier.rb
xml_data_extractor-0.3.0 lib/src/format/modifier.rb
xml_data_extractor-0.2.0 lib/src/format/modifier.rb
xml_data_extractor-0.1.0 lib/src/format/modifier.rb