Sha256: 60abdd3f518280e0cd7ecbc4d52f27a87e94e6e72ce3968210a474e6320b309c

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

module AttributesMapper
  class Builder
    include HasAttributes

    attr_reader :input

    def initialize(input)
      transformed_input = if configuration.apply_input_data_transform?
                            configuration.apply_input_data_transform_proc.call(input)
                          else
                            input
                          end

      @input = Rordash::HashUtil.deep_symbolize_keys(transformed_input)
      @mapped_input = {}
    end

    def build
      @mapped_input = paths_builder.build_for(input).deep_symbolize_keys
      self
    end

    def to_h
      @mapped_input
    end

    def paths_builder
      @paths_builder ||= begin
        builder = JsonPath::Builder.new
        if configuration.source_data_wrapper_class?
          builder.with_wrapped_data_class(configuration.source_data_wrapper_class)
        end
        add_paths_to_builder(builder)
        builder
      end
    end

    private

    def add_paths_to_builder(builder)
      self.class.each_path_context_key_value_pair do |to, path_attrs|
        params = path_attrs.merge(to: to)

        if params.key?(SCOPE)
          scope_path = self.class.find_path_by_scope_name(params[SCOPE])
          builder.within(scope_path) { add_path_to_builder(builder, params) }
        else
          add_path_to_builder(builder, params)
        end
      end
    end

    def add_path_to_builder(builder, path_params)
      builder.from(path_params[:from], **path_params.except(:from, SCOPE))
    end

    def method_missing(method_name, *arguments, &block)
      return @mapped_input[method_name] if @mapped_input.key?(method_name)

      super
    end

    def respond_to_missing?(method_name, include_private = false)
      @mapped_input.key?(method_name) || super
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
attributes-mapper-0.1.4 lib/attributes-mapper/builder.rb
attributes-mapper-0.1.2 lib/attributes-mapper/builder.rb
attributes-mapper-0.1.1 lib/attributes-mapper/builder.rb