class Tengine::Support::Config::Definition::Field

Attributes

__block__[RW]
__name__[RW]
__parent__[RW]
__type__[RW]
convertor[RW]
default[RW]
default_description[RW]
description[RW]
enum[RW]
hidden[RW]
type[RW]

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 9
def initialize(attrs = {})
  attrs.each{|k, v| send("#{k}=", v)}
end

Public Instance Methods

accept_visitor(visitor) click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 38
def accept_visitor(visitor)
  visitor.visit(self)
end
action?() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 14
def action?; @__type__ == :action; end
convert(value, context = self) click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 59
def convert(value, context = self)
  return convertor.call(value) if convertor
  result = case self.type
  when :boolean then !!value
  when :integer then value.nil? ? nil : value.to_i
  when :string then value.nil? ? nil : value.to_s
  else value
  end
  result ||= default_value(context)
  if self.enum && !self.enum.include?(result)
    raise ArgumentError, "must be one of #{self.enum.inspect} but was #{result.inspect}"
  end
  result
end
default_value(context = __parent__) click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 30
def default_value(context = __parent__)
  default.respond_to?(:to_proc) ? context.instance_eval(&default) : default
end
description_value() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 23
def description_value
  [
    __parent__.get_value(description),
    __parent__.get_value(default_description)
  ].join(' ')
end
field?() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 13
def field?; @__type__ == :field; end
hidden?() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 17
def hidden?; !!self.hidden; end
long_opt() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 55
def long_opt
  '--' << name_array.join('-').gsub(%r{_}, '-')
end
name_array() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 42
def name_array
  (__parent__ ? __parent__.name_array : []) + [__name__]
end
root() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 46
def root
  __parent__ ? __parent__.root : nil
end
separator?() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 15
def separator?; @__type__ == :separator; end
short_opt() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 50
def short_opt
  r = root.mapping[ name_array ]
  r ? "-#{r}" : nil
end
to_hash() click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 34
def to_hash
  __parent__.send(__name__) || default_value
end
update(attrs) click to toggle source
# File lib/tengine/support/config/definition/field.rb, line 19
def update(attrs)
  attrs.each{|k, v| send("#{k}=", v)}
end