Class: FoxTail::Theme

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

Overview

FoxTail Component Theme

Constant Summary collapse

BASE_KEY =
"base"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theme = {}, class_merger: ClassnameMerger.new) ⇒ Theme

Returns a new instance of Theme.



8
9
10
11
# File 'lib/fox_tail/theme.rb', line 8

def initialize(theme = {}, class_merger: ClassnameMerger.new)
  @class_merger = class_merger
  @base_theme = (theme || {}).deep_stringify_keys
end

Class Method Details

.load_file(file) ⇒ FoxTail::Theme

Load the theme from a YAML file

Parameters:

  • file (String)

    The path to the theme file.

Returns:

Raises:

  • (IO::Error)

    if unable to load the theme file



128
129
130
# File 'lib/fox_tail/theme.rb', line 128

def load_file(file)
  new YAML.load_file(file, aliases: true)
end

Instance Method Details

#apply(key, object, attributes = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/fox_tail/theme.rb', line 51

def apply(key, object, attributes = {})
  key = normalize_key key
  return if key.blank? || object.blank?

  object_theme = lookup_key base_theme, key
  attributes = ActiveSupport::HashWithIndifferentAccess.new attributes
  object = ActiveSupport::HashWithIndifferentAccess.new object if object.is_a? Hash
  apply_theme object_theme, object, attributes
end

#classname(key) ⇒ Object Also known as: []

Return the classnames for the ‘key` in the theme



32
33
34
35
36
37
# File 'lib/fox_tail/theme.rb', line 32

def classname(key)
  key = normalize_key key
  return if key.blank?

  lookup_key base_theme, key
end

#dupObject



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

def dup
  Theme.new @base_theme.deep_dup
end

#merge(theme) ⇒ Object



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

def merge(theme)
  dup.merge! theme
end

#merge!(theme) ⇒ Object

Merge another theme into the current theme



14
15
16
17
18
19
20
21
# File 'lib/fox_tail/theme.rb', line 14

def merge!(theme)
  theme = Theme.load_file theme if theme.is_a? String
  theme = theme.base_theme if theme.is_a? Theme
  extended = theme.delete("_extend") || {}
  base_theme.deep_merge! theme
  extend_theme base_theme, extended
  self
end

#theme(key) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/fox_tail/theme.rb', line 41

def theme(key)
  key = normalize_key key
  return if key.blank?

  hash = lookup_key base_theme, key
  return if hash.blank?

  self.class.new hash
end

#to_hObject



61
62
63
# File 'lib/fox_tail/theme.rb', line 61

def to_h
  @base_theme.deep_dup.to_h
end