Class: Actionizer::Inputs

Inherits:
Object
  • Object
show all
Defined in:
lib/actionizer/inputs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInputs

Returns a new instance of Inputs.



5
6
7
# File 'lib/actionizer/inputs.rb', line 5

def initialize
  @declared_params_by_method = {}
end

Instance Attribute Details

#declared_params_by_methodObject (readonly)

Returns the value of attribute declared_params_by_method.



3
4
5
# File 'lib/actionizer/inputs.rb', line 3

def declared_params_by_method
  @declared_params_by_method
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/actionizer/inputs.rb', line 3

def method
  @method
end

Instance Method Details

#add(param:, required:, opts:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/actionizer/inputs.rb', line 40

def add(param:, required:, opts:)
  if ![nil, true, false].include?(opts[:null])
    raise ArgumentError, 'Please specify either true or false for a null option'
  end

  if opts[:type] && opts[:type].class != Class
    raise ArgumentError, "Please specify a class for type: (#{opts[:type]} is not a class)"
  end

  @declared_params_by_method[method][param] = { required: required,
                                                null: false == opts[:null] ? false : true,
                                                type: opts.fetch(:type, nil) }
end

#check_for_param_error(method_name, params = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/actionizer/inputs.rb', line 9

def check_for_param_error(method_name, params = {})
  # If no inputs_for was declared, don't do any checking
  return false if !declared_params_by_method.key?(method_name)

  params.each_key do |param|
    return "Param #{param} not declared" if !declared_params_by_method.fetch(method_name).include?(param)
  end

  declared_params_by_method.fetch(method_name, {}).each_pair do |param, attrs|
    return "Param #{param} can't be nil" if !attrs.fetch(:null) && params[param].nil?

    type = attrs.fetch(:type)
    return "Param #{param} must descend from #{type}" if type && !(params[param].class <= type)

    next if !attrs.fetch(:required)

    return "Param #{param} is required for #{method_name}" if !params.include?(param)
  end

  false
end

#endObject



36
37
38
# File 'lib/actionizer/inputs.rb', line 36

def end
  @method = nil
end

#no_params_declared?(method) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/actionizer/inputs.rb', line 54

def no_params_declared?(method)
  declared_params_by_method.fetch(method, {}).empty?
end

#outside_inputs_for_block?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/actionizer/inputs.rb', line 58

def outside_inputs_for_block?
  method.nil?
end

#start(method) ⇒ Object



31
32
33
34
# File 'lib/actionizer/inputs.rb', line 31

def start(method)
  @method = method
  @declared_params_by_method[method] = {}
end