spectra 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ee1b6454f86dcc042a8b03acd6cf8660aa6baa86
4
- data.tar.gz: 970a196f54c7562ed965fd13bf551b5eeb3f77e6
3
+ metadata.gz: 570f1ad695aa5a7555900c3d6fa42e00bb6bee17
4
+ data.tar.gz: eee2b8f331250c0389a6599c98c62c0b5d4a48ee
5
5
  SHA512:
6
- metadata.gz: fbdcb8d68ee9cc39eee38d0df5085da70973af302e0ba8feee1e8f7ff66dde156a6b1bb9411d0b37f8d51e1f994b6c83281be83b029d6556537885ec9a0960df
7
- data.tar.gz: b86738e123ee3d95ade7f5ceab28bd013bbea8a7d034b05078cdf48a41dd7658bb2bf2d8a9bebadaad5031164037055879fc8a72993a590d116144add2aa5e32
6
+ metadata.gz: 099a09753a9ad6f0dbb093dffa5fb1a6da18d1743a6dd4ac0a307f9e29323dba68b28c56e0b7e6f95b93aa02e60d64ee51c1783d5765f6d44b15e67c9625ebae
7
+ data.tar.gz: 4ac07c5bbd1cd9c71d7c0b8588bc6d74e7706f99d1f7d8d9aad60ab358852067354498c3a5cb4e50eca590d7439e85a920c613b290d2183de148f55d063837fd
data/.gitignore CHANGED
@@ -20,8 +20,7 @@ tmp
20
20
  *.o
21
21
  *.a
22
22
  mkmf.log
23
+ *.swp
23
24
 
24
- spectrum.rb
25
25
  .ruby-version
26
26
  .ruby-gemset
27
-
data/README.md CHANGED
@@ -40,3 +40,16 @@ color :gray, hex: 0xEEEEEE, a: 0.6
40
40
  color :white, w: 1.0
41
41
  color :overlay, r: 0.8, g: 0.7, b: 0.2, a: 0.75
