Class: RailsSimpleParams::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_simple_params/parameter.rb

Constant Summary collapse

TIME_TYPES =
[Date, DateTime, Time].freeze
STRING_OR_TIME_TYPES =
([String] + TIME_TYPES).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, value:, options: {}, type: nil) ⇒ Parameter

Returns a new instance of Parameter.



10
11
12
13
14
15
# File 'lib/rails_simple_params/parameter.rb', line 10

def initialize(name:, value:, options: {}, type: nil)
  @name = name
  @value = value
  @options = options
  @type = type
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/rails_simple_params/parameter.rb', line 5

def name
  @name
end

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/rails_simple_params/parameter.rb', line 5

def options
  @options
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/rails_simple_params/parameter.rb', line 5

def type
  @type
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/rails_simple_params/parameter.rb', line 5

def value
  @value
end

Instance Method Details

#set_defaultObject



21
22
23
# File 'lib/rails_simple_params/parameter.rb', line 21

def set_default
  self.value = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
end

#should_set_default?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/rails_simple_params/parameter.rb', line 17

def should_set_default?
  value.nil? && check_param_presence?(options[:default])
end

#transformObject



25
26
27
# File 'lib/rails_simple_params/parameter.rb', line 25

def transform
  self.value = options[:transform].to_proc.call(value)
end

#validateObject



29
30
31
# File 'lib/rails_simple_params/parameter.rb', line 29

def validate
  Validator.new(self).validate!
end