Class: ActiveRecordYAMLSerializer

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

Instance Method Summary collapse

Constructor Details

#initialize(obj, params) ⇒ ActiveRecordYAMLSerializer

Returns a new instance of ActiveRecordYAMLSerializer.



7
8
9
10
11
12
# File 'lib/active_record_helpers.rb', line 7

def initialize(obj, params)
  @obj = obj
  @params = params.symbolize_keys!
  @contents = ''
  @indent_level = 0
end

Instance Method Details

#get_yamlObject



32
33
34
# File 'lib/active_record_helpers.rb', line 32

def get_yaml
  YAML::load( @contents )
end

#save_as(file_name) ⇒ Object



36
37
38
39
40
# File 'lib/active_record_helpers.rb', line 36

def save_as(file_name)
  File.open(file_name, 'w') do |f|
    f.write @contents
  end
end

#serializeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record_helpers.rb', line 14

def serialize
  @contents = ''
  @contents += serialize_root_values(@params)

  if @obj.is_a? ActiveRecord::Base
    @contents += serialize_record(@obj, false, @params) #asnotarray
  elsif @obj.is_a? Array
    @contents += "items:\n"
    @obj.each do |item|
      reset_indent
      indent
      @contents += serialize_record(item, true, @params)  #asarray
    end
  end


end