Class: BBLib::HashStruct

Inherits:
Hash
  • Object
show all
Defined in:
lib/bblib/core/classes/hash_struct.rb

Method Summary

Methods inherited from Hash

#deep_merge, #deep_merge!, #diff, #dive, #except, #expand, #hash_path, #hash_path_copy, #hash_path_copy_to, #hash_path_delete, #hash_path_for, #hash_path_move, #hash_path_move_to, #hash_path_set, #hash_paths, #hmap, #keys_to_s, #keys_to_s!, #keys_to_sym, #keys_to_sym!, #kmap, #only, #path_nav, #reverse, #reverse!, #squish, #to_hash_struct, #to_tree_hash, #unshift, #vmap

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/bblib/core/classes/hash_struct.rb', line 8

def method_missing(method, *args, &block)
  if args.empty? && ![:to_ary].include?(method)
    if method.to_s.end_with?('?')
      define_singleton_method(method) do
        self[method[0..-2].to_sym] ? true : false
      end
      send(method)
    else
      define_singleton_method(method) do
        self[method]
      end
      self[method]
    end
  elsif method.to_s.end_with?('=')
    define_singleton_method(method) do |arg|
      self[method[0..-2].to_sym] = arg
    end
    self[method[0..-2].to_sym] = args.first
  else
    super
  end
end