Class: Hansi::Theme
- Inherits:
-
Object
- Object
- Hansi::Theme
- Defined in:
- lib/hansi/theme.rb
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Returns the value of attribute rules.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(*inherit, **rules) ⇒ Theme
constructor
A new instance of Theme.
- #inspect ⇒ Object
- #merge(other) ⇒ Object
- #theme_name ⇒ Object
- #to_css(&block) ⇒ Object
- #to_h ⇒ Object
Constructor Details
Instance Attribute Details
#rules ⇒ Object (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
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 |
#hash ⇒ Object
31 32 33 |
# File 'lib/hansi/theme.rb', line 31 def hash rules.hash end |
#inspect ⇒ Object
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_name ⇒ Object
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_h ⇒ Object
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 |