Module: Bai2::AttrReaderFromIvarHash
- Included in:
- BaiFile, BaiFile::Account, BaiFile::Group, BaiFile::Transaction
- Defined in:
- lib/bai2/attr-reader-from-ivar-hash.rb
Overview
Helps define methods that simply read from a hash ivar. For example, imagine this class:
class Person
def initialize
@info = {
first_name: 'John',
last_name: 'Smith',
}
end
attr_reader_from_ivar_hash :@info, :first_name, :last_name
end
That last statement will automagically create methods ‘.first_name`, and `.last_name` on `Person`, which saves a whole bunch of typing :).
Instance Method Summary collapse
Instance Method Details
#attr_reader_from_ivar_hash(ivar, *keys) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/bai2/attr-reader-from-ivar-hash.rb', line 23 def attr_reader_from_ivar_hash(ivar, *keys) keys.each do |key| define_method(key) do (instance_variable_get(ivar) || {})[key] end end end |