Sha256: c6f0c761a674197afcc6dfff00d3c48fa8eb5d0bb259a9e13c47ed8336f5b811

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

class JsonParser::Builder < Sinclair

  attr_reader :attr_names

  def initialize(attr_names, clazz, options)
    super(clazz, {
      after:     false,
      cached:    false,
      case:      :lower_camel,
      class:     nil,
      compact:   false,
      default:   nil,
      flatten:   false,
      full_path: nil,
      json:      :json,
      path:      nil,
      type:      :none
    }.merge(options.symbolize_keys))

    @attr_names = attr_names
    init
  end

  private

  delegate :path, :full_path, :cached, :compact,
           :type, :after, to: :options_object

  def init
    attr_names.each do |attr|
      add_attr(attr)
    end
  end

  def json_name
    options[:json]
  end

  def real_path(attribute)
    full_path || [path, attribute].compact.join('.')
  end

  def wrapper_clazz
    options[:class]
  end

  def case_type
    options[:case]
  end

  def fetcher_options
    options.slice(:compact, :after, :type, :flatten, :default).merge({
      clazz: wrapper_clazz,
      case_type: case_type
    })
  end

  def add_attr(attribute)
    add_method attribute, "#{cached ? cached_fetcher(attribute) : attr_fetcher(attribute)}"
  end

  def attr_fetcher(attribute)
    <<-CODE
      ::JsonParser::Fetcher.new(
        #{json_name}, '#{real_path(attribute)}', self, #{fetcher_options}
      ).fetch
    CODE
  end

  def cached_fetcher(attribute)
    <<-CODE
      @#{attribute} ||= #{attr_fetcher(attribute)}
    CODE
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
json_parser-1.3.0 lib/json_parser/builder.rb