Class: Hansi::Theme

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*inherit, **rules) ⇒ Theme

Returns a new instance of Theme.



18
19
20
21
# File 'lib/hansi/theme.rb', line 18

def initialize(*inherit, **rules)
  inherited = inherit.map { |t| Theme[t].rules }.inject({}, :merge)
  @rules    = inherited.merge(rules).freeze
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



17
18
19
# File 'lib/hansi/theme.rb', line 17

def rules
  @rules
end

Class Method Details

.[](key) ⇒ Object



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

def self.[](key)
  case key
  when self   then key
  when Symbol then Themes.fetch(key)
  when Hash   then new(**key)
  when Array  then key.map { |k| self[k] }.inject(:merge)
  else raise ArgumentError, "cannot convert %p into a %p" % [key, self]
  end
end

.[]=(key, value) ⇒ Object



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

def self.[]=(key, value)
  Themes[key.to_sym] = self[value]
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/hansi/theme.rb', line 23

def ==(other)
  other.class == self.class and other.rules == self.rules
end

#[](key) ⇒ Object



35
36
37
38
39
40
# File 'lib/hansi/theme.rb', line 35

def [](key)
  return self[rules[key]]        if rules.include? key
  return self[rules[key.to_sym]] if key.respond_to? :to_sym and rules.include? key.to_sym
  ColorParser.parse(key)
rescue ColorParser::IllegalValue
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/hansi/theme.rb', line 27

def eql?(other)
  other.class.eql?(self.class) and other.rules.eql?(self.rules)
end

#hashObject



31
32
33
# File 'lib/hansi/theme.rb', line 31

def hash
  rules.hash
end

#inspectObject



62
63
64
# File 'lib/hansi/theme.rb', line 62

def inspect
  "%p[%p]" % [self.class, theme_name || rules]
end

#merge(other) ⇒ Object



42
43
44
45
# File 'lib/hansi/theme.rb', line 42

def merge(other)
  other_rules = self.class[other].rules
  self.class.new(**rules.merge(other_rules))
end

#theme_nameObject



58
59
60
# File 'lib/hansi/theme.rb', line 58

def theme_name
  Themes.keys.detect { |key| Themes[key] == self }
end

#to_css(&block) ⇒ Object



52
53
54
55
56
# File 'lib/hansi/theme.rb', line 52

def to_css(&block)
  mapping = rules.keys.group_by { |key| self[key] }
  mapping = mapping.map { |c,k| c.to_css(*k, &block) }
  mapping.compact.join(?\n)
end

#to_hObject



47
48
49
50
# File 'lib/hansi/theme.rb', line 47

def to_h
  mapped = rules.keys.map { |key| [key, self[key]] if self[key] }
  Hash[mapped.compact]
end