Sha256: 29fd70728aade4cfb16a45c197df0f51e3a5caf96c4fe939940572c979130af4

Contents?: true

Size: 1.35 KB

Versions: 8

Compression:

Stored size: 1.35 KB

Contents

Dir[File.join(File.dirname(__FILE__), 'argument_mapper', '*.rb')].each do |path|
  require path
end

module Rake
  module Funnel
    module Support
      class Mapper
        include ArgumentMapper
        include InstantiateSymbol
        instantiate Styles

        def initialize(style = :default)
          raise "You cannot use the 'nil' mapper style. Available mappers are: #{available.inspect}" if style.nil?

          @style = create(style)
        end

        def map(args = {})
          model = (args || {}).map { |switch, value|
            value = [value] unless value.is_a?(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.is_a?(Enumerable)
            pairs = value.map { |k, v|
              KeyValuePair.new(k, v)
            }

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

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rake-funnel-0.18.0 lib/rake/funnel/support/mapper.rb
rake-funnel-0.17.0 lib/rake/funnel/support/mapper.rb
rake-funnel-0.16.1 lib/rake/funnel/support/mapper.rb
rake-funnel-0.16.0 lib/rake/funnel/support/mapper.rb
rake-funnel-0.15.0.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.14.0.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.13.0.pre lib/rake/funnel/support/mapper.rb
rake-funnel-0.12.0.pre lib/rake/funnel/support/mapper.rb