Class: Psych::Nodes::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/atk/extra_yaml.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#documentObject

Returns the value of attribute document.



92
93
94
# File 'lib/atk/extra_yaml.rb', line 92

def document
  @document
end

Instance Method Details

#[](key) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/atk/extra_yaml.rb', line 118

def [](key)
    previous = nil
    # for seq
    if key.is_a?(Integer)
        return self.children[key]
    end
    # for maps
    for each in self.children.reverse
        if each.respond_to?(:value) && each.value == key
            return previous
        end
        previous = each
    end
end

#anchor_and_tag(anchor: nil, tag: nil) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/atk/extra_yaml.rb', line 93

def anchor_and_tag(anchor:nil, tag:nil)
    anchor = @anchor if anchor == nil 
    tag = @tag if tag == nil 
    
    string = ""
    if anchor
        string += "&#{anchor} "
    end
    
    if tag
        string += "!#{tag} "
    end
    return string
end

#indent_levelObject

saving this for later once the JSON method can be replaced



109
110
111
112
113
114
115
116
# File 'lib/atk/extra_yaml.rb', line 109

def indent_level
    if self.respond_to?(:children)
        if self.children.size > 0
            return self.children[0].start_column
        end
    end
    return 4 + self.start_column
end

#replace_value_with(value: nil, literal: nil, anchor: nil, tag: nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/atk/extra_yaml.rb', line 133

def replace_value_with(value:nil, literal:nil, anchor: nil, tag:nil)
    # check version
    if VERSION_OF_RUBY < "2.5.0"
        raise "\n\nSomewhere, replace_value_with() is being called, which is related to editing yaml\nthe problem is this function needs ruby >= 2.5.0\nbut the code is being run with ruby #{RUBY_VERSION}"
    end
    
    if literal == nil
        new_value = value.to_json
    else
        new_value = literal
    end
    middle_part = self.anchor_and_tag(anchor:anchor, tag:tag) + new_value
    new_string = inject_string(@document.string, middle_part, @start_line, @start_column, @end_line, @end_column)
    @document.init( string: new_string )
end