Class: SymbolMatrix

Inherits:
Hash
  • Object
show all
Includes:
Discoverer::Reader, Discoverer::Writer
Defined in:
lib/reader/symbolmatrix.rb,
lib/writer/symbolmatrix.rb,
lib/symbolmatrix/symbolmatrix.rb,
lib/symbolmatrix/serialization.rb

Defined Under Namespace

Classes: InvalidKeyException, KeyNotDefinedException, MergeError, Serialization

Instance Method Summary collapse

Constructor Details

#initialize(argument = nil) ⇒ SymbolMatrix

Initializes the SymbolMatrix with the passed Hash if a hash is passed



6
7
8
9
# File 'lib/symbolmatrix/symbolmatrix.rb', line 6

def initialize hash = nil
  super
  merge! hash unless hash.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object

Allows values to be retrieved and set using Ruby’s dot method syntax.



63
64
65
66
67
68
69
# File 'lib/symbolmatrix/symbolmatrix.rb', line 63

def method_missing sym, *args
  if sym.to_s.index "="
    store sym.to_s[0..-2].to_sym, args.shift
  else
    self[sym]
  end
end

Instance Method Details

#[](key) ⇒ Object

Retrieves the value of the given key.



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

def [] key
  validate_key key
  raise KeyNotDefinedException, "The key :#{key} is not defined" unless self.has_key? key.to_sym
  super key.to_sym
end

#from_file(*args) ⇒ Object

Deprecated.

Use #from.file



34
35
36
# File 'lib/reader/symbolmatrix.rb', line 34

def from_file *args
  Kernel.warn "[DEPRECATION]: #from_file is deprecated, please use #from.file instead"
end

#from_yaml(*args) ⇒ Object

Deprecated.

Use #from.yaml



29
30
31
# File 'lib/reader/symbolmatrix.rb', line 29

def from_yaml *args
  Kernel.warn "[DEPRECATION]: #from_yaml is deprecated, please use #from.yaml instead"
end

#merge(hash) ⇒ Object

Checks the keys for compatibility with SymbolMatrix and calls the merge in Hash



54
55
56
57
58
59
60
# File 'lib/symbolmatrix/symbolmatrix.rb', line 54

def merge hash
  # Before merging, let's check the keys
  hash.each_key do |key|
    validate_key key
  end
  super hash
end

#merge!(hash) ⇒ Object Also known as: update

Merges the passed hash within self



45
46
47
48
49
# File 'lib/symbolmatrix/symbolmatrix.rb', line 45

def merge! hash
  hash.each do |key, value|
    store key, value
  end
end

#recursive_merge(hash, override = false) ⇒ Object

Merges this SymbolMatrix with another SymbolMatrix recursively



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/symbolmatrix/symbolmatrix.rb', line 72

def recursive_merge hash, override = false
  result = SymbolMatrix.new
  self.keys.each do |key|
    result[key] = self[key]
  end

  hash.keys.each do |key|
    if result.has_key? key
      if result[key].respond_to? :recursive_merge
        result[key] = result[key].recursive_merge hash[key], override
      else
        if override
          result[key] = hash[key]
        else
          raise MergeError, "The value of the :#{key} key is already defined. Run recursive merge with flag true if you want it to override"
        end
      end
    else
      result[key] = hash[key]
    end
  end
  return result
end

#recursive_merge!(symbolmatrix, override = false) ⇒ Object

Merges recursively the passed SymbolMatrix into self



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/symbolmatrix/symbolmatrix.rb', line 97

def recursive_merge! symbolmatrix, override = false
  symbolmatrix.each do |key, value|
    if self.has_key? key
      if self[key].respond_to? :recursive_merge!
        self[key].recursive_merge! value, override
      else
        if override
          self[key] = value
        else
          raise MergeError, "The value of the :#{key} key is already defined. Run recursive merge with flag true if you want it to override"
        end
      end
    else 
      self[key] = value
    end
  end
end

#store(key, val) ⇒ Object Also known as: []=

Sets the value of the given key to val if the key is valid.



21
22
23
24
25
26
27
28
# File 'lib/symbolmatrix/symbolmatrix.rb', line 21

def store key, val
  validate_key key
  if val.instance_of? Hash
    super(key.to_sym, SymbolMatrix.new(val))
  else
    super(key.to_sym, val)
  end
end

#to_hashObject

Deprecated.

Use #to.hash instead



40
41
42
# File 'lib/symbolmatrix/symbolmatrix.rb', line 40

def to_hash 
  Kernel.warn "[DEPRECATION]: #to_hash is deprecated, please use #to.hash instead"
end

#validate_key(key) ⇒ Object

Validates whether the given key is usable in a SymbolMatrix



12
13
14
15
16
17
18
# File 'lib/symbolmatrix/symbolmatrix.rb', line 12

def validate_key key
  unless key.respond_to? :to_sym
    raise InvalidKeyException, "The key '#{key}' does not respond to #to_sym, so is not a valid key for SymbolMatrix"
  end
    
  return true
end