Class: Rules::Parameters::Parameter

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

Direct Known Subclasses

Attribute, Constant

Constant Summary collapse

VALID_TYPES =
[:date, :integer, :float, :boolean, :string, :regexp]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Parameter

Returns a new instance of Parameter.



27
28
29
30
31
32
33
# File 'lib/rules/parameters/parameter.rb', line 27

def initialize(options = {})
  self.key  = options[:key].to_sym
  self.name = options[:name] || options[:key].to_s.humanize
  self.type = options[:type] if options[:type]

  raise "Unknown type #{type}" if type && !VALID_TYPES.include?(type)
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.cast(value, type) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rules/parameters/parameter.rb', line 7

def self.cast(value, type)
  return value unless type
  case type
  when :date
    Date.parse(value.to_s)
  when :integer
    value.to_i
  when :float
    value.to_f
  when :boolean
    value.to_s == 'true' ? true : false
  when :string
    value.to_s
  when :regexp
    Regexp.new(value.to_s)
  else
    raise "Don't know how to cast #{type}"
  end
end

Instance Method Details

#to_sObject



35
36
37
# File 'lib/rules/parameters/parameter.rb', line 35

def to_s
  name
end