Class: ActiveRecord::TypedStore::TypedHash
- Inherits:
-
HashWithIndifferentAccess
- Object
- HashWithIndifferentAccess
- ActiveRecord::TypedStore::TypedHash
- Defined in:
- lib/active_record/typed_store/typed_hash.rb
Class Attribute Summary collapse
-
.columns ⇒ Object
readonly
Returns the value of attribute columns.
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object (also: #store)
-
#initialize(constructor = {}) ⇒ TypedHash
constructor
A new instance of TypedHash.
- #merge!(other_hash) ⇒ Object (also: #update)
Constructor Details
#initialize(constructor = {}) ⇒ TypedHash
Returns a new instance of TypedHash.
17 18 19 20 21 |
# File 'lib/active_record/typed_store/typed_hash.rb', line 17 def initialize(constructor={}) super() update(defaults_hash) update(constructor) if constructor.is_a?(Hash) end |
Class Attribute Details
.columns ⇒ Object (readonly)
Returns the value of attribute columns.
7 8 9 |
# File 'lib/active_record/typed_store/typed_hash.rb', line 7 def columns @columns end |
Class Method Details
.create(columns) ⇒ Object
9 10 11 12 13 |
# File 'lib/active_record/typed_store/typed_hash.rb', line 9 def create(columns) Class.new(self) do @columns = columns.index_by { |c| c.name.to_s } end end |
Instance Method Details
#[]=(key, value) ⇒ Object Also known as: store
23 24 25 |
# File 'lib/active_record/typed_store/typed_hash.rb', line 23 def []=(key, value) super(key, cast_value(key, value)) end |
#merge!(other_hash) ⇒ Object Also known as: update
28 29 30 31 32 33 34 35 36 |
# File 'lib/active_record/typed_store/typed_hash.rb', line 28 def merge!(other_hash) other_hash.each_pair do |key, value| if block_given? && key?(key) value = yield(convert_key(key), self[key], value) end self[convert_key(key)] = convert_value(value) end self end |