Class: SknUtils::NestedResult

Inherits:
Object
  • Object
show all
Defined in:
lib/skn_utils/nested_result.rb

Direct Known Subclasses

SknConfiguration

Instance Method Summary collapse

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



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/skn_utils/nested_result.rb', line 317

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

Returns:

  • (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

#hashObject



151
152
153
# File 'lib/skn_utils/nested_result.rb', line 151

def hash
  to_hash.hash
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

#keysObject

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_hashObject 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_sObject

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