Class: SknUtils::NestedResult
Direct Known Subclasses
SknHash, Configuration, DottedHash, PageControls, ResultBean
Class Method Summary collapse
- .with_instance_vars(args) ⇒ Object
-
.with_methods(args) ⇒ Object
## Onlye good for the first level.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #===)
Ruby basic Class methods.
- #[](attr) ⇒ Object
-
#[]=(attr, value) ⇒ Object
Feature: if a new attribute is added, on first read method_missing will create getters/setters.
-
#delete_field(name) ⇒ Object
protect public methods.
-
#encode_with(coder) ⇒ Object
YAML/Psych load support, chance to re-initialize value methods.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#hash_from(sym) ⇒ Object
returns hash from any root key starting point: object.root_key - protected to reasonably ensure key is a symbol.
-
#init_with(coder) ⇒ Object
Use our hash from above to fully re-initialize this instance.
-
#initialize(params = {}, speed = true) ⇒ NestedResult
constructor
A new instance of NestedResult.
-
#keys ⇒ Object
Feature: returns keys from root input Hash.
-
#to_hash ⇒ Object
(also: #to_h)
Exporters.
- #to_json(*args) ⇒ Object
-
#to_s ⇒ Object
Returns a string containing a detailed summary of the keys and values.
Constructor Details
#initialize(params = {}, speed = true) ⇒ NestedResult
Returns a new instance of NestedResult.
105 106 107 |
# File 'lib/skn_utils/nested_result.rb', line 105 def initialize(params={}, speed=true) reset_from_empty!(params, speed) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
Feature: post-assign key/value pair, <attr>?? predicate, create getter/setter on first access
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/skn_utils/nested_result.rb', line 327 def method_missing(method, *args, &block) method_sym = key_as_sym(method) method_nsym = method_sym.is_a?(Symbol) ? method.to_s[0..-2].to_sym : method if method.to_s.end_with?("=") # add new key/value pair, transform value if Hash or Array initialize_from_hash({method_nsym => args.first}) # Add Reader/Writer one first need elsif container.key?(method_sym) container[method_sym] # Add Reader/Writer one first need elsif method.to_s.end_with?('?') # order of tests is significant, attribute?(method_nsym) else # TODO: replace following with nil to match OpenStruct or Hash behavior when key not found nil # e = NoMethodError.new "undefined method `#{method}' for #{self.class.name}", method, args # e.set_backtrace caller(1) # raise e end end |
Class Method Details
.with_instance_vars(args) ⇒ Object
101 102 103 |
# File 'lib/skn_utils/nested_result.rb', line 101 def self.with_instance_vars(args) new(args, true) end |
.with_methods(args) ⇒ Object
## Onlye good for the first level
98 99 100 |
# File 'lib/skn_utils/nested_result.rb', line 98 def self.with_methods(args) new(args, false) end |
Instance Method Details
#==(other) ⇒ Object Also known as: ===
Ruby basic Class methods
149 150 151 152 |
# File 'lib/skn_utils/nested_result.rb', line 149 def ==(other) return false unless other.is_a?(NestedResult) to_hash.eql?(other.to_hash) end |
#[](attr) ⇒ Object
109 110 111 |
# File 'lib/skn_utils/nested_result.rb', line 109 def [](attr) container[key_as_sym(attr)] end |
#[]=(attr, value) ⇒ Object
Feature: if a new attribute is added, on first read method_missing will create getters/setters
114 115 116 |
# File 'lib/skn_utils/nested_result.rb', line 114 def []=(attr, value) container.store(key_as_sym(attr), value) end |
#delete_field(name) ⇒ Object
protect public methods
118 119 120 121 122 123 124 |
# File 'lib/skn_utils/nested_result.rb', line 118 def delete_field(name) # protect public methods sym = key_as_sym(name) unless !sym.is_a?(Symbol) || self.class.method_defined?(sym) singleton_class.send(:remove_method, "#{sym.to_s}=".to_sym, sym) rescue nil container.delete(sym) end end |
#encode_with(coder) ⇒ Object
YAML/Psych load support, chance to re-initialize value methods
Use our unwrapped/original input Hash when yaml’ing
173 174 175 |
# File 'lib/skn_utils/nested_result.rb', line 173 def encode_with(coder) coder['container'] = attributes end |
#eql?(other) ⇒ Boolean
155 156 157 158 |
# File 'lib/skn_utils/nested_result.rb', line 155 def eql?(other) return false unless other.is_a?(NestedResult) to_hash.eql?(other.to_hash) end |
#hash ⇒ Object
160 161 162 |
# File 'lib/skn_utils/nested_result.rb', line 160 def hash to_hash.hash end |
#hash_from(sym) ⇒ Object
returns hash from any root key starting point: object.root_key
-
protected to reasonably ensure key is a symbol
187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/skn_utils/nested_result.rb', line 187 def hash_from(sym) starting_sym = key_as_sym(sym) bundle = ((starting_sym == container) ? container : { starting_sym => container[starting_sym] }) bundle.keys.each_with_object({}) do |attr,collector| value = bundle[attr] case value when NestedResult, self.class value = value.to_hash when Array value = value.map {|ele| array_to_hash(ele) } end collector[attr] = value # new copy end end |
#init_with(coder) ⇒ Object
Use our hash from above to fully re-initialize this instance
178 179 180 181 182 183 |
# File 'lib/skn_utils/nested_result.rb', line 178 def init_with(coder) case coder.tag when '!ruby/object:SknUtils::NestedResult', "!ruby/object:#{self.class.name}" reset_from_empty!( coder.map['container'] ) end end |
#keys ⇒ Object
Feature: returns keys from root input Hash
165 166 167 |
# File 'lib/skn_utils/nested_result.rb', line 165 def keys container.keys end |
#to_hash ⇒ Object Also known as: to_h
Exporters
129 130 131 |
# File 'lib/skn_utils/nested_result.rb', line 129 def to_hash attributes end |
#to_json(*args) ⇒ Object
135 136 137 |
# File 'lib/skn_utils/nested_result.rb', line 135 def to_json(*args) attributes.to_json(*args) end |
#to_s ⇒ Object
Returns a string containing a detailed summary of the keys and values.
142 143 144 |
# File 'lib/skn_utils/nested_result.rb', line 142 def to_s attributes.to_s end |