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.
-
#frozen ⇒ Object
Returns the value of attribute frozen.
-
#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
- #dup ⇒ Object
-
#initialize(*args) ⇒ Parameter
constructor
A new instance of Parameter.
- #merge!(other) ⇒ Object
- #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.
16 17 18 19 20 21 |
# File 'lib/libis/tools/parameter.rb', line 16 def initialize(*args) # noinspection RubySuperCallWithoutSuperclassInspection super(*args) self[:options] ||= {} self[:datatype] ||= guess_datatype end |
Instance Attribute Details
#constraint ⇒ Object
Returns the value of attribute constraint
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def constraint @constraint end |
#datatype ⇒ Object
Returns the value of attribute datatype
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def datatype @datatype end |
#default ⇒ Object
Returns the value of attribute default
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def default @default end |
#description ⇒ Object
Returns the value of attribute description
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def description @description end |
#frozen ⇒ Object
Returns the value of attribute frozen
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def frozen @frozen end |
#name ⇒ Object
Returns the value of attribute name
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def name @name end |
#options ⇒ Object
Returns the value of attribute options
14 15 16 |
# File 'lib/libis/tools/parameter.rb', line 14 def @options end |
Class Method Details
.from_hash(h) ⇒ Object
54 55 56 |
# File 'lib/libis/tools/parameter.rb', line 54 def self.from_hash(h) h.each { |k, v| self[k.to_s.to_sym] = v } end |
Instance Method Details
#[](key) ⇒ Object
42 43 44 45 46 |
# File 'lib/libis/tools/parameter.rb', line 42 def [](key) # noinspection RubySuperCallWithoutSuperclassInspection return super(key) if members.include?(key) self[:options][key] end |
#[]=(key, value) ⇒ Object
48 49 50 51 52 |
# File 'lib/libis/tools/parameter.rb', line 48 def []=(key, value) # noinspection RubySuperCallWithoutSuperclassInspection return super(key, value) if members.include?(key) self[:options][key] = value end |
#dup ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/libis/tools/parameter.rb', line 23 def dup # noinspection RubySuperCallWithoutSuperclassInspection new_obj = super # noinspection RubyResolve new_obj[:options] = Marshal.load(Marshal.dump(self[:options])) new_obj end |
#merge!(other) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/libis/tools/parameter.rb', line 31 def merge!(other) other.each do |k,v| if k == :options self[:options].merge!(v) else self[k] = v end end self end |
#parse(value = nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/libis/tools/parameter.rb', line 69 def parse(value = nil) result = value.nil? ? self[:default] : convert(value) check_constraint(result) result end |
#to_h ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/libis/tools/parameter.rb', line 58 def to_h # noinspection RubySuperCallWithoutSuperclassInspection 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
75 76 77 78 79 80 81 82 |
# File 'lib/libis/tools/parameter.rb', line 75 def valid_value?(value) begin parse(value) rescue return false end true end |