Class: RailsParam::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_param/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.



8
9
10
11
12
13
# File 'lib/rails_param/parameter.rb', line 8

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.



3
4
5
# File 'lib/rails_param/parameter.rb', line 3

def name
  @name
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/rails_param/parameter.rb', line 3

def options
  @options
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/rails_param/parameter.rb', line 3

def type
  @type
end

#valueObject

Returns the value of attribute value.



3
4
5
# File 'lib/rails_param/parameter.rb', line 3

def value
  @value
end

Instance Method Details

#set_defaultObject



19
20
21
# File 'lib/rails_param/parameter.rb', line 19

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

#should_set_default?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/rails_param/parameter.rb', line 15

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

#transformObject



23
24
25
# File 'lib/rails_param/parameter.rb', line 23

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

#validateObject



27
28
29
# File 'lib/rails_param/parameter.rb', line 27

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