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

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

Constants included from Support::Descriptable

Support::Descriptable::DESCRIPTION_MAX_COUNT

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, #config_file_name, #config_folder, #config_path, #dsu_folder, #entries_file_name, #entries_folder, #entries_path, #gem_dir, #migration_version_folder, #migration_version_path, #root_folder, #seed_data_folder, #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, #write, write, write!, #write!

Constructor Details

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

Returns a new instance of ColorTheme.

Raises:

  • (ArgumentError)


67
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
# File 'lib/dsu/models/color_theme.rb', line 67

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_for, 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.



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

def options
  @options
end

#theme_nameObject

Returns the value of attribute theme_name.



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

def theme_name
  @theme_name
end

Class Method Details

.allObject



115
116
117
118
119
120
# File 'lib/dsu/models/color_theme.rb', line 115

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

.configurationObject



122
123
124
# File 'lib/dsu/models/color_theme.rb', line 122

def configuration
  Models::Configuration.new
end

.currentObject



126
127
128
129
130
131
# File 'lib/dsu/models/color_theme.rb', line 126

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.



135
136
137
# File 'lib/dsu/models/color_theme.rb', line 135

def current_or_default
  current || default
end

.defaultObject



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

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

.delete(theme_name:) ⇒ Object



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

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

.delete!(theme_name:) ⇒ Object



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

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

.ensure_color_theme_color_defaults_for(theme_hash: DEFAULT_THEME) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/dsu/models/color_theme.rb', line 151

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)


162
163
164
# File 'lib/dsu/models/color_theme.rb', line 162

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

.find(theme_name:) ⇒ Object



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

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

.find_or_create(theme_name:) ⇒ Object



171
172
173
174
175
# File 'lib/dsu/models/color_theme.rb', line 171

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



177
178
179
180
181
# File 'lib/dsu/models/color_theme.rb', line 177

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?



250
251
252
253
254
255
# File 'lib/dsu/models/color_theme.rb', line 250

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



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

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

#delete!Object



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

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

#exist?Boolean

Returns:

  • (Boolean)


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

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

#hashObject



258
259
260
261
262
# File 'lib/dsu/models/color_theme.rb', line 258

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

#to_hObject



234
235
236
237
238
239
240
# File 'lib/dsu/models/color_theme.rb', line 234

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

#to_theme_colors_hObject



242
243
244
245
246
247
248
# File 'lib/dsu/models/color_theme.rb', line 242

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