Class: Yerba::Scalar

Inherits:
Object
  • Object
show all
Defined in:
lib/yerba/scalar.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document_or_value, selector_or_opts = nil, value = nil, location = nil, key = nil, quote_style: nil) ⇒ Scalar

Returns a new instance of Scalar.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yerba/scalar.rb', line 7

def initialize(document_or_value, selector_or_opts = nil, value = nil, location = nil, key = nil, quote_style: nil)
  if document_or_value.is_a?(Document) || document_or_value.nil?
    @document = document_or_value
    @selector = selector_or_opts
    @value = value
    @location = location
    @key = key
    @quote_style = quote_style
  else
    @document = nil
    @selector = nil
    @value = document_or_value
    @location = nil
    @key = nil
    @quote_style = selector_or_opts.is_a?(Hash) ? selector_or_opts[:quote_style] : quote_style
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



5
6
7
# File 'lib/yerba/scalar.rb', line 5

def key
  @key
end

#locationObject (readonly)

Returns the value of attribute location.



5
6
7
# File 'lib/yerba/scalar.rb', line 5

def location
  @location
end

#selectorObject (readonly)

Returns the value of attribute selector.



5
6
7
# File 'lib/yerba/scalar.rb', line 5

def selector
  @selector
end

Instance Method Details

#==(other) ⇒ Object



61
62
63
# File 'lib/yerba/scalar.rb', line 61

def ==(other)
  value == other
end

#deleteObject



73
74
75
# File 'lib/yerba/scalar.rb', line 73

def delete
  @document&.delete(@selector)
end

#inspectObject



77
78
79
80
81
82
83
# File 'lib/yerba/scalar.rb', line 77

def inspect
  if @selector
    "#<Yerba::Scalar selector=#{@selector.inspect} value=#{value.inspect}>"
  else
    "#<Yerba::Scalar value=#{value.inspect} quote_style=#{quote_style.inspect}>"
  end
end

#quote_styleObject



29
30
31
# File 'lib/yerba/scalar.rb', line 29

def quote_style
  @quote_style || @document&.get_quote_style(@selector)
end

#quote_style=(style) ⇒ Object



33
34
35
36
37
# File 'lib/yerba/scalar.rb', line 33

def quote_style=(style)
  @document&.set_quote_style(@selector, style)

  @quote_style = style
end

#to_fObject



57
58
59
# File 'lib/yerba/scalar.rb', line 57

def to_f
  value.to_f
end

#to_iObject



53
54
55
# File 'lib/yerba/scalar.rb', line 53

def to_i
  value.to_i
end

#to_sObject



45
46
47
# File 'lib/yerba/scalar.rb', line 45

def to_s
  value.to_s
end

#to_strObject



49
50
51
# File 'lib/yerba/scalar.rb', line 49

def to_str
  to_s
end

#to_yamlObject



65
66
67
68
69
70
71
# File 'lib/yerba/scalar.rb', line 65

def to_yaml
  case value
  when nil then "null"
  when String then Formatting.quote(value, quote_style)
  else value.to_s
  end
end

#valueObject



25
26
27
# File 'lib/yerba/scalar.rb', line 25

def value
  @value ||= @document&.get(@selector)
end

#value=(new_value) ⇒ Object Also known as: set



39
40
41
42
# File 'lib/yerba/scalar.rb', line 39

def value=(new_value)
  @document&.set(@selector, new_value)
  @value = new_value
end