Sha256: 95ff35b50e49faa395fea7b18a88d959deae5cb01add0a17b233159ba07273ee

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module Biggs
  class Extractor
    def initialize(options)
      @value_methods = Biggs::Formatter::FIELDS.reduce({}) do |methods, field|
        methods[field] = options[field] if options[field] || options[field] == false
        methods
      end
    end

    def get_values(instance)
      Biggs::Formatter::FIELDS_WO_COUNTRY.inject({}) do |values, field|
        values[field] = get_value(instance, field)
        values
      end
    end

    def get_value(instance, field)
      return if @value_methods[field] == false

      key = @value_methods[field] || field

      case key
      when Symbol
        instance.send(key.to_sym)
      when Proc
        key.call(instance).to_s
      when Array
        if key.all?{|it| it.is_a?(Symbol) }
          key.map{|method| instance.send(method) }.reject(&:blank?).join("\n")
        else
          raise "Biggs: Can't handle #{field} type Array with non-symbolic entries"
        end
      else
        raise "Biggs: Can't handle #{field} type #{key.class}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
biggs-0.7.0 lib/biggs/extractor.rb
biggs-0.6.0 lib/biggs/extractor.rb