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.



11
12
13
14
15
# File 'lib/libis/tools/parameter.rb', line 11

def initialize(*args)
  # noinspection RubySuperCallWithoutSuperclassInspection
  super(*args)
  self.options = {} unless self.options
end

Instance Attribute Details

#constraintObject

Returns the value of attribute constraint

Returns:

  • (Object)

    the current value of constraint



9
10
11
# File 'lib/libis/tools/parameter.rb', line 9

def constraint
  @constraint
end

#datatypeObject

Returns the value of attribute datatype

Returns:

  • (Object)

    the current value of datatype



9
10
11
# File 'lib/libis/tools/parameter.rb', line 9

def datatype
  @datatype
end

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



9
10
11
# File 'lib/libis/tools/parameter.rb', line 9

def default
  @default
end

#descriptionObject

Returns the value of attribute description

Returns:

  • (Object)

    the current value of description



9
10
11
# File 'lib/libis/tools/parameter.rb', line 9

def description
  @description
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



9
10
11
# File 'lib/libis/tools/parameter.rb', line 9

def name
  @name
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



9
10
11
# File 'lib/libis/tools/parameter.rb', line 9

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



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_hObject



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

Returns:

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