class ModulePlus::ConfigDef

Attributes

desc[R]
key[R]
path[R]
sample[R]
type[R]

Public Class Methods

new(key, path, attributes = {}) click to toggle source
# File lib/module_plus/config_convention.rb, line 133
def initialize(key, path, attributes = {})
  @key  = key.to_s
  @path = path
  @desc = attributes[:desc] || ""
  @type = _type(attributes[:type])
  @type_strict = _type_strict(@type)
  @sample = attributes[:sample] || _sample(@type)
end

Public Instance Methods

field() click to toggle source
# File lib/module_plus/config_convention.rb, line 142
def field
  "#{@path.join(".")}.#{@key}"
end
value(v) click to toggle source
# File lib/module_plus/config_convention.rb, line 146
def value(v)
  raise new NotPermitedValueType.new("#{path}.#{@key}: #{v} is NOT Matched Type for #{@type}") unless @type_strict.call(v)
  v
end

Private Instance Methods

_sample(type) click to toggle source
# File lib/module_plus/config_convention.rb, line 156
def _sample(type)
  case type
  when :string       then; 'string'
  when :int          then; 0
  when :float        then; 0.1
  when :boolean      then; true
  when :string_array then; ['string']
  when :int_array    then; [0]
  when :float_array  then; [0.1]
  else
    nil
  end
end
_type(t) click to toggle source
# File lib/module_plus/config_convention.rb, line 152
def _type(t)
  (t && t.downcase.to_sym) || :string
end
_type_strict(t) click to toggle source
# File lib/module_plus/config_convention.rb, line 169
def _type_strict(t)
  case t
  when :string       then; -> (v){ v.is_a? String  }
  when :int          then; -> (v){ v.is_a? Numeric }
  when :float        then; -> (v){ v.is_a? Numeric }
  when :boolean      then; -> (v){ v.is_a? TrueClass or v.is_a? FalseClass }
  when :string_array then; -> (v){ v.is_a? Array and v.all?{ |s| s.is_a? String } }
  when :int_array    then; -> (v){ v.is_a? Array and v.all?{ |s| s.is_a? Numeric } }
  when :float_array  then; -> (v){ v.is_a? Array and v.all?{ |s| s.is_a? Numeric } }
  else
    raise NotPermitedValueType.new( "Type #{t} of Config Key:#{@key} - #{desc} in #{path}")
  end
end