Class: Spectra::View

Inherits:
Mustache
  • Object
show all
Includes:
Serializable
Defined in:
lib/spectra/views/view.rb

Direct Known Subclasses

ObjcCategory, Palette, SwiftExtension

Instance Attribute Summary collapse

Attributes included from Serializable

#directory, #filename

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#destination, #serialize, #write_file

Constructor Details

#initialize(attributes) ⇒ View

Returns a new instance of View.



13
14
15
16
17
# File 'lib/spectra/views/view.rb', line 13

def initialize(attributes)
  self.directory = attributes[:directory]
  self.filename  = attributes[:filename]
  self.renamer   = attributes[:renamer]
end

Instance Attribute Details

#colorsObject

Returns the value of attribute colors.



11
12
13
# File 'lib/spectra/views/view.rb', line 11

def colors
  @colors
end

#prefixObject

Returns the value of attribute prefix.



11
12
13
# File 'lib/spectra/views/view.rb', line 11

def prefix
  @prefix
end

#renamerObject

Returns the value of attribute renamer.



11
12
13
# File 'lib/spectra/views/view.rb', line 11

def renamer
  @renamer
end

Class Method Details

.from_attributes(attributes) ⇒ Object



19
20
21
# File 'lib/spectra/views/view.rb', line 19

def self.from_attributes(attributes)
  [ self.new(attributes) ]
end

.template_nameObject



29
30
31
# File 'lib/spectra/views/view.rb', line 29

def self.template_name
   super.split('/').last
end

Instance Method Details

#format_color_name(color) ⇒ Object



52
53
54
# File 'lib/spectra/views/view.rb', line 52

def format_color_name(color)
  self.renamer.call(color, self.prefix)
end

#format_color_value(value) ⇒ Object



56
57
58
# File 'lib/spectra/views/view.rb', line 56

def format_color_value(value)
  '%.2f' % (value || 0.0)
end

#render(attributes) ⇒ Object

Rendering



37
38
39
40
41
# File 'lib/spectra/views/view.rb', line 37

def render(attributes)
  self.prefix = attributes[:prefix]
  self.colors = self.views_from_colors(attributes[:colors])
  super() # offload to Mustache
end

#to_sObject

Debugging



64
65
66
# File 'lib/spectra/views/view.rb', line 64

def to_s
  "<#{self.class.name} => #{self.path}, renamer: #{@renamer}>"
end

#views_from_colors(colors) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/spectra/views/view.rb', line 43

def views_from_colors(colors)
  colors.map do |color| 
    ColorView.new(color, {
      name:  proc { |c| self.format_color_name(c) },
      value: proc { |v| self.format_color_value(v) }
    }) 
  end
end