Class: YAML::Transformer::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/yaml_extensions/transform.rb

Constant Summary collapse

@@cpt =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Node

Returns a new instance of Node.

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
# File 'lib/yaml_extensions/transform.rb', line 59

def initialize ( name )
  raise ArgumentError unless name.is_a?(Regexp)
  @name = name
  @values = []
  @uid = @@cpt
  @@cpt += 1
  # Save a node to handle the '#' feature.
  @saved_node_key = nil
  @saved_node_value = nil
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



55
56
57
# File 'lib/yaml_extensions/transform.rb', line 55

def name
  @name
end

#valuesObject (readonly)

Returns the value of attribute values.



55
56
57
# File 'lib/yaml_extensions/transform.rb', line 55

def values
  @values
end

Instance Method Details

#dottify(stream) ⇒ Object

Dottify a node.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/yaml_extensions/transform.rb', line 83

def dottify ( stream )
  # Declare node
  stream << "#{unique_name} [shape=box, color=blue, label=#{self}]\n"
  # Print node links with values
  stream << "#{unique_name} -> { rank=same; "
  @values.each_index { |i| stream << "value#{@uid}_#{i}; " }
  stream << "}\n"
  # Declare each values and links
  @values.each_with_index do |val, i|
    stream << "value#{@uid}_#{i} [shape=box, color=yellow, label=\"/"
    stream << val.regexp.source << '/'
    stream << ' #' if val.remember
    stream << "\"]\n" 
    stream << "value#{@uid}_#{i} -> { rank=same; "
    val.associated.each { |match| stream << "#{match.unique_name}; " }
    stream << "} \n"
    val.associated.each { |match| match.dottify(stream) }
  end
end

#get_associated(value, remember = false) ⇒ Object

Return the associated nodes of a value Add the value if not present.



73
74
75
76
77
78
79
80
# File 'lib/yaml_extensions/transform.rb', line 73

def get_associated ( value, remember=false )
  val = @values.find { |e| e.regexp == value and e.remember == remember }
  if val.nil?
    val = Struct::Value.new(value, remember, [])
    @values << val
  end
  return val.associated
end

#has_saved_node?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/yaml_extensions/transform.rb', line 139

def has_saved_node?
  return ! (@saved_node_key.nil? and @saved_node_value.nil?)
end

#match(key, value) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/yaml_extensions/transform.rb', line 103

def match (  key, value )
  return [] unless key =~ @name
  activated = []
  @values.each do |val|
    if ! value.is_a?(String) or value =~ val.regexp
      save_node_set(key, value) if val.remember
      val.associated.each do |n|
        if n.is_a?(Match)
          # There must be a remembered node in the path.
          p self unless has_saved_node?
          n[@saved_node_key, @saved_node_value]
        else
          n.save_node_set(@saved_node_key,
                          @saved_node_value) if has_saved_node?
          activated << n
        end
      end
      save_node_reset if val.remember
    end
  end
  save_node_reset
  return activated
end

#save_node_resetObject



134
135
136
137
# File 'lib/yaml_extensions/transform.rb', line 134

def save_node_reset
  @saved_node_key = nil
  @saved_node_value = nil
end

#save_node_set(key, value) ⇒ Object

Raises:

  • (RuntimeError)


127
128
129
130
131
132
# File 'lib/yaml_extensions/transform.rb', line 127

def save_node_set ( key, value )
  raise RuntimeError unless @saved_node_key.nil?
  @saved_node_key = key
  raise RuntimeError unless @saved_node_value.nil?
  @saved_node_value = value
end

#to_sObject

Human readable name.



148
149
150
# File 'lib/yaml_extensions/transform.rb', line 148

def to_s
  "\"/#{@name.source}/\""
end

#unique_nameObject



143
144
145
# File 'lib/yaml_extensions/transform.rb', line 143

def unique_name
  "node#{@uid}"
end