Class: SknUtils::NestedResult

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

Constant Summary collapse

InspectKey =

Returns a string containing a detailed summary of the keys and values.

:__inspect_key__

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ NestedResult

Returns a new instance of NestedResult.



96
97
98
99
# File 'lib/skn_utils/nested_result.rb', line 96

def initialize(params={})
  @container =  {}
  initialize_from_hash(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



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/skn_utils/nested_result.rb', line 320

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?("=") and container[method_nsym].nil?           # add new key/value pair, transform value if Hash or Array
    initialize_from_hash({method_nsym => args.first})

  elsif container.key?(method_sym)
    puts "#{__method__}() method: #{method}"
    enable_dot_notation(method_sym)                                          # Add Reader/Writer one first need
    container[method_sym]

  elsif method.to_s.end_with?('?')                                           # order of tests is significant,
    attribute?(method_nsym)

  else
    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



164
165
166
167
# File 'lib/skn_utils/nested_result.rb', line 164

def ==(other)
  return false unless other.is_a?(NestedResult)
  to_hash.eql?(other.to_hash)
end

#[](attr) ⇒ Object



101
102
103
# File 'lib/skn_utils/nested_result.rb', line 101

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



106
107
108
# File 'lib/skn_utils/nested_result.rb', line 106

def []=(attr, value)
  container.store(key_as_sym(attr), value)
end

#delete_field(name) ⇒ Object

protect public methods



110
111
112
113
114
115
116
# File 'lib/skn_utils/nested_result.rb', line 110

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



188
189
190
# File 'lib/skn_utils/nested_result.rb', line 188

def encode_with(coder)
  coder['container'] = self.to_h
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
# File 'lib/skn_utils/nested_result.rb', line 170

def eql?(other)
  return false unless other.is_a?(NestedResult)
  to_hash.eql?(other.to_hash)
end

#hashObject



175
176
177
# File 'lib/skn_utils/nested_result.rb', line 175

def hash
  to_hash.hash
end

#init_with(coder) ⇒ Object

Use our hash from above to fully re-initialize this instance



193
194
195
196
197
198
# File 'lib/skn_utils/nested_result.rb', line 193

def init_with(coder)
  case coder.tag
    when '!ruby/object:SknUtils::NestedResult'
      initialize_from_hash( coder.map['container'] )
  end
end

#inspectObject Also known as: to_s

:nodoc:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/skn_utils/nested_result.rb', line 135

def inspect
  package = to_hash
  str = "#<#{self.class}"

  ids = (Thread.current[InspectKey] ||= [])
  if ids.include?(object_id)
    return str << ' ...>'
  end

  ids << object_id
  begin
    first = true
    for k,v in package
      str << "," unless first
      first = false
      str << " #{k}=#{v.inspect}"
    end
    return str << '>'
  ensure
    ids.pop
  end
end

#keysObject

Feature: returns keys from root input Hash



180
181
182
# File 'lib/skn_utils/nested_result.rb', line 180

def keys
  container.keys
end

#to_hashObject Also known as: to_h

Exporters



121
122
123
# File 'lib/skn_utils/nested_result.rb', line 121

def to_hash
  attributes
end

#to_json(*args) ⇒ Object



127
128
129
# File 'lib/skn_utils/nested_result.rb', line 127

def to_json(*args)
  attributes.to_json(*args)
end