Class: Hyperloop::Operation::ParamsWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/hyper-operation/railway/params_wrapper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs) ⇒ ParamsWrapper

Returns a new instance of ParamsWrapper.



5
6
7
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 5

def initialize(inputs)
  @inputs = inputs
end

Class Method Details

.add_param(*args, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 40

def add_param(*args, &block)
  type_method, name, opts, block = translate_args(*args, &block)
  if opts.key? :default
    hash_filter.optional { send(type_method, name, opts, &block) }
  else
    hash_filter.required { send(type_method, name, opts, &block) }
  end

  # don't forget specially handling for procs
  define_method(name) do
    @inputs[name]
  end
  define_method(:"#{name}=") do |x|
    method_missing(:"#{name}=", x) if @locked
    @inputs[name] = x
  end
end

.combine_arg_array(args) ⇒ Object



24
25
26
27
28
29
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 24

def combine_arg_array(args)
  hash = args.inject({}.with_indifferent_access) do |h, arg|
    raise ArgumentError.new("All arguments must be hashes") unless arg.respond_to? :to_h
    h.merge!(arg.to_h)
  end
end

.dispatch_params(params, hashes = {}) ⇒ Object



58
59
60
61
62
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 58

def dispatch_params(params, hashes = {})
  params = params.dup
  hashes.each { |hash| hash.each { |k, v| params.send(:"#{k}=", v) } }
  params.lock
end

.get_name_and_opts(*args) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 86

def get_name_and_opts(*args)
  if args[0].is_a? Hash
    opts = args[0]
    name = opts.first.first
    opts[:default] = opts.first.last
    opts.delete(name)
  else
    name = args[0]
    opts = args[1] || {}
  end
  [name, opts]
end

.hash_filterObject



64
65
66
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 64

def hash_filter
  @hash_filter ||= Mutations::HashFilter.new
end

.process_params(operation, args) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 31

def process_params(operation, args)
  raw_inputs = combine_arg_array(args)
  inputs, errors = hash_filter.filter(raw_inputs)
  params_wrapper = new(inputs)
  operation.instance_eval do
    @raw_inputs, @params, @errors = [raw_inputs, params_wrapper, errors]
  end
end

.translate_args(*args, &block) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 68

def translate_args(*args, &block)
  name, opts = get_name_and_opts(*args)
  if opts.key?(:type)
    type_method = opts.delete(:type)
    if type_method.is_a?(Array)
      opts[:class] = type_method.first if type_method.count > 0
      type_method = Array
    elsif type_method.is_a?(Hash) || type_method == Hash
      type_method = Hash
      block ||= proc { duck :* }
    end
    type_method = type_method.to_s.underscore
  else
    type_method = :duck
  end
  [type_method, name, opts, block || proc {}]
end

Instance Method Details

#lockObject



9
10
11
12
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 9

def lock
  @locked = true
  self
end

#to_hObject



14
15
16
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 14

def to_h
  @inputs.with_indifferent_access
end

#to_sObject



18
19
20
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 18

def to_s
  to_h.to_s
end