Class: Spectra::Serializer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Serializer

Returns a new instance of Serializer.



11
12
13
14
# File 'lib/spectra/serializer.rb', line 11

def initialize(attributes)
  self.path     = attributes[:path]
  self.template = Template.from_attributes(attributes[:template])
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/spectra/serializer.rb', line 9

def path
  @path
end

#templateObject

Returns the value of attribute template.



9
10
11
# File 'lib/spectra/serializer.rb', line 9

def template
  @template
end

Class Method Details

.from_type(type, attributes) ⇒ Object

Factory



39
40
41
42
43
44
45
46
47
# File 'lib/spectra/serializer.rb', line 39

def self.from_type(type, attributes)
  case type.intern
    when :palette, :swift
      [ Serializer.new(attributes) ]
    when :objc
      [ Serializer.new(attributes.deep_merge(template: { is_header: true })), Serializer.new(attributes) ]
    else raise "Specfied an invalid serializer type: #{type}"
  end
end

Instance Method Details

#resource_path(attributes) ⇒ Object



29
30
31
32
33
# File 'lib/spectra/serializer.rb', line 29

def resource_path(attributes)
  resource_path = self.path || self.template.path
  resource_path << '/' unless resource_path.end_with?('/')
  resource_path + self.template.filename
end

#serialize(attributes) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/spectra/serializer.rb', line 16

def serialize(attributes)
  text = self.template.render(attributes)
  path = self.resource_path(attributes)

  Spectra.logger.debug "#{Spectra.options.dry_run ? 'would write' : 'writing'} to: #{path}" 

  if Spectra.options.dry_run
    Spectra.logger.debug "\n#{text}"
  else
    File.open(path, 'w+') { |file| file.write(text) }
  end
end

#to_sObject

Debugging



53
54
55
# File 'lib/spectra/serializer.rb', line 53

def to_s
  "<serializer => #{self.path || './'}, #{self.template}>"
end