Class: Dsu::Models::ColorTheme
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 },
info: { color: :cyan },
success: { color: :green },
warning: { color: :yellow },
error: { color: :light_yellow, background: :red },
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
Support::Fileable::MIGRATION_VERSION_FILE_NAME
Instance Attribute Summary collapse
#file_path, #version
Class Method Summary
collapse
Instance Method Summary
collapse
#presenter
#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
included, #short_description
apply_theme, #prompt_with_options
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.
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")
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
#options ⇒ Object
Returns the value of attribute options.
66
67
68
|
# File 'lib/dsu/models/color_theme.rb', line 66
def options
@options
end
|
#theme_name ⇒ Object
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
.all ⇒ Object
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
|
.current ⇒ Object
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_default ⇒ Object
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
|
.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
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
|
#delete ⇒ Object
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
111
112
113
|
# File 'lib/dsu/models/color_theme.rb', line 111
def exist?
self.class.exist?(theme_name: theme_name)
end
|
#hash ⇒ Object
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_h ⇒ Object
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
|