Class: ShinyThemes::ThemeConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/shiny_themes/theme_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defaults = Engine.default_options) ⇒ ThemeConfig

Create the ordered options, populate with provided hash and load YAML file options.

Parameters:

  • default_options (Hash) —

    (Hash.new) - Options to populate the theme config with.



13
14
15
16
17
18
# File 'lib/shiny_themes/theme_config.rb', line 13

def initialize(defaults = Engine.default_options)
  unless Rails.application.config.try(:theme)
    Rails.application.config.theme = ActiveSupport::OrderedOptions.new
  end
  @defaults = defaults
end

Instance Attribute Details

#defaults ⇒ Object

Returns the value of attribute defaults.



3
4
5
# File 'lib/shiny_themes/theme_config.rb', line 3

def defaults
  @defaults
end

Instance Method Details

#config_pathname ⇒ Object

The pathname where the theme config file can be found



37
38
39
# File 'lib/shiny_themes/theme_config.rb', line 37

def config_pathname
  Rails.root.join('config', 'theme.yml')
end

#full_config ⇒ Object



41
42
43
44
45
46
47
# File 'lib/shiny_themes/theme_config.rb', line 41

def full_config
  begin
    @full_config ||= YAML.load(File.open(config_pathname))
  rescue Errno::ENOENT
    {}
  end
end

#load ⇒ Object

Load the theme.yml file and merge it with the theme configuration



21
22
23
24
25
26
# File 'lib/shiny_themes/theme_config.rb', line 21

def load
  new_config = full_config[Rails.env].try(:deep_symbolize_keys!) || {}
  # Honor values in config file over defaults
  @defaults.reject! { |k, _| new_config.keys.include?(k) }
  Rails.application.config.theme.merge!(@defaults.merge(new_config))
end

#save ⇒ Object

Save the current state of the theme config to the theme.yml file



29
30
31
32
33
34
# File 'lib/shiny_themes/theme_config.rb', line 29

def save
  # Don't save default values
  save_config = Rails.application.config.theme.reject { |k, _| @defaults.keys.include?(k) }
  full_config[Rails.env].merge!(save_config)
  File.open(config_pathname, 'w') { |f| f << full_config.to_yaml }
end