Class: Dsu::Models::ColorTheme

Inherits:
Crud::JsonFile show all
Includes:
Support::ColorThemable, Support::Descriptable, Support::Fileable, Support::Presentable
Defined in:
lib/dsu/models/color_theme.rb

Overview

This class represents a dsu color theme.

Constant Summary collapse

THEME_FILE_NAME_REGEX =
/.+.json/
VERSION =
Migration::VERSION
DEFAULT_THEME_NAME =
'default'
DEFAULT_THEME_COLORS =

Theme colors key/value pair format: <key>: { color: <color> [, mode: <mode>] [, background: <background>] } Where <color> (required) == any color represented in the colorize gem ‘String.colors` array.

<mode> (optional, default is :default) == any mode represented in the colorize gem `String.modes` array.
<background> (optional, default is :default) == any color represented in the colorize gem
             `String.colors` array.
{
  help: { color: :cyan },
  dsu_header: { color: :white, mode: :bold, background: :cyan },
  dsu_footer: { color: :cyan },
  header: { color: :cyan, mode: :bold },
  subheader: { color: :cyan, mode: :underline },
  body: { color: :cyan },
  footer: { color: :light_cyan },
  date: { color: :cyan, mode: :bold },
  index: { color: :light_cyan },
  # Status colors.
  info: { color: :cyan },
  success: { color: :green },
  warning: { color: :yellow },
  error: { color: :light_yellow, background: :red },
  # Prompts
  prompt: { color: :cyan, mode: :bold },
  prompt_options: { color: :white, mode: :bold }
}.freeze
DEFAULT_THEME =
{
  version: VERSION,
  description: 'Default theme.'
}.merge(DEFAULT_THEME_COLORS).freeze
MIN_DESCRIPTION_LENGTH =
2
MAX_DESCRIPTION_LENGTH =
256

Constants included from Support::Fileable

Support::Fileable::MIGRATION_VERSION_FILE_NAME

Instance Attribute Summary collapse

Attributes inherited from Crud::JsonFile

#file_path, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Presentable

#presenter

Methods included from Support::Fileable

#backup_folder_for, #config_file_name, #config_folder, #config_path, #current_project_file, #current_project_file_name, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #project_file_for, #project_folder_for, #projects_folder, #root_folder, #seed_data_dsu_configuration_for, #seed_data_dsu_folder_for, #temp_folder, #theme_file_name, #themes_folder, #themes_path

Methods included from Support::Descriptable

included, #short_description

Methods included from Support::ColorThemable

apply_theme, #prompt_with_options

Methods inherited from Crud::JsonFile

file_does_not_exist_message, #file_exist?, file_exist?, parse, #persisted?, read, read!, #reload, #save, #save!, #to_model, #update_version!, #write, write, #write!, write!

Constructor Details

#initialize(theme_name:, theme_hash: nil, options: {}) ⇒ ColorTheme

Returns a new instance of ColorTheme.

Raises:

  • (ArgumentError)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dsu/models/color_theme.rb', line 68

def initialize(theme_name:, theme_hash: nil, options: {})
  raise ArgumentError, 'theme_name is nil.' if theme_name.nil?
  raise ArgumentError, "theme_name is the wrong object type: \"#{theme_name}\"." unless theme_name.is_a?(String)
  unless theme_hash.is_a?(Hash) || theme_hash.nil?
    raise ArgumentError, "theme_hash is the wrong object type: \"#{theme_hash}\"."
  end

  FileUtils.mkdir_p themes_folder

  @theme_name = theme_name
  @options = options || {}

  super(self.class.send(:themes_path, theme_name: @theme_name))

  theme_hash ||= DEFAULT_THEME.merge(description: "#{@theme_name.capitalize} theme")

  # Color themes I expect will change a lot, so we're using
  # a little meta-programming here to dynamically create
  # public attr_readers and private attr_writers based on the
  # keys in DEFAULT_THEME, then assign those attributes from
  # the values in theme_hash. theme_hash will be guaranteed to
  # have the same keys as DEFAULT_THEME.keys at this point
  # because we called ensure_theme_hash! above.
  DEFAULT_THEME.each_key do |attr|
    self.class.class_eval do
      attr_reader attr
      attr_writer attr
      private :"#{attr}="
    end
    attr_value = theme_hash[attr]
    attr_value = attr_value.merge_default_colors if default_theme_color_keys.include?(attr)
    send(:"#{attr}=", attr_value)
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



66
67
68
# File 'lib/dsu/models/color_theme.rb', line 66

def options
  @options
end

#theme_nameObject

Returns the value of attribute theme_name.



66
67
68
# File 'lib/dsu/models/color_theme.rb', line 66

def theme_name
  @theme_name
end

Class Method Details

.allObject



118
119
120
121
122
123
# File 'lib/dsu/models/color_theme.rb', line 118

def all
  Dir.glob("#{themes_folder}/*").map do |file_path|
    theme_name = File.basename(file_path, '.*')
    find(theme_name: theme_name)
  end
end

.configurationObject



125
126
127
# File 'lib/dsu/models/color_theme.rb', line 125

def configuration
  Models::Configuration.new
end

.currentObject



129
130
131
132
133
134
# File 'lib/dsu/models/color_theme.rb', line 129

def current
  theme_name = configuration.theme_name
  return unless exist?(theme_name: theme_name)

  find(theme_name: theme_name)
end

.current_or_defaultObject

Returns the current color theme if it exists; otherwise, it returns the default color theme.



138
139
140
# File 'lib/dsu/models/color_theme.rb', line 138

def current_or_default
  current || default
end

.defaultObject



142
143
144
# File 'lib/dsu/models/color_theme.rb', line 142

def default
  new(theme_name: DEFAULT_THEME_NAME, theme_hash: DEFAULT_THEME)
end

.delete(theme_name:) ⇒ Object



146
147
148
# File 'lib/dsu/models/color_theme.rb', line 146

def delete(theme_name:)
  superclass.delete(file_path: themes_path(theme_name: theme_name))
end

.delete!(theme_name:) ⇒ Object



150
151
152
# File 'lib/dsu/models/color_theme.rb', line 150

def delete!(theme_name:)
  superclass.delete!(file_path: themes_path(theme_name: theme_name))
end

.ensure_color_theme_color_defaults_for(theme_hash: DEFAULT_THEME) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/dsu/models/color_theme.rb', line 154

def ensure_color_theme_color_defaults_for(theme_hash: DEFAULT_THEME)
  theme_hash = theme_hash.dup

  theme_hash.each_pair do |key, value|
    next unless default_theme_color_keys.include?(key)

    theme_hash[key] = value.merge_default_colors
  end
  theme_hash
end

.exist?(theme_name:) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/dsu/models/color_theme.rb', line 165

def exist?(theme_name:)
  superclass.file_exist?(file_path: themes_path(theme_name: theme_name))
end

.find(theme_name:) ⇒ Object



169
170
171
172
# File 'lib/dsu/models/color_theme.rb', line 169

def find(theme_name:)
  theme_hash = read!(file_path: themes_path(theme_name: theme_name))
  Services::ColorTheme::HydratorService.new(theme_name: theme_name, theme_hash: theme_hash).call
end

.find_or_create(theme_name:) ⇒ Object



174
175
176
177
178
# File 'lib/dsu/models/color_theme.rb', line 174

def find_or_create(theme_name:)
  return find(theme_name: theme_name) if exist?(theme_name: theme_name)

  new(theme_name: theme_name).tap(&:write!)
end

.find_or_initialize(theme_name:) ⇒ Object



180
181
182
183
184
# File 'lib/dsu/models/color_theme.rb', line 180

def find_or_initialize(theme_name:)
  return find(theme_name: theme_name) if exist?(theme_name: theme_name)

  new(theme_name: theme_name)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



201
202
203
204
205
206
# File 'lib/dsu/models/color_theme.rb', line 201

def ==(other)
  return false unless other.is_a?(self.class)
  return false unless other.theme_name == theme_name

  DEFAULT_THEME.keys.all? { |key| public_send(key) == other.public_send(key) }
end

#deleteObject



103
104
105
# File 'lib/dsu/models/color_theme.rb', line 103

def delete
  self.class.delete(theme_name: theme_name)
end

#delete!Object



107
108
109
# File 'lib/dsu/models/color_theme.rb', line 107

def delete!
  self.class.delete!(theme_name: theme_name)
end

#exist?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/dsu/models/color_theme.rb', line 111

def exist?
  self.class.exist?(theme_name: theme_name)
end

#hashObject



209
210
211
212
213
# File 'lib/dsu/models/color_theme.rb', line 209

def hash
  DEFAULT_THEME.keys.map { |key| public_send(key) }.tap do |hashes|
    hashes << theme_name.hash
  end.hash
end

#to_hObject



193
194
195
196
197
198
199
# File 'lib/dsu/models/color_theme.rb', line 193

def to_h
  {}.tap do |hash|
    DEFAULT_THEME.each_key do |key|
      hash[key] = public_send(key)
    end
  end
end