Sha256: 07c6e3292c6ed8bd71f72a048bf7d7f8f3832300f011d408ac1673f56b64ed13

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

Dir["#{File.dirname(__FILE__)}/mapper_styles/*.rb"].each do |path|
  require path
end

module Rake::Funnel::Support
  class Switch
    attr_reader :switch, :values

    def initialize(switch, values)
      @values = values
      @switch = switch
    end
  end

  class Value
    attr_reader :key, :value

    def initialize(value)
      @key = nil
      @value = value
    end
  end

  class KeyValuePair
    attr_reader :key, :value

    def initialize(key, value)
      @key = key
      @value = value
    end
  end

  class Mapper
    def initialize(style = :Default)
      raise "You cannot use a 'nil' mapper. Available mappers are: #{MapperStyles.constants.sort.join(', ')}" if style.nil?

      @style = style
      begin
        @style = MapperStyles.const_get(style).new if style.kind_of?(Symbol)
      rescue => ex
        raise "Something went wrong while creating the '#{style}' mapper: #{ex}"
      end
    end

    def map(args = {})
      model = (args || {}).map { |switch, value|
        value = [value] unless value.kind_of?(Array)

        values = value.map do |val|
          get_values(val)
        end

        Switch.new(switch, values)
      }.flatten

      @style
        .generate_from(model)
        .map { |args| args.map { |arg| camel_case_symbols(arg) }}
        .map(&:join)
    end

    private
    def get_values(value)
      if value.kind_of?(Enumerable)
        pairs = value.map { |k, v|
          KeyValuePair.new(k, v)
        }

        return Array.new(pairs) if value.kind_of?(Array)
        pairs
      else
        Value.new(value)
      end
    end

    def camel_case_symbols(value)
      return value unless value.kind_of?(Symbol)
      value.camelize
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rake-funnel-0.0.6.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.0.5.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.0.4.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.0.3.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.0.2.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.0.1.pre lib/rake/funnel/support/mapper.rb