Class: ActiveRecord::Store::IndifferentCoder

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

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coder_or_class_name) ⇒ IndifferentCoder

Returns a new instance of IndifferentCoder.



167
168
169
170
171
172
173
174
# File 'lib/active_record/store.rb', line 167

def initialize(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(coder_or_class_name || Object)
    end
end

Class Method Details

.as_indifferent_hash(obj) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/active_record/store.rb', line 184

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



176
177
178
# File 'lib/active_record/store.rb', line 176

def dump(obj)
  @coder.dump self.class.as_indifferent_hash(obj)
end

#load(yaml) ⇒ Object



180
181
182
# File 'lib/active_record/store.rb', line 180

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