Sha256: 88090d1e22a8d5d8d12266586c6b820a5e0cf7c489ed8430fbb4e0691e7dd90a

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 KB

Contents

module Comma
  
  class Extractor

    def initialize(instance, &block)
      @instance = instance
      @block = block
      @results = []
    end
    
    def results
      instance_eval &@block
      @results
    end
  end
  
  class HeaderExtractor < Extractor
    
    def method_missing(sym, *args, &block)
      @results << sym.to_s.humanize if args.blank?
      
      args.each do |arg|
        case arg
        when Hash
          arg.each do |k, v|
            @results << v.to_s.humanize
          end
        when Symbol, String
          @results << arg.to_s.humanize
        else
          raise "Unknown header symbol #{arg.inspect}"
        end
      end
    end
  end
  
  class DataExtractor < Extractor
    
    def method_missing(sym, *args, &block)
      @results << @instance.send(sym).to_s if args.blank?
      
      args.each do |arg|
        case arg
        when Hash
          arg.each do |k, v|
            @results << @instance.send(sym).send(k).to_s
          end
        when Symbol
          @results << @instance.send(sym).send(arg).to_s
        when String
          @results << @instance.send(sym).to_s
        else
          raise "Unknown data symbol #{arg.inspect}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
crafterm-comma-0.1.2 lib/comma/extractors.rb