Class: Palette

Inherits:
Object
  • Object
show all
Defined in:
lib/dunmanifestin/palette.rb

Constant Summary collapse

GAP_BETWEEN_LISTS =
/\n(?=\|)/
PALETTE_TITLE =
/^\|(.*?)\n/
COMMENT =
/\/\/(.*?)\n/

Class Method Summary collapse

Class Method Details

.declare_phrase_class(list_name) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/dunmanifestin/palette.rb', line 33

def declare_phrase_class list_name
  name = list_name.underscore.camelize
  qlass = "Phrase::#{name}".constantize
rescue NameError
  # This seems to always happen.
  qlass = Class.new(Phrase)
  Phrase.const_set name, qlass
end

.expose(path) ⇒ Object



6
7
8
9
10
# File 'lib/dunmanifestin/palette.rb', line 6

def expose path
  File.open(path).read
    .split(GAP_BETWEEN_LISTS)
    .each &method(:expose_swatches)
end

.expose_swatches(body) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dunmanifestin/palette.rb', line 12

def expose_swatches body
  body.gsub!(COMMENT, '')
  list_name = body.match(PALETTE_TITLE)[1].to_s

  if list_name.empty?
    # This seems to support palettes without |explicitTitles,
    #  Taking them from the filename, instead.
    list_name = Pathname.new(path).basename
  else
    # Do not include palette title as a swatch
    # These comments should be tests.
    body.gsub!(PALETTE_TITLE, '')
  end

  # This metaprogramming is mysterious to me.
  qlass = declare_phrase_class list_name
  # Where is 'list' defined?
  qlass.list body
rescue NoMethodError
end