Class: ThemeSettingsManager
- Inherits:
-
Object
- Object
- ThemeSettingsManager
show all
- Defined in:
- lib/theme_settings_manager.rb
Defined Under Namespace
Classes: Bool, Enum, Float, Integer, List, Objects, String, Upload
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, default, theme, opts = {}) ⇒ ThemeSettingsManager
Returns a new instance of ThemeSettingsManager.
30
31
32
33
34
35
36
|
# File 'lib/theme_settings_manager.rb', line 30
def initialize(name, default, theme, opts = {})
@name = name.to_sym
@default = default
@theme = theme
@opts = opts
@types = self.class.types
end
|
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
4
5
6
|
# File 'lib/theme_settings_manager.rb', line 4
def default
@default
end
|
#name ⇒ Object
Returns the value of attribute name.
4
5
6
|
# File 'lib/theme_settings_manager.rb', line 4
def name
@name
end
|
#theme ⇒ Object
Returns the value of attribute theme.
4
5
6
|
# File 'lib/theme_settings_manager.rb', line 4
def theme
@theme
end
|
Class Method Details
.cast(value) ⇒ Object
22
23
24
|
# File 'lib/theme_settings_manager.rb', line 22
def self.cast(value)
value
end
|
.cast_row_value(row) ⇒ Object
10
11
12
13
14
|
# File 'lib/theme_settings_manager.rb', line 10
def self.cast_row_value(row)
type_name = self.types.invert[row.data_type].downcase.capitalize
klass = "ThemeSettingsManager::#{type_name}".constantize
klass.cast(klass.(row))
end
|
.create(name, default, type, theme, opts = {}) ⇒ Object
16
17
18
19
20
|
# File 'lib/theme_settings_manager.rb', line 16
def self.create(name, default, type, theme, opts = {})
type_name = self.types.invert[type].downcase.capitalize
klass = "ThemeSettingsManager::#{type_name}".constantize
klass.new(name, default, theme, opts)
end
|
26
27
28
|
# File 'lib/theme_settings_manager.rb', line 26
def self.(row)
row.value
end
|
.types ⇒ Object
6
7
8
|
# File 'lib/theme_settings_manager.rb', line 6
def self.types
ThemeSetting.types
end
|
Instance Method Details
#create_record!(args) ⇒ Object
79
80
81
82
83
|
# File 'lib/theme_settings_manager.rb', line 79
def create_record!(args)
record = ThemeSetting.new(name: @name, data_type: type, theme: @theme, **args)
record.save!
record
end
|
#db_record ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/theme_settings_manager.rb', line 67
def db_record
theme.theme_settings.to_a.find do |i|
i.name.to_s == @name.to_s && i.data_type.to_s == type.to_s
end
end
|
#description ⇒ Object
50
51
52
|
# File 'lib/theme_settings_manager.rb', line 50
def description
@opts[:description] end
|
#ensure_is_valid_value!(new_value) ⇒ Object
#has_max? ⇒ Boolean
101
102
103
104
|
# File 'lib/theme_settings_manager.rb', line 101
def has_max?
max = @opts[:max]
(max.is_a?(::Integer) || max.is_a?(::Float)) && max != ::Float::INFINITY
end
|
#has_min? ⇒ Boolean
96
97
98
99
|
# File 'lib/theme_settings_manager.rb', line 96
def has_min?
min = @opts[:min]
(min.is_a?(::Integer) || min.is_a?(::Float)) && min != -::Float::INFINITY
end
|
#has_record? ⇒ Boolean
85
86
87
|
# File 'lib/theme_settings_manager.rb', line 85
def has_record?
db_record.present?
end
|
#requests_refresh? ⇒ Boolean
54
55
56
|
# File 'lib/theme_settings_manager.rb', line 54
def requests_refresh?
@opts[:refresh]
end
|
#type ⇒ Object
46
47
48
|
# File 'lib/theme_settings_manager.rb', line 46
def type
@types[type_name]
end
|
#type_name ⇒ Object
42
43
44
|
# File 'lib/theme_settings_manager.rb', line 42
def type_name
self.class.name.demodulize.downcase.to_sym
end
|
#update_record!(args) ⇒ Object
75
76
77
|
# File 'lib/theme_settings_manager.rb', line 75
def update_record!(args)
db_record.tap { |instance| instance.update!(args) }
end
|
#value ⇒ Object
38
39
40
|
# File 'lib/theme_settings_manager.rb', line 38
def value
has_record? ? db_record.value : default
end
|
#value=(new_value) ⇒ Object
58
59
60
61
62
63
64
65
|
# File 'lib/theme_settings_manager.rb', line 58
def value=(new_value)
ensure_is_valid_value!(new_value)
value = new_value.to_s
record = has_record? ? update_record!(value:) : create_record!(value:)
record.value
end
|