Class: DeepFreezer::Base

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Base

Returns a new instance of Base.



7
8
9
# File 'lib/deep_freezer/base.rb', line 7

def initialize(obj)
  @obj = obj
end

Class Attribute Details

.attrsObject

Returns the value of attribute attrs.



4
5
6
# File 'lib/deep_freezer/base.rb', line 4

def attrs
  @attrs
end

.fixture_pathObject

Returns the value of attribute fixture_path.



4
5
6
# File 'lib/deep_freezer/base.rb', line 4

def fixture_path
  @fixture_path
end

.model(model) ⇒ Object

Returns the value of attribute model.



4
5
6
# File 'lib/deep_freezer/base.rb', line 4

def model
  @model
end

Class Method Details

.freeze(*attrs) ⇒ Object



11
12
13
# File 'lib/deep_freezer/base.rb', line 11

def self.freeze(*attrs)
  @attrs = attrs
end

.reset!Object



34
35
36
37
# File 'lib/deep_freezer/base.rb', line 34

def self.reset!
  path = DeepFreezer::Base.fixture_path.to_s + "/*.yml"
  Dir.glob(path).each { |file| File.delete(file) }
end

Instance Method Details

#freezeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deep_freezer/base.rb', line 19

def freeze
  freezable = @obj.class.new
  self.class.attrs.each do |attr|
    if self.respond_to?(attr)
      freezable.send("#{attr}=", self.send(attr))
    else
      freezable.send("#{attr}=", @obj.send(attr))
    end
  end

  yaml = ([{ "#{freezable.class.to_s}" => freezable.attributes }].to_yaml).gsub("---", "")

  write_to_file yaml, freezable.class.to_s.tableize
end