Class: Comma::Extractor

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

Direct Known Subclasses

DataExtractor, HeaderExtractor

Instance Method Summary collapse

Constructor Details

#initialize(instance, style, formats) ⇒ Extractor

Returns a new instance of Extractor.



7
8
9
10
11
12
13
# File 'lib/comma/extractor.rb', line 7

def initialize(instance, style, formats)
  @instance = instance
  @style = style
  @formats = formats
  @results = []
  @style_stack = [style]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/comma/extractor.rb', line 40

def method_missing(sym, *args, &block)
  @results << extract_column(sym, &block) if args.empty?

  args.each do |arg|
    case arg
    when Hash
      arg.each { |k, v| @results << extract_column(k, association: sym, label: v, &block) }
    when Symbol
      @results << extract_column(arg, association: sym, label: arg, &block)
    when String
      @results << extract_column(sym, label: arg, &block)
    else
      raise "Unknown #{column_kind} symbol #{arg.inspect}"
    end
  end
end

Instance Method Details

#__use__(style) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/comma/extractor.rb', line 24

def __use__(style)
  if @style_stack.include?(style)
    chain = (@style_stack + [style]).join(' -> ')
    raise Comma::CircularStyleReference, "Circular __use__ reference detected: #{chain}"
  end

  format = @formats.fetch(style) { raise "No comma format defined for style #{style}" }

  @style_stack.push(style)
  begin
    instance_eval(&format)
  ensure
    @style_stack.pop
  end
end

#id(*args, &block) ⇒ Object



20
21
22
# File 'lib/comma/extractor.rb', line 20

def id(*args, &block)
  method_missing(:id, *args, &block)
end

#results ⇒ Object



15
16
17
18
# File 'lib/comma/extractor.rb', line 15

def results
  instance_eval(&@formats[@style])
  @results.map { |r| convert_to_data_value(r) }
end