Class: Yerba::Scalar
- Inherits:
-
Object
- Object
- Yerba::Scalar
- Defined in:
- lib/yerba/scalar.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#location ⇒ Object
readonly
Returns the value of attribute location.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #delete ⇒ Object
-
#initialize(document_or_value, selector_or_opts = nil, value = nil, location = nil, key = nil, quote_style: nil) ⇒ Scalar
constructor
A new instance of Scalar.
- #inspect ⇒ Object
- #quote_style ⇒ Object
- #quote_style=(style) ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
- #to_yaml ⇒ Object
- #value ⇒ Object
- #value=(new_value) ⇒ Object (also: #set)
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
5 6 7 |
# File 'lib/yerba/scalar.rb', line 5 def key @key end |
#location ⇒ Object (readonly)
Returns the value of attribute location.
5 6 7 |
# File 'lib/yerba/scalar.rb', line 5 def location @location end |
#selector ⇒ Object (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 |
#delete ⇒ Object
73 74 75 |
# File 'lib/yerba/scalar.rb', line 73 def delete @document&.delete(@selector) end |
#inspect ⇒ Object
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_style ⇒ Object
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_f ⇒ Object
57 58 59 |
# File 'lib/yerba/scalar.rb', line 57 def to_f value.to_f end |
#to_i ⇒ Object
53 54 55 |
# File 'lib/yerba/scalar.rb', line 53 def to_i value.to_i end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/yerba/scalar.rb', line 45 def to_s value.to_s end |
#to_str ⇒ Object
49 50 51 |
# File 'lib/yerba/scalar.rb', line 49 def to_str to_s end |
#to_yaml ⇒ Object
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 |
#value ⇒ Object
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 |