Class: Libis::Tools::Parameter
- Defined in:
- lib/libis/tools/parameter.rb
Overview
noinspection RubyConstantNamingConvention
Constant Summary collapse
- TRUE_BOOL =
%w'true yes t y 1'- FALSE_BOOL =
%w'false no f n 0'
Instance Attribute Summary collapse
-
#constraint ⇒ Object
Returns the value of attribute constraint.
-
#datatype ⇒ Object
Returns the value of attribute datatype.
-
#default ⇒ Object
Returns the value of attribute default.
-
#description ⇒ Object
Returns the value of attribute description.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #guess_datatype ⇒ Object
-
#initialize(*args) ⇒ Parameter
constructor
A new instance of Parameter.
- #parse(value = nil) ⇒ Object
- #to_h ⇒ Object
- #valid_value?(value) ⇒ Boolean
Methods inherited from Struct
from_json, #set, #to_hash, #to_json
Constructor Details
#initialize(*args) ⇒ Parameter
Returns a new instance of Parameter.
11 12 13 14 15 |
# File 'lib/libis/tools/parameter.rb', line 11 def initialize(*args) # noinspection RubySuperCallWithoutSuperclassInspection super(*args) self. = {} unless self. end |
Instance Attribute Details
#constraint ⇒ Object
Returns the value of attribute constraint
9 10 11 |
# File 'lib/libis/tools/parameter.rb', line 9 def constraint @constraint end |
#datatype ⇒ Object
Returns the value of attribute datatype
9 10 11 |
# File 'lib/libis/tools/parameter.rb', line 9 def datatype @datatype end |
#default ⇒ Object
Returns the value of attribute default
9 10 11 |
# File 'lib/libis/tools/parameter.rb', line 9 def default @default end |
#description ⇒ Object
Returns the value of attribute description
9 10 11 |
# File 'lib/libis/tools/parameter.rb', line 9 def description @description end |
#name ⇒ Object
Returns the value of attribute name
9 10 11 |
# File 'lib/libis/tools/parameter.rb', line 9 def name @name end |
#options ⇒ Object
Returns the value of attribute options
9 10 11 |
# File 'lib/libis/tools/parameter.rb', line 9 def end |
Class Method Details
.from_hash(h) ⇒ Object
29 30 31 |
# File 'lib/libis/tools/parameter.rb', line 29 def self.from_hash(h) h.each { |k,v| self[k.to_s.to_sym] = v } end |
Instance Method Details
#[](key) ⇒ Object
17 18 19 20 21 |
# File 'lib/libis/tools/parameter.rb', line 17 def [](key) # noinspection RubySuperCallWithoutSuperclassInspection return super(key) if members.include?(key) self[:options][key] end |
#[]=(key, value) ⇒ Object
23 24 25 26 27 |
# File 'lib/libis/tools/parameter.rb', line 23 def []=(key,value) # noinspection RubySuperCallWithoutSuperclassInspection return super(key,value) if members.include?(key) self[:options][key] = value end |
#guess_datatype ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/libis/tools/parameter.rb', line 63 def guess_datatype return send(:datatype) if send(:datatype) case send(:default) when TrueClass, FalseClass 'bool' when NilClass 'string' when Integer 'int' when Float 'float' when DateTime, Date, Time 'datetime' when Array 'array' when Hash 'hash' else send(:default).class.name.downcase end end |
#parse(value = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/libis/tools/parameter.rb', line 43 def parse(value = nil) result = if value.nil? send(:default) else dtype = guess_datatype.to_s.downcase convert(dtype, value) end check_constraint(result) result end |
#to_h ⇒ Object
33 34 35 36 37 38 |
# File 'lib/libis/tools/parameter.rb', line 33 def to_h super.inject({}) do |hash, key, value| key == :options ? value.each {|k,v| hash[k] = v} : hash[key] = value hash end end |
#valid_value?(value) ⇒ Boolean
54 55 56 57 58 59 60 61 |
# File 'lib/libis/tools/parameter.rb', line 54 def valid_value?(value) begin parse(value) rescue return false end true end |