Class: StyleTrain::Theme

Inherits:
Object
  • Object
show all
Defined in:
lib/style_train/theme.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value_hash, palette = {}) ⇒ Theme

Returns a new instance of Theme.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
# File 'lib/style_train/theme.rb', line 33

def initialize(name, value_hash, palette = {})
  raise ArgumentError, "Unique name is required as first argument" if !name || self.class.themes[name]
  self.palette = palette
  self.value_hash = Gnash.new(self.class.defaults.merge(substitute(value_hash)))
  missing_keys = self.class.required_keys - self.value_hash.keys.map{|k| k.to_sym}
  raise ArgumentError, "Required keys not provided: #{missing_keys.map{|k| k.inspect}.join(", ")}" if !missing_keys.empty?
  self.name = name
  self.class.themes[name] = self
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/style_train/theme.rb', line 3

def name
  @name
end

#paletteObject

Returns the value of attribute palette.



3
4
5
# File 'lib/style_train/theme.rb', line 3

def palette
  @palette
end

#value_hashObject

Returns the value of attribute value_hash.



3
4
5
# File 'lib/style_train/theme.rb', line 3

def value_hash
  @value_hash
end

Class Method Details

.[](key) ⇒ Object



25
26
27
# File 'lib/style_train/theme.rb', line 25

def self.[](key)
  themes[key]
end

.defaults(hash = nil) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/style_train/theme.rb', line 17

def self.defaults hash=nil
  if hash
    @defaults = Gnash.new(hash)
  else
    @defaults || Gnash.new
  end
end

.keysObject



29
30
31
# File 'lib/style_train/theme.rb', line 29

def self.keys
  themes.keys.map{|k| k.to_sym}
end

.required_keys(*args) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/style_train/theme.rb', line 5

def self.required_keys *args
  if args.empty?
    @required_keys ||= []
  else
    @required_keys = args.map{ |v| v.to_sym }
  end
end

.themesObject



13
14
15
# File 'lib/style_train/theme.rb', line 13

def self.themes
  @themes ||= Gnash.new
end

Instance Method Details

#[](key) ⇒ Object



50
51
52
# File 'lib/style_train/theme.rb', line 50

def [](key)
  value_hash[key]
end

#[]=(key, value) ⇒ Object



54
55
56
57
58
59
# File 'lib/style_train/theme.rb', line 54

def []=(key, value)
  if (value.nil? || value.to_s.empty?) && self.class.required_keys.include?(key.to_sym)
    raise "Cannot set a required key to nothing"
  end
  value_hash[key] = value
end

#substitute(hash) ⇒ Object



43
44
45
46
47
48
# File 'lib/style_train/theme.rb', line 43

def substitute hash
  hash.each do |key, value|
    hash[key] = palette[value] if palette[value]
  end
  hash
end