Sha256: 34df9b639698bcb57ff61d9569bd0fb64ef131529d959b9cbf9706d55e6b0951

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

class JsonParser::Builder < Sinclair

  attr_reader :attr_names, :path, :full_path, :cached

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

    @attr_names = attr_names
    @path = path
    @full_path = full_path
    @cached = cached
    init
  end

  private

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

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

  def json_name
    options[:json]
  end

  def wrapper_clazz
    options[:class]
  end

  def case_type
    options[:case]
  end

  def fetcher_options(attribute)
    options.slice(:compact, :after, :type, :flatten, :default).merge({
      clazz: wrapper_clazz,
      case_type: case_type,
      path: real_path(attribute)
    })
  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}, self, #{fetcher_options(attribute)}
      ).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.1 lib/json_parser/builder.rb