Class: ActiveRecord::Store::IndifferentCoder

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/store.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, coder_or_class_name) ⇒ IndifferentCoder

Returns a new instance of IndifferentCoder.



259
260
261
262
263
264
265
266
# File 'activerecord/lib/active_record/store.rb', line 259

def initialize(attr_name, coder_or_class_name)
  @coder =
    if coder_or_class_name.respond_to?(:load) && coder_or_class_name.respond_to?(:dump)
      coder_or_class_name
    else
      ActiveRecord::Coders::YAMLColumn.new(attr_name, coder_or_class_name || Object)
    end
end

Class Method Details

.as_indifferent_hash(obj) ⇒ Object



276
277
278
279
280
281
282
283
284
285
# File 'activerecord/lib/active_record/store.rb', line 276

def self.as_indifferent_hash(obj)
  case obj
  when ActiveSupport::HashWithIndifferentAccess
    obj
  when Hash
    obj.with_indifferent_access
  else
    ActiveSupport::HashWithIndifferentAccess.new
  end
end

Instance Method Details

#dump(obj) ⇒ Object



268
269
270
# File 'activerecord/lib/active_record/store.rb', line 268

def dump(obj)
  @coder.dump as_regular_hash(obj)
end

#load(yaml) ⇒ Object



272
273
274
# File 'activerecord/lib/active_record/store.rb', line 272

def load(yaml)
  self.class.as_indifferent_hash(@coder.load(yaml || ""))
end