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


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 45

def add_param(*args, &block)
  type_method, name, opts, block = translate_args(*args, &block)
  inbound_params << :"#{name}" if opts.delete(:inbound)
  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


29
30
31
32
33
34
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 29

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


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

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


97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 97

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


70
71
72
73
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 70

def hash_filter
  # the :duck method is added in lib/hyper-operation.rb globally
  @hash_filter ||= Mutations::HashFilter.new
end

.inbound_paramsObject


75
76
77
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 75

def inbound_params
  @inbound_params ||= Set.new
end

.process_params(operation, args) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 36

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


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 79

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
17
18
19
20
21
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 14

def to_h
  inputs = @inputs
  if @locked
    inputs = inputs.dup
    self.class.inbound_params.each { |name| inputs.delete :"#{name}" }
  end
  inputs.with_indifferent_access
end

#to_sObject


23
24
25
# File 'lib/hyper-operation/railway/params_wrapper.rb', line 23

def to_s
  to_h.to_s
end