class SC::HashStruct

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.

Public Class Methods

new(opts = {}) click to toggle source

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

Calls superclass method
# File lib/sproutcore/models/hash_struct.rb, line 51
def initialize(opts = {})
  super()
  opts.each do |k,v|
    self[k.to_sym] = v
  end
end

Public Instance Methods

deep_clone() click to toggle source

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.

# 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 = {}) click to toggle source

Returns true if the receiver has all of the options set

# File lib/sproutcore/models/hash_struct.rb, line 32
def has_options?(opts = {})
  opts.each do |key, value|
    this_value = self[key.to_sym]
    return false if (this_value != value)
  end
  return true
end
merge(other_hash) click to toggle source

Reimplement to return a new HashStruct

# File lib/sproutcore/models/hash_struct.rb, line 116
def merge(other_hash)
  ret = self.class.new.merge!(self)
  ret.merge!(other_hash) if other_hash != self
  return ret
end
merge!(other_hash) click to toggle source

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

# File lib/sproutcore/models/hash_struct.rb, line 107
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
method_missing(id, *args) click to toggle source

Allow for method-like access to hash also…

# File lib/sproutcore/models/hash_struct.rb, line 59
def method_missing(id, *args)
  method_name = id.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
    print_first_caller(method_name)
    self[method_name.to_sym] = value

  # convert property? => !!self[:property]
  elsif method_name =~ /\?$/
    !!self[method_name[0..-2].to_sym]
  else
    print_first_caller(method_name)
    self[method_name.to_sym]
  end
end
print_first_caller(*extras) click to toggle source
to_hash() click to toggle source
# File lib/sproutcore/models/hash_struct.rb, line 40
def to_hash
  ret = {}
  each { |key, value| ret[key] = value }
  return ret
end