Class: Gum::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/gum/factory.rb

Class Method Summary collapse

Class Method Details

.args_to_mapping(args) ⇒ Object



11
12
13
14
15
# File 'lib/gum/factory.rb', line 11

def self.args_to_mapping(args)
  args.inject({}) do |mapping, arg|
    mapping.update normalize_mapping(arg)
  end
end

.build(type, args) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/gum/factory.rb', line 3

def self.build(type, args)
  mapping = args_to_mapping(args)
  options = mapping.delete(:options)
  mapping.each do |param, field|
    yield new(type, param, field, options)
  end
end

.new(type, param, field, options) ⇒ Object



28
29
30
31
# File 'lib/gum/factory.rb', line 28

def self.new(type, param, field, options)
  filter = type.is_a?(Class) ? type : Filters.const_get(type.to_s.camelize, false)
  filter.new param, field, options
end

.normalize_mapping(mapping) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/gum/factory.rb', line 17

def self.normalize_mapping(mapping)
  case mapping
    when Hash
      mapping
    when Array
      mapping.zip(mapping).to_h
    else
      { mapping => mapping }
  end
end