42
42
  ```
43
+
44
+ ## Installation
45
+
46
+ Spectra is published as a Rubygem. Install with `gem`:
47
+ ```ruby
48
+ gem install spectra
49
+ ```
50
+
51
+ Or `bundler`:
52
+ ```ruby
53
+ gem 'spectra', '~> 0.1.1'
54
+ ```
55
+
data/bin/spectra CHANGED
@@ -4,25 +4,63 @@ require 'spectra'
4
4
  require 'ostruct'
5
5
  require 'optparse'
6
6
 
7
+ command_list = %Q(
8
+ subcommands:
9
+ + create: creates a template spectrum.rb file
10
+ )
11
+
12
+ command_descriptions = {
13
+ base: %Q(
14
+ Invoke without a subcommand to generate colors from the spectrum.rb in the current directory
15
+ ),
16
+ create: %Q(
17
+ Creates a template spectrum.rb file populated with instructional defualts in the current directory.
18
+ )}
19
+
7
20
  options = OpenStruct.new
8
- options.verbose = false
9
21
 
10
- OptionParser.new do |opts|
11
- opts.banner = 'usage: spectra [-vh]'
12
-
22
+ main = OptionParser.new do |opts|
23
+ opts.banner = 'usage: spectra [options] [subcommand [options]]'
24
+
25
+ opts.separator command_descriptions[:base][1..-1]
26
+ opts.separator "\noptions:"
27
+
13
28
  opts.on('-v', '--verbose', 'Enables verbose debugging output') do |flag|
14
- options.verbose = true
29
+ options.verbose = flag
15
30
  end
16
31
 
17
32
  opts.on('-d', '--dry-run', 'Processes colors without writing anything to disk') do |flag|
18
- options.dry_run = true
33
+ options.dry_run = flag
19
34
  end
20
35
 
21
- opts.on('-h', '--help', 'Shows this message') do |flag|
36
+ opts.on('-h', '--help', 'Shows this message') do
22
37
  puts opts; exit
23
38
  end
24
39
 
25
- end.parse!(ARGV)
40
+ opts.separator command_list
41
+
42
+ end
43
+
44
+ subcommands = {
45
+ create: OptionParser.new do |opts|
46
+ opts.banner = 'usage: create'
47
+ opts.separator command_descriptions[:create][1..-1]
48
+
49
+ opts.separator "\noptions"
50
+ opts.on('-h', '--help', 'Shows this message') do
51
+ puts opts; exit
52
+ end
53
+ end
54
+ }
55
+
56
+ main.order! # order parses all possible arguments desctructively (in order, duh)
57
+ subcommand = ARGV.shift.intern
58
+ subcommands[subcommand].order! if subcommand
26
59
 
27
- Spectra.generate(options)
60
+ case subcommand
61
+ when :create
62
+ Spectra.create(options);
63
+ else
64
+ Spectra.generate(options)
65
+ end
28
66
 
@@ -0,0 +1,35 @@
1
+
2
+ require 'spectra/components'
3
+ require 'spectra/utilities/extensions'
4
+
5
+ module Spectra
6
+
7
+ class Color
8
+
9
+ attr_accessor :name, :components
10
+
11
+ def initialize(name, components)
12
+ self.name = name
13
+ self.components = components
14
+ end
15
+
16
+ def respond_to?(name)
17
+ super || Components.valid?(name)
18
+ end
19
+
20
+ def method_missing(name)
21
+ Components.valid?(name) ? self.components[name] : super
22
+ end
23
+
24
+ ##
25
+ ## Debugging
26
+ ##
27
+
28
+ def to_s
29
+ "<color: #{name} colors: #{components.values_at(:red, :green, :blue, :alpha).map{|v| '%.2f' % (v || 0.0) }}>"
30
+ end
31
+
32
+ end
33
+
34
+ end
35
+
@@ -20,6 +20,10 @@ module Spectra
20
20
  components.each { |key, value| components[key] = self.normalize(value) }
21
21
  end
22
22
 
23
+ def self.hexify(components)
24
+ (components[:red] * 255 << 16) | (components[:green] * 255 << 8) | (components[:blue] * 255)
25
+ end
26
+
23
27
  def self.valid?(name)
24
28
  self.valid.include?(name)
25
29
  end
@@ -28,17 +32,12 @@ module Spectra
28
32
  ## Helpers
29
33
  ##
30
34
 
31
- def self.componentize_hex(value)
32
- hex = value.is_a?(String) ? value.to_i : value
33
- Hash.new.tap do |hash|
34
- hash[:red] = (hex & 0xFF0000) >> 16
35
- hash[:green] = (hex & 0x00FF00) >> 8
36
- hash[:blue] = (hex & 0x0000FF)
37
- end
35
+ def self.componentize_hex(hex)
36
+ { red: (hex & 0xFF0000) >> 16, green: (hex & 0x00FF00) >> 8, blue: (hex & 0x0000FF) }
38
37
  end
39
38
 
40
- def self.componentize_white(value)
41
- { red: value, green: value, blue: value }
39
+ def self.componentize_white(white)
40
+ { red: white, green: white, blue: white }
42
41
  end
43
42
 
44
43
  ##
@@ -0,0 +1,57 @@
1
+
2
+ require 'spectra/color'
3
+ require 'spectra/views/factory'
4
+
5
+ module Spectra
6
+ class Spectrum
7
+
8
+ attr_accessor :_prefix
9
+ attr_accessor :colors, :views
10
+
11
+ def generate(definition)
12
+ # evaluate color definition file, setup defaults
13
+ self.instance_eval(definition)
14
+ self.formats(:palette, :objc) unless self.views
15
+
16
+ # render views according to the specification
17
+ self.views.each do |view|
18
+ view.serialize({ colors: self.colors, prefix: self._prefix })
19
+ end
20
+ end
21
+
22
+ ##
23
+ ## DSL
24
+ ##
25
+
26
+ def prefix(prefix)
27
+ self._prefix = prefix
28
+ end
29
+
30
+ def formats(*types)
31
+ types.each { |type| format(type) }
32
+ end
33
+
34
+ def format(type, directory = nil, &renamer)
35
+ self.views ||= []
36
+ self.views.concat(Views.from_attributes({ type: type, directory: directory, renamer: renamer }))
37
+ end
38
+
39
+ def color(name, attributes)
40
+ self.colors ||= []
41
+ self.colors << Color.new(name, Components.componentize(attributes))
42
+ end
43
+
44
+ def components(*components)
45
+ components.hash_from(:red, :green, :blue, :alpha)
46
+ end
47
+
48
+ def hex(*components)
49
+ components.hash_from(:hex, :alpha)
50
+ end
51
+
52
+ def white(*components)
53
+ components.hash_from(:white, :alpha)
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,24 @@
1
+
2
+ ## replace with your desired class/method prefix (used where applicable)
3
+ prefix :foo
4
+
5
+ ## Formats
6
+
7
+ ## specify formats with `format` or `formats`
8
+ format :palette
9
+
10
+ ## replace path with your own personal category directory
11
+ format :objc, 'path/to/categories'
12
+
13
+ ## uncomment to customize the naming scheme for a particulary format
14
+ # format :objc do |name, prefix|
15
+ # name.camelize(true)
16
+ # end
17
+
18
+ ## Colors
19
+
20
+ ## note: the last component is always alpha, and can safely be omitted
21
+ color :red, (components 255, 0, 0, 1.0)
22
+ color :gray, (white 0.5, 1.0)
23
+ color :green, (hex 0x00ff00, 1.0)
24
+
File without changes
@@ -1,3 +1,3 @@
1
1
  module Spectra
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -0,0 +1,28 @@
1
+
2
+ require 'spectra/views/palette'
3
+ require 'spectra/views/objc_category'
4
+ require 'spectra/views/swift_extension'
5
+
6
+ module Spectra
7
+
8
+ module Views
9
+
10
+ def self.from_attributes(attributes)
11
+ klass = self.view_class(attributes[:type])
12
+ klass.from_attributes(attributes)
13
+ end
14
+
15
+ def self.view_class(type)
16
+ case type.intern
17
+ when :palette
18
+ Palette
19
+ when :objc
20
+ ObjcCategory
21
+ when :swift
22
+ SwiftExtension
23
+ end
24
+ end
25
+
26
+ end
27
+
28
+ end
@@ -1,12 +1,16 @@
1
1
 
2
- require 'spectra/templates/template'
2
+ require 'spectra/views/view'
3
3
 
4
4
  module Spectra
5
5
 
6
- class ObjcCategory < Template
6
+ class ObjcCategory < View
7
7
 
8
8
  attr_accessor :is_header
9
9
 
10
+ def self.from_attributes(attributes)
11
+ [ self.new(attributes.merge(is_header: true)), self.new(attributes) ]
12
+ end
13
+
10
14
  def initialize(attributes)
11
15
  super
12
16
  self.is_header = attributes[:is_header]
@@ -49,7 +53,7 @@ module Spectra
49
53
  ##
50
54
 
51
55
  def filename
52
- "UIColor+#{self.class_prefix}Color.#{self.file_extension}"
56
+ super || "UIColor+#{self.class_prefix}Color.#{self.file_extension}"
53
57
  end
54
58
 
55
59
  end
File without changes
@@ -0,0 +1,27 @@
1
+
2
+ require 'spectra/views/view'
3
+
4
+ module Spectra
5
+
6
+ class Palette < View
7
+
8
+ def renamer
9
+ @renamer || lambda { |name, prefix| name.camelize(true) }
10
+ end
11
+
12
+ ##
13
+ ## Pathing
14
+ ##
15
+
16
+ def directory
17
+ @directory || "#{Dir.home}/Library/Colors/"
18
+ end
19
+
20
+ def filename
21
+ super || "#{self.prefix}-palette.clr"
22
+ end
23
+
24
+ end
25
+
26
+ end
27
+
@@ -0,0 +1,34 @@
1
+
2
+ module Spectra
3
+
4
+ module Serializable
5
+
6
+ attr_accessor :directory, :filename
7
+
8
+ def serialize(attributes)
9
+ text = self.render(attributes)
10
+ path = self.destination(attributes)
11
+
12
+ Spectra.logger.debug "#{Spectra.options.dry_run ? 'would write' : 'writing'} to: #{path}"
13
+
14
+ if Spectra.options.dry_run
15
+ Spectra.logger.debug "\n#{text}"
16
+ else
17
+ File.open(path, 'w+') { |file| file.write(text) }
18
+ end
19
+ end
20
+
21
+ def destination(attributes)
22
+ destination = self.directory
23
+ destination << '/' unless destination.end_with?('/')
24
+ destination + self.filename
25
+ end
26
+
27
+ def directory
28
+ @directory ||= './'
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
@@ -0,0 +1,23 @@
1
+
2
+ require 'spectra/views/view'
3
+
4
+ module Spectra
5
+
6
+ class SwiftExtension < View
7
+
8
+ def renamer
9
+ @renamer || lambda { |name, prefix| name.camelize(true) }
10
+ end
11
+
12
+ ##
13
+ ## Pathing
14
+ ##
15
+
16
+ def filename
17
+ super || 'Colors.swift'
18
+ end
19
+
20
+ end
21
+
22
+ end
23
+
@@ -0,0 +1,120 @@
1
+
2
+ require 'mustache'
3
+ require 'spectra/components'
4
+ require 'spectra/utilities/extensions'
5
+ require 'spectra/views/serializable'
6
+
7
+ module Spectra
8
+
9
+ class View < Mustache
10
+
11
+ include Serializable
12
+
13
+ attr_accessor :colors, :renamer, :prefix
14
+
15
+ def initialize(attributes)
16
+ self.directory = attributes[:directory]
17
+ self.filename = attributes[:filename]
18
+ self.renamer = attributes[:renamer]
19
+ end
20
+
21
+ def self.from_attributes(attributes)
22
+ [ self.new(attributes) ]
23
+ end
24
+
25
+ ##
26
+ ## Mustache
27
+ ##
28
+
29
+ self.path = File.dirname(__FILE__)
30
+
31
+ def self.template_name
32
+ super.split('/').last
33
+ end
34
+
35
+ ##
36
+ ## Rendering
37
+ ##
38
+
39
+ def render(attributes)
40
+ self.prefix = attributes[:prefix]
41
+ self.colors = self.views_from_colors(attributes[:colors])
42
+ super() # offload to Mustache
43
+ end
44
+
45
+ def views_from_colors(colors)
46
+ colors.map do |color|
47
+ ColorView.new(color, {
48
+ name: proc { |c| self.format_color_name(c) },
49
+ value: proc { |v| self.format_color_value(v) }
50
+ })
51
+ end
52
+ end
53
+
54
+ def format_color_name(name)
55
+ self.renamer.call(name, self.prefix)
56
+ end
57
+
58
+ def format_color_value(value)
59
+ '%.2f' % (value || 0.0)
60
+ end
61
+
62
+ ##
63
+ ## Debugging
64
+ ##
65
+
66
+ def to_s
67
+ "<#{self.class.name} => #{self.path}, renamer: #{@renamer}>"
68
+ end
69
+
70
+ end
71
+
72
+ class ColorView
73
+
74
+ attr_accessor :color, :formatters
75
+
76
+ def initialize(color, formatters)
77
+ self.color = color
78
+ self.formatters = formatters
79
+ end
80
+
81
+ def respond_to?(name)
82
+ super || self.color.respond_to?(name)
83
+ end
84
+
85
+ def method_missing(name, *args)
86
+ if Components.valid?(name)
87
+ self.format(:value, self.color.send(name, *args))
88
+ else
89
+ super
90
+ end
91
+ end
92
+
93
+ ##
94
+ ## Accessors
95
+ ##
96
+
97
+ def name
98
+ self.format(:name, self.color.name)
99
+ end
100
+
101
+ def hex
102
+ self.format(:value, Components.hexify(self.color.components))
103
+ end
104
+
105
+ def format(type, value)
106
+ self.formatters[type].call(value)
107
+ end
108
+
109
+ ##
110
+ ## Debugging
111
+ ##
112
+
113
+ def to_s
114
+ "<view => #{self.color}>"
115
+ end
116
+
117
+ end
118
+
119
+ end
120
+
data/lib/spectra.rb CHANGED
@@ -1,17 +1,16 @@
1
1
 
2
2
  require 'spectra/version'
3
- require 'spectra/logger'
4
- require 'spectra/models'
3
+ require 'spectra/spectrum'
4
+ require 'spectra/utilities/logger'
5
5
 
6
6
  module Spectra
7
7
 
8
8
  class << self
9
- attr_accessor :logger, :options
9
+ attr_accessor :logger, :options
10
10
  end
11
11
 
12
12
  def self.generate(options)
13
13
  self.options = options
14
- logger.level = options.verbose ? Logger::DEBUG : Logger::INFO
15
14
 
16
15
  begin
17
16
  definition = IO.read('spectrum.rb')
@@ -19,13 +18,30 @@ module Spectra
19
18
  logger.terminate "Failed to read spectrum.rb file: #{execption}"
20
19
  end
21
20
 
22
- spectra = Root.new
23
- spectra.generate(definition)
21
+ spectrum = Spectrum.new
22
+ spectrum.generate(definition)
24
23
  end
25
24
 
25
+ def self.create(options)
26
+ self.options = options
27
+
28
+ begin
29
+ IO.copy_stream(File.dirname(__FILE__) + '/spectra/template.rb', 'spectrum.rb')
30
+ rescue Exception => exception
31
+ logger.terminate "Failed to create spectrum.rb file: #{exception}"
32
+ else
33
+ logger.info "created #{Dir.pwd}/spectrum.rb"
34
+ end
35
+ end
36
+
26
37
  def self.logger
27
38
  @logger ||= SpectraLogger.new(STDOUT)
28
39
  end
29
40
 
41
+ def options=(options)
42
+ @options = options
43
+ self.logger.level = options.verbose ? Logger::DEBUG : Logger::INFO
44
+ end
45
+
30
46
  end
31
47
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spectra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ty Cobb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-03 00:00:00.000000000 Z
11
+ date: 2014-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -68,21 +68,22 @@ files:
68
68
  - Rakefile
69
69
  - bin/spectra
70
70
  - lib/spectra.rb
71
+ - lib/spectra/color.rb
71
72
  - lib/spectra/components.rb
72
- - lib/spectra/dsl.rb
73
- - lib/spectra/extensions.rb
74
- - lib/spectra/logger.rb
75
- - lib/spectra/models.rb
76
- - lib/spectra/serializer.rb
77
- - lib/spectra/templates/factory.rb
78
- - lib/spectra/templates/objc_category.mustache
79
- - lib/spectra/templates/objc_category.rb
80
- - lib/spectra/templates/palette.mustache
81
- - lib/spectra/templates/palette.rb
82
- - lib/spectra/templates/swift_extension.mustache
83
- - lib/spectra/templates/swift_extension.rb
84
- - lib/spectra/templates/template.rb
73
+ - lib/spectra/spectrum.rb
74
+ - lib/spectra/template.rb
75
+ - lib/spectra/utilities/extensions.rb
76
+ - lib/spectra/utilities/logger.rb
85
77
  - lib/spectra/version.rb
78
+ - lib/spectra/views/factory.rb
79
+ - lib/spectra/views/objc_category.mustache
80
+ - lib/spectra/views/objc_category.rb
81
+ - lib/spectra/views/palette.mustache
82
+ - lib/spectra/views/palette.rb
83
+ - lib/spectra/views/serializable.rb
84
+ - lib/spectra/views/swift_extension.mustache
85
+ - lib/spectra/views/swift_extension.rb
86
+ - lib/spectra/views/view.rb
86
87
  - spectra.gemspec
87
88
  homepage: https://github.com/derkis/Spectrum
88
89
  licenses:
data/lib/spectra/dsl.rb DELETED
@@ -1,52 +0,0 @@
1
-
2
- require 'spectra/components'
3
- require 'spectra/serializer'
4
-
5
- module Spectra
6
-
7
- module DSL
8
- def prefix(prefix)
9
- self._prefix = prefix
10
- end
11
-
12
- def formats(*types)
13
- types.each { |type| format(type) }
14
- end
15
-
16
- def format(type, path = nil, &renamer)
17
- self.serializers ||= []
18
-
19
- serializers = Serializer.from_type(type, {
20
- path: path,
21
- template: { type: type, renamer: renamer }
22
- })
23
- self.serializers.concat(serializers)
24
-
25
- serializers.each { |serializer| Spectra.logger.debug "dsl add: #{serializer}" }
26
- end
27
-
28
- def color(name, attributes)
29
- self.colors ||= []
30
-
31
- color = Color.new(name, Components.componentize(attributes))
32
- self.colors << color
33
-
34
- Spectra.logger.debug "dsl add: #{color}"
35
- end
36
-
37
- def components(*components)
38
- components.hash_from(:red, :green, :blue, :alpha)
39
- end
40
-
41
- def hex(*components)
42
- components.hash_from(:hex, :alpha)
43
- end
44
-
45
- def white(*components)
46
- components.hash_from(:white, :alpha)
47
- end
48
-
49
- end
50
-
51
- end
52
-
@@ -1,57 +0,0 @@
1
-
2
- require 'spectra/extensions'
3
- require 'spectra/components'
4
- require 'spectra/dsl'
5
-
6
- module Spectra
7
-
8
- class Root
9
-
10
- attr_accessor :_prefix
11
- attr_accessor :colors, :serializers
12
-
13
- include DSL
14
-
15
- def generate(definition)
16
- self.instance_eval(definition)
17
- self.formats(:palette, :objc) unless self.serializers
18
-
19
- self.serializers.each do |serializer|
20
- serializer.serialize({
21
- colors: self.colors,
22
- prefix: self._prefix,
23
- })
24
- end
25
- end
26
-
27
- end
28
-
29
- class Color
30
-
31
- attr_accessor :name, :components
32
-
33
- def initialize(name, components)
34
- self.name = name
35
- self.components = components
36
- end
37
-
38
- def respond_to?(name)
39
- super || Components.valid?(name)
40
- end
41
-
42
- def method_missing(name)
43
- Components.valid?(name) ? self.components[name] : super
44
- end
45
-
46
- ##
47
- ## Debugging
48
- ##
49
-
50
- def to_s
51
- "<color: #{name} colors: #{components.values_at(:red, :green, :blue, :alpha).map{|v| v.is_a?(String) ? v : '%.2f' % (v || 0.0) }}>"
52
- end
53
-
54
- end
55
-
56
- end
57
-
@@ -1,60 +0,0 @@
1
-
2
- require 'spectra/extensions'
3
- require 'spectra/templates/factory'
4
-
5
- module Spectra
6
-
7
- class Serializer
8
-
9
- attr_accessor :template, :path
10
-
11
- def initialize(attributes)
12
- self.path = attributes[:path]
13
- self.template = Template.from_attributes(attributes[:template])
14
- end
15
-
16
- def serialize(attributes)
17
- text = self.template.render(attributes)
18
- path = self.resource_path(attributes)
19
-
20
- Spectra.logger.debug "#{Spectra.options.dry_run ? 'would write' : 'writing'} to: #{path}"
21
-
22
- if Spectra.options.dry_run
23
- Spectra.logger.debug "\n#{text}"
24
- else
25
- File.open(path, 'w+') { |file| file.write(text) }
26
- end
27
- end
28
-
29
- def resource_path(attributes)
30
- resource_path = self.path || self.template.path
31
- resource_path << '/' unless resource_path.end_with?('/')
32
- resource_path + self.template.filename
33
- end
34
-
35
- ##
36
- ## Factory
37
- ##
38
-
39
- def self.from_type(type, attributes)
40
- case type.intern
41
- when :palette, :swift
42
- [ Serializer.new(attributes) ]
43
- when :objc
44
- [ Serializer.new(attributes.deep_merge(template: { is_header: true })), Serializer.new(attributes) ]
45
- else raise "Specfied an invalid serializer type: #{type}"
46
- end
47
- end
48
-
49
- ##
50
- ## Debugging
51
- ##
52
-
53
- def to_s
54
- "<serializer => #{self.path || './'}, #{self.template}>"
55
- end
56
-
57
- end
58
-
59
- end
60
-
@@ -1,23 +0,0 @@
1
-
2
- require 'spectra/templates/palette'
3
- require 'spectra/templates/objc_category'
4
- require 'spectra/templates/swift_extension'
5
-
6
- module Spectra
7
-
8
- class Template
9
-
10
- def self.from_attributes(attributes)
11
- case attributes[:type].intern
12
- when :palette
13
- Palette.new(attributes)
14
- when :objc
15
- ObjcCategory.new(attributes)
16
- when :swift
17
- SwiftExtension.new(attributes)
18
- end
19
- end
20
-
21
- end
22
-
23
- end
@@ -1,27 +0,0 @@
1
-
2
- require 'spectra/templates/template'
3
-
4
- module Spectra
5
-
6
- class Palette < Template
7
-
8
- def renamer
9
- @renamer ||= lambda { |name, prefix| name.camelize(true) }
10
- end
11
-
12
- ##
13
- ## Pathing
14
- ##
15
-
16
- def path
17
- "#{Dir.home}/Library/Colors/"
18
- end
19
-
20
- def filename
21
- "#{self.prefix}-palette.clr"
22
- end
23
-
24
- end
25
-
26
- end
27
-
@@ -1,23 +0,0 @@
1
-
2
- require 'spectra/templates/template'
3
-
4
- module Spectra
5
-
6
- class SwiftExtension < Template
7
-
8
- def renamer
9
- @renamer ||= lambda { |name, prefix| name.camelize(true) }
10
- end
11
-
12
- ##
13
- ## Pathing
14
- ##
15
-
16
- def filename
17
- return 'Colors.swift'
18
- end
19
-
20
- end
21
-
22
- end
23
-
@@ -1,73 +0,0 @@
1
-
2
- require 'mustache'
3
-
4
- module Spectra
5
-
6
- class Template < Mustache
7
-
8
- attr_accessor :renamer, :colors, :prefix
9
-
10
- def initialize(attributes)
11
- self.renamer = attributes[:renamer]
12
- end
13
-
14
- ##
15
- ## Mustache
16
- ##
17
-
18
- self.path = 'lib/spectra/templates'
19
-
20
- def self.template_name
21
- super.split('/').last
22
- end
23
-
24
- ##
25
- ## Rendering
26
- ##
27
-
28
- def render(attributes)
29
- self.prefix = attributes[:prefix]
30
- self.colors = parse_colors(attributes[:colors])
31
- super() # offload to Mustache
32
- end
33
-
34
- def parse_colors(colors)
35
- colors.map { |color| self.parse_color(color) }
36
- end
37
-
38
- def parse_color(color)
39
- copy = color.clone
40
- copy.name = self.format_color_name(color)
41
- copy.components = color.components.map do |key, value|
42
- [ key, self.format_color_value(value) ]
43
- end.to_h
44
- copy
45
- end
46
-
47
- def format_color_name(color)
48
- self.renamer.call(color.name, self.prefix)
49
- end
50
-
51
- def format_color_value(value)
52
- '%.2f' % (value || 0.0)
53
- end
54
-
55
- ##
56
- ## Pathing
57
- ##
58
-
59
- def path
60
- './'
61
- end
62
-
63
- ##
64
- ## Debugging
65
- ##
66
-
67
- def to_s
68
- "<template: #{self.class.name} renamer: #{@renamer}>"
69
- end
70
-
71
- end
72
-
73
- end