Class: BBLib::PathHash
- Inherits:
- BasicObject
- Defined in:
- lib/hash_path/path_hash.rb
Overview
Wraps a hash in a PathHash object which allows ActiveRecord-like access to hash parameters. For example, methods are treated as keys passed in to Hash’s [] method and ._ can be used to indicate that the next element should be searched for recursively.
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#recursive ⇒ Object
readonly
Returns the value of attribute recursive.
Instance Method Summary collapse
- #[](val) ⇒ Object
- #_ ⇒ Object
- #_fval ⇒ Object (also: #_f)
- #_path(arg, formula = nil) ⇒ Object
- #_val ⇒ Object (also: #_v)
-
#initialize(hash) ⇒ PathHash
constructor
A new instance of PathHash.
-
#method_missing(arg, formula = nil) ⇒ Object
Does not fall back on super as ALL input is accepted.
- #respond_to_missing?(*args) ⇒ Boolean
Constructor Details
#initialize(hash) ⇒ PathHash
Returns a new instance of PathHash.
18 19 20 |
# File 'lib/hash_path/path_hash.rb', line 18 def initialize(hash) @hash = hash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(arg, formula = nil) ⇒ Object
Does not fall back on super as ALL input is accepted
50 51 52 53 54 55 56 57 58 |
# File 'lib/hash_path/path_hash.rb', line 50 def method_missing(arg, formula = nil) arg = (@recursive ? "..#{arg}" : arg.to_s) + (formula ? "(#{formula})" : '') if @hash.is_a?(::Array) PathHash.new @hash.flat_map { |h| if h.is_a?(::Array) || h.is_a?(::Hash) then h.hash_path(arg) end } else PathHash.new @hash.hpath(arg) end end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
16 17 18 |
# File 'lib/hash_path/path_hash.rb', line 16 def hash @hash end |
#recursive ⇒ Object (readonly)
Returns the value of attribute recursive.
16 17 18 |
# File 'lib/hash_path/path_hash.rb', line 16 def recursive @recursive end |
Instance Method Details
#[](val) ⇒ Object
22 23 24 |
# File 'lib/hash_path/path_hash.rb', line 22 def [](val) PathHash.new(@hash.map { |h| h[val] }) end |
#_ ⇒ Object
40 41 42 43 |
# File 'lib/hash_path/path_hash.rb', line 40 def _ @recursive = true self end |
#_fval ⇒ Object Also known as: _f
32 33 34 35 36 |
# File 'lib/hash_path/path_hash.rb', line 32 def _fval @hash.first rescue @hash end |
#_path(arg, formula = nil) ⇒ Object
45 46 47 |
# File 'lib/hash_path/path_hash.rb', line 45 def _path(arg, formula = nil) method_missing arg, formula end |
#_val ⇒ Object Also known as: _v
26 27 28 |
# File 'lib/hash_path/path_hash.rb', line 26 def _val @hash end |
#respond_to_missing?(*args) ⇒ Boolean
60 61 62 |
# File 'lib/hash_path/path_hash.rb', line 60 def respond_to_missing?(*args) true || super end |