Class: VCDry::Config
- Inherits:
-
Object
- Object
- VCDry::Config
- Defined in:
- lib/vcdry/config.rb
Constant Summary collapse
- NOT_DEFINED =
Object.new.freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #array? ⇒ Boolean
- #default ⇒ Object
- #default? ⇒ Boolean
- #dup ⇒ Object
- #enum? ⇒ Boolean
- #enum_values ⇒ Object
-
#initialize(name, type = nil, **options) ⇒ Config
constructor
A new instance of Config.
- #instance_variable ⇒ Object
- #optional? ⇒ Boolean
- #required? ⇒ Boolean
- #type_cast(value) ⇒ Object
Constructor Details
#initialize(name, type = nil, **options) ⇒ Config
Returns a new instance of Config.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/vcdry/config.rb', line 12 def initialize(name, type = nil, **) @name = name.to_sym @type = (type.nil? || type.respond_to?(:call)) ? type : VCDry::Types[type] @options = # Translate default: nil to optional: true if @options.key?(:default) && @options[:default].nil? @options.delete(:default) @options[:optional] = true end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/vcdry/config.rb', line 10 def name @name end |
Instance Method Details
#array? ⇒ Boolean
24 25 26 |
# File 'lib/vcdry/config.rb', line 24 def array? !!@options[:array] end |
#default ⇒ Object
46 47 48 49 50 |
# File 'lib/vcdry/config.rb', line 46 def default value = @options.fetch(:default, NOT_DEFINED) value = value.call if value.respond_to?(:call) value end |
#default? ⇒ Boolean
28 29 30 |
# File 'lib/vcdry/config.rb', line 28 def default? @options.key?(:default) end |
#dup ⇒ Object
32 33 34 |
# File 'lib/vcdry/config.rb', line 32 def dup self.class.new(@name, @type, **@options.dup) end |
#enum? ⇒ Boolean
36 37 38 |
# File 'lib/vcdry/config.rb', line 36 def enum? @options.key?(:values) end |
#enum_values ⇒ Object
40 41 42 43 44 |
# File 'lib/vcdry/config.rb', line 40 def enum_values @enum_values ||= Array(@options[:values]).map do |value| @type.nil? ? value : @type.call(value) end end |
#instance_variable ⇒ Object
52 53 54 |
# File 'lib/vcdry/config.rb', line 52 def instance_variable "@#{name}" end |
#optional? ⇒ Boolean
56 57 58 |
# File 'lib/vcdry/config.rb', line 56 def optional? default? || @options[:optional] end |
#required? ⇒ Boolean
60 61 62 |
# File 'lib/vcdry/config.rb', line 60 def required? !optional? end |
#type_cast(value) ⇒ Object
64 65 66 |
# File 'lib/vcdry/config.rb', line 64 def type_cast(value) array? ? type_cast_array(value) : type_cast_value(value) end |