Class: SC::HashStruct

Inherits:
Hash
  • Object
show all
Defined in:
lib/sproutcore/models/hash_struct.rb

Overview

A HashStruct is a type of hash that can also be accessed as a structed (like an OpenStruct). It also treats strings and symbols as the same for keys.

Direct Known Subclasses

Generator, Manifest, Target

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ HashStruct

Pass in any options you want set initially on the manifest entry.



50
51
52
53
# File 'lib/sproutcore/models/hash_struct.rb', line 50

def initialize(opts = {})
  super
  self.merge!(opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object

Allow for method-like access to hash also...



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sproutcore/models/hash_struct.rb', line 56

def method_missing(method_name, *args)
  method_name = method_name.to_s
  if method_name =~ /=$/
    # suppoert property? = true
    if method_name =~ /\?=$/
      method_name = method_name[0..-3]
      value = !!args[0]
    else
      method_name = method_name[0..-2]
      value = args[0]
    end
    self[method_name] = value
    
  # convert property? => !!self[:property]
  elsif method_name =~ /\?$/
    !!self[method_name[0..-2]]
  else
    self[method_name]
  end
end

Instance Method Details

#[](key) ⇒ Object

Treat all keys like symbols



78
79
80
81
82
# File 'lib/sproutcore/models/hash_struct.rb', line 78

def [](key)
  sym_key = key.to_sym rescue nil
  raise "HashStruct cannot convert #{key} to symbol" if sym_key.nil?
  fetch(sym_key, nil)
end

#[]=(key, value) ⇒ Object



84
85
86
87
88
# File 'lib/sproutcore/models/hash_struct.rb', line 84

def []=(key, value)
  sym_key = key.to_sym rescue nil
  raise "HashStruct cannot convert #{key} to symbol" if sym_key.nil?
  store(sym_key, value)
end

#deep_cloneObject

This method will provide a deep clone of the hash and its contents. If any member methods also respond to deep_clone, that method will be used.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sproutcore/models/hash_struct.rb', line 18

def deep_clone
  sibling = self.class.new
  self.each do | key, value |
    if value.respond_to? :deep_clone
      value = value.deep_clone
    else
      value = value.clone rescue value
    end
    sibling[key] = value
  end
  sibling
end

#has_options?(opts = {}) ⇒ Boolean

Returns true if the receiver has all of the options set

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/sproutcore/models/hash_struct.rb', line 32

def has_options?(opts = {})
  opts.each do |key, value|
    return false if self[key] != value
  end
  return true
end

#merge(other_hash) ⇒ Object

Reimplement to return a new HashStruct



101
102
103
104
105
# File 'lib/sproutcore/models/hash_struct.rb', line 101

def merge(other_hash)
  ret = self.class.new.merge!(self)
  ret.merge!(other_hash) if other_hash != self
  return ret 
end

#merge!(other_hash) ⇒ Object

Reimplement merge! to go through the []=() method so that keys can be symbolized



92
93
94
95
96
97
98
# File 'lib/sproutcore/models/hash_struct.rb', line 92

def merge!(other_hash)
  return self if other_hash == self
  unless other_hash.nil?
    other_hash.each { |k,v| self[k] = v }
  end
  return self
end

#to_hashObject



39
40
41
42
43
# File 'lib/sproutcore/models/hash_struct.rb', line 39

def to_hash
  ret = {}
  each { |key, value| ret[key] = value }
  return ret
end