Class: Rules::Parameters::Parameter
- Inherits:
-
Object
- Object
- Rules::Parameters::Parameter
- Defined in:
- lib/rules/parameters/parameter.rb
Constant Summary collapse
- VALID_TYPES =
[:date, :integer, :float, :boolean, :string, :regexp]
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Parameter
constructor
A new instance of Parameter.
- #to_s ⇒ Object
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( = {}) self.key = [:key].to_sym self.name = [:name] || [:key].to_s.humanize self.type = [:type] if [:type] raise "Unknown type #{type}" if type && !VALID_TYPES.include?(type) end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
5 6 7 |
# File 'lib/rules/parameters/parameter.rb', line 5 def key @key end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/rules/parameters/parameter.rb', line 5 def name @name end |
#type ⇒ Object
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_s ⇒ Object
35 36 37 |
# File 'lib/rules/parameters/parameter.rb', line 35 def to_s name end |