Class: Biggs::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/biggs/extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Extractor

Returns a new instance of Extractor.



3
4
5
6
7
8
# File 'lib/biggs/extractor.rb', line 3

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

Instance Method Details

#get_value(instance, field) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/biggs/extractor.rb', line 17

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

#get_values(instance) ⇒ Object



10
11
12
13
14
15
# File 'lib/biggs/extractor.rb', line 10

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