Class: ColorBundle

Inherits:
Object
  • Object
show all
Defined in:
lib/punkmaker/colors.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(recs) ⇒ ColorBundle

Returns a new instance of ColorBundle.



19
20
21
22
# File 'lib/punkmaker/colors.rb', line 19

def initialize( recs )
    @recs = recs
    @colors_by_key = _build_colors_by_key( @recs )
end

Class Method Details

.normalize_key(str) ⇒ Object



12
13
14
15
# File 'lib/punkmaker/colors.rb', line 12

def self.normalize_key( str )
    ## remove all non-alphanum chars (a-z,0-9)
    str.downcase.gsub(/[^a-z0-9]/, '').strip
end

.read(*paths) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/punkmaker/colors.rb', line 4

def self.read( *paths )
    recs = []
    paths.each do |path|
        recs += read_csv( path )
    end
    new( recs )
end

Instance Method Details

#_build_colors_by_key(recs) ⇒ Object

helpers



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/punkmaker/colors.rb', line 37

def _build_colors_by_key( recs )
   h = {}
   recs.each_with_index do |rec|

     color = Color.from_hex( rec['color'] )
     names = (rec['names'] || '').split( '|' )
     
     names.each do |name|
       key = normalize_key( name )

       if h[ key ]
         puts "!!! ERROR - color name is not unique:"
         pp rec
         puts "duplicate:"
         pp h[key]
         exit 1
       end
       h[ key ] = color
     end
  end
   h
end

#_find(name) ⇒ Object Also known as: []



31
# File 'lib/punkmaker/colors.rb', line 31

def _find( name )  find_by( name: name ); end

#find_by(name:) ⇒ Object



26
27
28
29
# File 'lib/punkmaker/colors.rb', line 26

def find_by( name: )
   key = normalize_key( name )  ## normalize q(uery) string/symbol
   @colors_by_key[ key ]
end

#normalize_key(str) ⇒ Object



16
# File 'lib/punkmaker/colors.rb', line 16

def normalize_key( str )  self.class.normalize_key( str ); end

#recordsObject



24
# File 'lib/punkmaker/colors.rb', line 24

def records() @recs; end