Module: Spectra::Serializable

Included in:
View
Defined in:
lib/spectra/views/serializable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



6
7
8
# File 'lib/spectra/views/serializable.rb', line 6

def directory
  @directory
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/spectra/views/serializable.rb', line 6

def filename
  @filename
end

Instance Method Details

#destination(attributes) ⇒ Object



32
33
34
35
36
# File 'lib/spectra/views/serializable.rb', line 32

def destination(attributes)
  destination = self.directory
  destination << '/' unless destination.end_with?('/')
  destination + self.filename
end

#serialize(attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/spectra/views/serializable.rb', line 8

def serialize(attributes)
  text = self.render(attributes)
  path = self.destination(attributes)

  begin
    file = File.open(path, 'w+')
  rescue Exception  
    raise Informative, "Couldn't open #{path}"
  else
    self.write_file(file, text, path)
  ensure
    file.close unless file.nil?
  end
end

#write_file(file, text, path) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/spectra/views/serializable.rb', line 23

def write_file(file, text, path)
  Spectra.logger.info "[✓] Generating #{path}"
  if Config.dry_run
    Spectra.logger.info "\n#{text.chomp}"
  else
    file.write(text)
  end
end