Class: Sitepress::Data::Record
- Inherits:
-
Object
- Object
- Sitepress::Data::Record
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/sitepress/data.rb
Overview
Wraps a hash and returns managed elements
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #dig(*args, **kwargs, &block) ⇒ Object
- #each ⇒ Object
- #fetch(key, *args, &block) ⇒ Object
-
#initialize(hash) ⇒ Record
constructor
A new instance of Record.
- #method_missing(name, *args, **kwargs, &block) ⇒ Object
- #unmanage ⇒ Object
Constructor Details
#initialize(hash) ⇒ Record
Returns a new instance of Record.
46 47 48 |
# File 'lib/sitepress/data.rb', line 46 def initialize(hash) @hash = hash end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kwargs, &block) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sitepress/data.rb', line 72 def method_missing(name, *args, **kwargs, &block) if respond_to? name self.send name, *args, **kwargs, &block else key, modifier, _ = name.to_s.partition(/[!?]/) case modifier when "" self[key] when "!" self.fetch(key, *args, &block) when "?" !!self[key] end end end |
Instance Method Details
#[](key) ⇒ Object
54 55 56 |
# File 'lib/sitepress/data.rb', line 54 def [](key) Data.manage(@hash[key.to_s]) end |
#[]=(key, value) ⇒ Object
58 59 60 |
# File 'lib/sitepress/data.rb', line 58 def []=(key, value) Data.manage(@hash[key.to_s] = value) end |
#dig(*args, **kwargs, &block) ⇒ Object
89 90 91 |
# File 'lib/sitepress/data.rb', line 89 def dig(*args, **kwargs, &block) Data.manage @hash.dig(*args, **kwargs, &block) end |
#each ⇒ Object
62 63 64 65 66 |
# File 'lib/sitepress/data.rb', line 62 def each @hash.each do |key, value| yield key, Data.manage(value) end end |