Class: SC::HashStruct
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
Buildfile::Config, Generator, Manifest, ManifestEntry, Target
Instance Method Summary collapse
-
#deep_clone ⇒ Object
This method will provide a deep clone of the hash and its contents.
-
#has_options?(opts = {}) ⇒ Boolean
Returns true if the receiver has all of the options set.
-
#initialize(opts = {}) ⇒ HashStruct
constructor
Pass in any options you want set initially on the manifest entry.
-
#merge(other_hash) ⇒ Object
Reimplement to return a new HashStruct.
-
#merge!(other_hash) ⇒ Object
Reimplement merge! to go through the []=() method so that keys can be symbolized.
-
#method_missing(id, *args) ⇒ Object
Allow for method-like access to hash also…
- #print_first_caller(*extras) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ HashStruct
Pass in any options you want set initially on the manifest entry.
51 52 53 54 55 56 |
# 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 |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args) ⇒ Object
Allow for method-like access to hash also…
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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 |
Instance Method Details
#deep_clone ⇒ Object
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
32 33 34 35 36 37 38 |
# File 'lib/sproutcore/models/hash_struct.rb', line 32 def (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) ⇒ Object
Reimplement to return a new HashStruct
116 117 118 119 120 |
# 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) ⇒ Object
Reimplement merge! to go through the []=() method so that keys can be symbolized
107 108 109 110 111 112 113 |
# 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 |
#print_first_caller(*extras) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/sproutcore/models/hash_struct.rb', line 82 def print_first_caller(*extras) return unless ENV["DEBUG_HS"] first_caller = caller.find {|str| str !~ /hash_struct\.rb/ } unless first_caller =~ %r{spec/.*(_spec|spec_helper).rb} puts "---" p extras puts first_caller puts "---" end end |
#to_hash ⇒ Object
40 41 42 43 44 |
# File 'lib/sproutcore/models/hash_struct.rb', line 40 def to_hash ret = {} each { |key, value| ret[key] = value } return ret end |