Class: KDL::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/kdl/value.rb

Direct Known Subclasses

KDL::V1::Value, Boolean, Custom, Float, Int, NullImpl, String

Defined Under Namespace

Classes: Boolean, Custom, Float, Int, NullImpl, String

Constant Summary collapse

Null =
NullImpl.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, format: nil, type: nil) ⇒ Value

Returns a new instance of Value.



7
8
9
10
11
# File 'lib/kdl/value.rb', line 7

def initialize(value, format: nil, type: nil)
  @value = value
  @format = format
  @type = type
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



60
61
62
# File 'lib/kdl/value.rb', line 60

def method_missing(name, *args, **kwargs, &block)
  value.public_send(name, *args, **kwargs, &block)
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



5
6
7
# File 'lib/kdl/value.rb', line 5

def format
  @format
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/kdl/value.rb', line 5

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/kdl/value.rb', line 5

def value
  @value
end

Class Method Details

.from(value) ⇒ Object



163
164
165
166
167
168
169
170
171
172
# File 'lib/kdl/value.rb', line 163

def self.from(value)
  case value
  when ::String then String.new(value)
  when Integer then Int.new(value)
  when ::Float then Float.new(value)
  when TrueClass, FalseClass then Boolean.new(value)
  when NilClass then Null
  else raise Error, "unsupported value type: #{value.class}"
  end
end

Instance Method Details

#==(other) ⇒ Object



28
29
30
31
32
# File 'lib/kdl/value.rb', line 28

def ==(other)
  return self == other.value if other.is_a?(self.class)

  value == other
end

#as_type(type, parser = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kdl/value.rb', line 13

def as_type(type, parser = nil)
  if parser.nil?
    self.class.new(value, format: format, type: type)
  else
    result = parser.call(self, type)
    return self.as_type(type) if result.nil?

    unless result.is_a?(::KDL::Value::Custom)
      raise ArgumentError, "expected parser to return an instance of ::KDL::Value::Custom, got `#{result.class}'"
    end

    result
  end
end

#inspectObject



40
41
42
43
44
# File 'lib/kdl/value.rb', line 40

def inspect
  return value.inspect unless type

  "(#{type.inspect})#{value.inspect}"
end

#respond_to_missing?(name, include_all = false) ⇒ Boolean

Returns:



64
65
66
# File 'lib/kdl/value.rb', line 64

def respond_to_missing?(name, include_all = false)
  value.respond_to?(name, include_all)
end

#stringify_valueObject



46
47
48
49
50
# File 'lib/kdl/value.rb', line 46

def stringify_value
  return format % value if format

  value.to_s
end

#to_sObject



34
35
36
37
38
# File 'lib/kdl/value.rb', line 34

def to_s
  return stringify_value unless type

  "(#{StringDumper.call type})#{stringify_value}"
end

#to_v2Object



56
57
58
# File 'lib/kdl/value.rb', line 56

def to_v2
  self
end

#versionObject



52
53
54
# File 'lib/kdl/value.rb', line 52

def version
  2
end