Class: SknUtils::NestedResult
- Inherits:
-
Object
- Object
- SknUtils::NestedResult
- Defined in:
- lib/skn_utils/nested_result.rb
Direct Known Subclasses
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 = {}) ⇒ 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 = {}) ⇒ NestedResult
Returns a new instance of NestedResult.
96 97 98 |
# File 'lib/skn_utils/nested_result.rb', line 96 def initialize(params={}) reset_from_empty!(params) 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
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/skn_utils/nested_result.rb', line 318 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 behavior when key not found e = NoMethodError.new "undefined method `#{method}' for #{self.class.name}", method, args e.set_backtrace caller(1) raise e end end |
Instance Method Details
#==(other) ⇒ Object Also known as: ===
Ruby basic Class methods
140 141 142 143 |
# File 'lib/skn_utils/nested_result.rb', line 140 def ==(other) return false unless other.is_a?(NestedResult) to_hash.eql?(other.to_hash) end |
#[](attr) ⇒ Object
100 101 102 |
# File 'lib/skn_utils/nested_result.rb', line 100 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
105 106 107 |
# File 'lib/skn_utils/nested_result.rb', line 105 def []=(attr, value) container.store(key_as_sym(attr), value) end |
#delete_field(name) ⇒ Object
protect public methods
109 110 111 112 113 114 115 |
# File 'lib/skn_utils/nested_result.rb', line 109 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
164 165 166 |
# File 'lib/skn_utils/nested_result.rb', line 164 def encode_with(coder) coder['container'] = attributes end |
#eql?(other) ⇒ Boolean
146 147 148 149 |
# File 'lib/skn_utils/nested_result.rb', line 146 def eql?(other) return false unless other.is_a?(NestedResult) to_hash.eql?(other.to_hash) end |
#hash ⇒ Object
151 152 153 |
# File 'lib/skn_utils/nested_result.rb', line 151 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
178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/skn_utils/nested_result.rb', line 178 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 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
169 170 171 172 173 174 |
# File 'lib/skn_utils/nested_result.rb', line 169 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
156 157 158 |
# File 'lib/skn_utils/nested_result.rb', line 156 def keys container.keys end |
#to_hash ⇒ Object Also known as: to_h
Exporters
120 121 122 |
# File 'lib/skn_utils/nested_result.rb', line 120 def to_hash attributes end |
#to_json(*args) ⇒ Object
126 127 128 |
# File 'lib/skn_utils/nested_result.rb', line 126 def to_json(*args) attributes.to_json(*args) end |
#to_s ⇒ Object
Returns a string containing a detailed summary of the keys and values.
133 134 135 |
# File 'lib/skn_utils/nested_result.rb', line 133 def to_s attributes.to_s end |