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



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/actionizer/inputs.rb', line 53

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: opts[:null] != false,
                                                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
30
31
32
33
34
35
36
37
38
39
40
41
42
# 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
  if !declared_params_by_method.key?(method_name)
    return false
  end

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

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

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

    if !params.include?(param)
      next
    end

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

  false
end

#endObject



49
50
51
# File 'lib/actionizer/inputs.rb', line 49

def end
  @method = nil
end

#no_params_declared?(method) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/actionizer/inputs.rb', line 67

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

#outside_inputs_for_block?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/actionizer/inputs.rb', line 71

def outside_inputs_for_block?
  method.nil?
end

#start(method) ⇒ Object



44
45
46
47
# File 'lib/actionizer/inputs.rb', line 44

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