Sha256: 8bcd4a3143ffbc4a272eab4148e15015f6c68f526167bf71b2af50c36f4d6d38

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Comma

  class Extractor

    def initialize(instance, &block)
      @instance = instance
      @block = block
      @results = []
    end

    def results
      instance_eval &@block
      @results
    end

    def id
      method_missing(:id)
    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
        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).nil? ? '' : @instance.send(sym).send(k).to_s )
          end
        when Symbol
            @results << ( @instance.send(sym).nil? ? '' : @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

3 entries across 3 versions & 3 rubygems

Version Path
StephanZ-comma-0.1.8 lib/comma/extractors.rb
crafterm-comma-0.1.8 lib/comma/extractors.rb
rolando-comma-0.1.10 lib/comma/extractors.rb