Class: Separatum::Exporters::ActiveRecordCode

Inherits:
Object
  • Object
show all
Defined in:
lib/separatum/exporters/active_record_code.rb

Constant Summary collapse

T_TRANSACTION =
File.expand_path(File.join(__FILE__, %w(.. active_record_code transaction.rb.erb)))
T_OBJECT =
File.expand_path(File.join(__FILE__, %w(.. active_record_code object.rb.erb)))
T_ATTRIBUTE =
File.expand_path(File.join(__FILE__, %w(.. active_record_code attribute.rb.erb)))
T_PROGRAM =
File.expand_path(File.join(__FILE__, %w(.. active_record_code program.rb.erb)))

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**params) ⇒ ActiveRecordCode

Returns a new instance of ActiveRecordCode.



11
12
13
14
15
# File 'lib/separatum/exporters/active_record_code.rb', line 11

def initialize(**params)
  @file_name = params[:file_name]
  @ignore_not_unique_classes = params[:ignore_not_unique_classes] || []
  @time_machine = params[:time_machine]
end

Instance Attribute Details

#attributes_strObject (readonly)

Returns the value of attribute attributes_str.



4
5
6
# File 'lib/separatum/exporters/active_record_code.rb', line 4

def attributes_str
  @attributes_str
end

#ignore_not_unique_classesObject (readonly)

Returns the value of attribute ignore_not_unique_classes.



4
5
6
# File 'lib/separatum/exporters/active_record_code.rb', line 4

def ignore_not_unique_classes
  @ignore_not_unique_classes
end

#key_strObject (readonly)

Returns the value of attribute key_str.



4
5
6
# File 'lib/separatum/exporters/active_record_code.rb', line 4

def key_str
  @key_str
end

#objects_strObject (readonly)

Returns the value of attribute objects_str.



4
5
6
# File 'lib/separatum/exporters/active_record_code.rb', line 4

def objects_str
  @objects_str
end

#transactions_strObject (readonly)

Returns the value of attribute transactions_str.



4
5
6
# File 'lib/separatum/exporters/active_record_code.rb', line 4

def transactions_str
  @transactions_str
end

#value_strObject (readonly)

Returns the value of attribute value_str.



4
5
6
# File 'lib/separatum/exporters/active_record_code.rb', line 4

def value_str
  @value_str
end

Instance Method Details

#call(*hashes) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/separatum/exporters/active_record_code.rb', line 17

def call(*hashes)
  objects = ::Separatum::Converters::Hash2Object.new.(hashes.flatten)
  @objects_str = objects.map do |object|
    @attributes_str = object.attributes.map do |key, value|
      @key_str = key
      @value_str = value_to_code(value)
      ERB.new(File.read(T_ATTRIBUTE)).result(binding).strip
    end
    ERB.new(File.read(T_OBJECT)).result(binding).strip
  end
  @transactions_str = [ERB.new(File.read(T_TRANSACTION)).result(binding).strip]
  script = ERB.new(File.read(T_PROGRAM)).result(binding).strip

  if @file_name
    File.write(@file_name, script)
  end

  script
end

#value_to_code(value) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/separatum/exporters/active_record_code.rb', line 37

def value_to_code(value)
  if defined?(ActiveSupport::TimeWithZone) && value.is_a?(ActiveSupport::TimeWithZone)
    value.to_s.dump
  elsif defined?(Date) && value.is_a?(Date)
    value.to_s.dump
  elsif value.is_a?(String)
    value.dump
  else
    value.inspect
  end
end