Class: Libis::Tools::Parameter

Inherits:
Struct show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#constraintObject

Returns the value of attribute constraint

Returns:

  • (Object)

    the current value of constraint



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def constraint
  @constraint
end

#datatypeObject

Returns the value of attribute datatype

Returns:

  • (Object)

    the current value of datatype



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def datatype
  @datatype
end

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def default
  @default
end

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def description
  @description
end

#frozenObject

Returns the value of attribute frozen

Returns:

  • (Object)

    the current value of frozen



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def frozen
  @frozen
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



14
15
16
# File 'lib/libis/tools/parameter.rb', line 14

def options
  @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

#dupObject



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_hObject



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

Returns:

  • (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