Sha256: 41442dbed16977487dc3620aa1692f85eed82af2d3edd3440bb94d81fbbe1304

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 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.is_a? String) ? v : v.to_s.humanize)
          end
        when Symbol
          @results << arg.to_s.humanize
        when String
          @results << arg
        when Array
        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?
      method_arguments = args.detect{ |arg| arg.is_a? Array } || []
      
      args.each do |arg|
        case arg
        when Hash
          arg.each do |k, v|
            @results << @instance.send(sym, *method_arguments).send(k).to_s
          end
        when Symbol
          @results << @instance.send(sym, *method_arguments).send(arg).to_s
        when String
          @results << @instance.send(sym, *method_arguments).to_s
        when Array
        else
          raise "Unknown data symbol #{arg.inspect}"
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
milaniliev-comma-0.1.8 lib/comma/extractors.rb