Sha256: 600b3fd1395f7e54b770321d54d9f8bda2d5f39efed85edbba5594d7d535b7c2

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 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(*args)
      method_missing(:id, *args)
    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)
      if args.blank?
        result = block ? yield(@instance.send(sym)) : @instance.send(sym)
        @results << result.to_s
      end

      args.each do |arg|
        case arg
        when Hash
          arg.each do |k, v|
            if block
              @results << (@instance.send(sym).nil? ? '' : yield(@instance.send(sym).send(k)).to_s )
            else
              @results << (@instance.send(sym).nil? ? '' : @instance.send(sym).send(k).to_s )
            end
          end
        when Symbol
          if block
            @results << (@instance.send(sym).nil? ? '' : yield(@instance.send(sym).send(arg)).to_s)
          else
            @results << ( @instance.send(sym).nil? ? '' : @instance.send(sym).send(arg).to_s )
          end
        when String
          @results << (block ? yield(@instance.send(sym)) : @instance.send(sym)).to_s
        else
          raise "Unknown data symbol #{arg.inspect}"
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
comma-2.1.3 lib/comma/extractors.rb
comma-2.1.2 lib/comma/extractors.rb
comma-3.0.4 lib/comma/extractors.rb
comma-2.1.1 lib/comma/extractors.rb
comma-2.1 lib/comma/extractors.rb
comma-3.0.3 lib/comma/extractors.rb
comma-3.0.2 lib/comma/extractors.rb
comma-3.0.1 lib/comma/extractors.rb
comma-3.0.0 lib/comma/extractors.rb
comma-2.0 lib/comma/extractors.rb
comma-0.5.2 lib/comma/extractors.rb
comma-0.4.3 lib/comma/extractors.rb