Class: Prune::PObjects::PDictionary

Inherits:
Base
  • Object
show all
Defined in:
lib/prune/p_objects/p_dictionary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#indent!, #outdent!, #space, #value_to_s

Constructor Details

#initialize(pd = {}) ⇒ PDictionary

Returns a new instance of PDictionary.



8
9
10
11
# File 'lib/prune/p_objects/p_dictionary.rb', line 8

def initialize(pd = {})
  @dict = {}
  @dict.update(pd)
end

Instance Attribute Details

#dictObject (readonly)

Returns the value of attribute dict.



6
7
8
# File 'lib/prune/p_objects/p_dictionary.rb', line 6

def dict
  @dict
end

Instance Method Details

#[](key) ⇒ Object



17
18
19
# File 'lib/prune/p_objects/p_dictionary.rb', line 17

def [](key)
  @dict[key]
end

#[]=(key, value) ⇒ Object



21
22
23
# File 'lib/prune/p_objects/p_dictionary.rb', line 21

def []=(key, value)
  self.update(key => value)
end

#empty?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/prune/p_objects/p_dictionary.rb', line 13

def empty?
  return @dict.empty?
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/prune/p_objects/p_dictionary.rb', line 42

def has_key?(key)
  @dict.has_key?(key)
end

#keysObject



46
47
48
49
50
51
# File 'lib/prune/p_objects/p_dictionary.rb', line 46

def keys
  keys = @dict.keys.sort_by{|key| key.to_s}
  type_key = PName.new(:Type)
  keys.unshift(type_key) if keys.delete(type_key)
  keys
end

#sizeObject



53
54
55
# File 'lib/prune/p_objects/p_dictionary.rb', line 53

def size
  @dict.size
end

#to_sObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/prune/p_objects/p_dictionary.rb', line 57

def to_s
  case self.size
  when 0
    "<< >>"
  when 1
    "<< %s >>" % pair_to_s(self.keys.first)
  else
    out = []
    out << "<<"
    indent!
    self.keys.each{|key|
      out << space + pair_to_s(key)
    }
    outdent!
    out << space + ">>"
    out.join(LF)
  end
end

#update(pd) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/prune/p_objects/p_dictionary.rb', line 25

def update(pd)
  case pd
  when PDictionary
    raise PDictionaryKeyError unless pd.keys.all?{|key|
      key.instance_of?(PName)
    }
    @dict.update(pd.dict)
  when Hash
    raise PDictionaryKeyError unless pd.keys.all?{|key|
      key.instance_of?(PName)
    }
    @dict.update(pd)
  else
    raise PDictionaryTypeError
  end
end