Class: GraphViz::Attrs

Inherits:
Object show all
Defined in:
lib/graphviz/attrs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gviz, name, attributes) ⇒ Attrs

Returns a new instance of Attrs.



24
25
26
27
28
29
# File 'lib/graphviz/attrs.rb', line 24

def initialize( gviz, name, attributes )
   @name       = name
   @attributes = attributes
   @data       = Hash::new( )
   @graphviz   = gviz
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



22
23
24
# File 'lib/graphviz/attrs.rb', line 22

def data
  @data
end

Instance Method Details

#[](key) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/graphviz/attrs.rb', line 41

def []( key )
   if key.class == Hash
      key.each do |k, v|
         self[k] = v
      end
   else
      @data[key.to_s]
   end
end

#[]=(key, value) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/graphviz/attrs.rb', line 51

def []=( key, value )
   unless @attributes.keys.include?( key.to_s )
      raise ArgumentError, "#{@name} attribute '#{key.to_s}' invalid"
   end

   if value.nil?
      warn "Value for attribute `#{key}` can't be null"
      return
   end

   begin
      value = GraphViz::Types.const_get(@attributes[key.to_s]).new(value)
   rescue => e
      raise AttributeException, "Invalid value `#{value}` for attribute `#{key}` : #{e}"
   end

   if value
     @data[key.to_s] = value
     @graphviz.set_position( @name, key.to_s, @data[key.to_s] ) if @graphviz
   end
end

#eachObject



31
32
33
34
35
# File 'lib/graphviz/attrs.rb', line 31

def each
   @data.each do |k, v|
      yield(k, v)
   end
end

#to_hObject



37
38
39
# File 'lib/graphviz/attrs.rb', line 37

def to_h
   @data.clone
end