Class: SknUtils::NestedResult

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, speed = true) ⇒ NestedResult

Returns a new instance of NestedResult.



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

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



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/skn_utils/nested_result.rb', line 329

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



103
104
105
# File 'lib/skn_utils/nested_result.rb', line 103

def self.with_instance_vars(args)
  new(args, true)
end

.with_methods(args) ⇒ Object

## Onlye good for the first level



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

def self.with_methods(args)
  new(args, false)
end

Instance Method Details

#==(other) ⇒ Object Also known as: ===

Ruby basic Class methods



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

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

#[](attr) ⇒ Object



111
112
113
# File 'lib/skn_utils/nested_result.rb', line 111

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



116
117
118
# File 'lib/skn_utils/nested_result.rb', line 116

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

#delete_field(name) ⇒ Object

protect public methods



120
121
122
123
124
125
126
# File 'lib/skn_utils/nested_result.rb', line 120

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



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

def encode_with(coder)
  coder['container'] = attributes
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


157
158
159
160
# File 'lib/skn_utils/nested_result.rb', line 157

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

#hashObject



162
163
164
# File 'lib/skn_utils/nested_result.rb', line 162

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



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/skn_utils/nested_result.rb', line 189

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



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

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



167
168
169
# File 'lib/skn_utils/nested_result.rb', line 167

def keys
  container.keys
end

#to_hashObject Also known as: to_h

Exporters



131
132
133
# File 'lib/skn_utils/nested_result.rb', line 131

def to_hash
  attributes
end

#to_json(*args) ⇒ Object



137
138
139
# File 'lib/skn_utils/nested_result.rb', line 137

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

#to_sObject

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



144
145
146
# File 'lib/skn_utils/nested_result.rb', line 144

def to_s
  attributes.to_s
end