Class: Ibrain::Preferences::Configuration
- Inherits:
-
Object
- Object
- Ibrain::Preferences::Configuration
- Includes:
- Preferable
- Defined in:
- lib/ibrain/preferences/configuration.rb
Overview
This takes the preferrable methods and adds some syntatic sugar to access the preferences
class App < Configuration
preference :color, :string
end
a = App.new
Provides the following setters:
a.color = :blue
a[:color] = :blue
a.set color: :blue
a.preferred_color = :blue
and the following getters:
a.color
a[:color]
a.get :color
a.preferred_color
Direct Known Subclasses
Instance Attribute Summary collapse
-
#load_defaults_called ⇒ Object
readonly
Returns the value of attribute load_defaults_called.
-
#loaded_defaults ⇒ Object
readonly
Returns the value of attribute loaded_defaults.
-
#preference_store ⇒ Object
(also: #preferences)
Storage method for preferences.
Class Method Summary collapse
-
.by_version(*args) ⇒ Object
Generates a different preference default depending on #version_defaults.
- .class_name_attribute(name, default:) ⇒ Object
- .preference(name, type, options = {}) ⇒ Object
Instance Method Summary collapse
- #check_load_defaults_called(instance_constant_name = nil) ⇒ Object
- #configure {|config| ... } ⇒ Object
-
#initialize ⇒ Configuration
constructor
A new instance of Configuration.
-
#load_defaults(version) ⇒ Object
overriden.
-
#reset ⇒ Object
Reset all preferences to their default values.
- #set(preferences) ⇒ Object
-
#use_legacy_db_preferences! ⇒ Object
Replace the new static preference store with the legacy store which fetches preferences from the DB.
-
#use_static_preferences! ⇒ Object
Replace the default legacy preference store, which stores preferences in the Ibrain_preferences table, with a plain in memory hash.
Methods included from Preferable
#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Constructor Details
#initialize ⇒ Configuration
Returns a new instance of Configuration.
43 44 45 46 |
# File 'lib/ibrain/preferences/configuration.rb', line 43 def initialize @loaded_defaults = Ibrain.ibrain_version @load_defaults_called = false end |
Instance Attribute Details
#load_defaults_called ⇒ Object (readonly)
Returns the value of attribute load_defaults_called.
41 42 43 |
# File 'lib/ibrain/preferences/configuration.rb', line 41 def load_defaults_called @load_defaults_called end |
#loaded_defaults ⇒ Object (readonly)
Returns the value of attribute loaded_defaults.
39 40 41 |
# File 'lib/ibrain/preferences/configuration.rb', line 39 def loaded_defaults @loaded_defaults end |
#preference_store ⇒ Object Also known as: preferences
Storage method for preferences.
78 |
# File 'lib/ibrain/preferences/configuration.rb', line 78 attr_writer :preference_store |
Class Method Details
.by_version(*args) ⇒ Object
Generates a different preference default depending on #version_defaults
This method is meant to be used in the ‘default:` keyword argument for preference. For instance, in the example, `foo`’s default was ‘true` until version 3.0.0.alpha, when it became `false`:
133 134 135 136 137 |
# File 'lib/ibrain/preferences/configuration.rb', line 133 def self.by_version(*args) proc do |loaded_defaults| Ibrain::Core::VersionedValue.new(*args).call(loaded_defaults) end end |
.class_name_attribute(name, default:) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/ibrain/preferences/configuration.rb', line 145 def self.class_name_attribute(name, default:) ivar = :"@#{name}" define_method("#{name}=") do |class_name| # If this is a named class constant, we should store it as a string to # allow code reloading. class_name = class_name.name if class_name.is_a?(Class) && class_name.name instance_variable_set(ivar, class_name) end define_method(name) do class_name = instance_variable_get(ivar) class_name ||= default class_name = class_name.constantize if class_name.is_a?(String) class_name end end |
.preference(name, type, options = {}) ⇒ Object
139 140 141 142 143 |
# File 'lib/ibrain/preferences/configuration.rb', line 139 def self.preference(name, type, = {}) super alias_method name.to_s, "preferred_#{name}" alias_method "#{name}=", "preferred_#{name}=" end |
Instance Method Details
#check_load_defaults_called(instance_constant_name = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ibrain/preferences/configuration.rb', line 57 def check_load_defaults_called(instance_constant_name = nil) return if load_defaults_called || !Ibrain::Core.has_install_generator_been_run? target_name = instance_constant_name || "#{self.class.name}.new" Ibrain::Deprecation.warn <<~MSG It's recommended that you explicitly load the default configuration for your current Ibrain version. You can do it by adding the following call to your Ibrain initializer within the #{target_name} block: config.load_defaults('#{Ibrain.ibrain_version}') MSG end |
#configure {|config| ... } ⇒ Object
72 73 74 |
# File 'lib/ibrain/preferences/configuration.rb', line 72 def configure yield(self) end |
#load_defaults(version) ⇒ Object
overriden.
51 52 53 54 55 |
# File 'lib/ibrain/preferences/configuration.rb', line 51 def load_defaults(version) @loaded_defaults = version @load_defaults_called = true reset end |
#reset ⇒ Object
Reset all preferences to their default values.
106 107 108 |
# File 'lib/ibrain/preferences/configuration.rb', line 106 def reset set(default_preferences) end |
#set(preferences) ⇒ Object
116 117 118 119 120 |
# File 'lib/ibrain/preferences/configuration.rb', line 116 def set(preferences) preferences.each do |name, value| set_preference name, value end end |
#use_legacy_db_preferences! ⇒ Object
Replace the new static preference store with the legacy store which fetches preferences from the DB.
99 100 101 |
# File 'lib/ibrain/preferences/configuration.rb', line 99 def use_legacy_db_preferences! @preference_store = ScopedStore.new(self.class.name.underscore) end |
#use_static_preferences! ⇒ Object
Replace the default legacy preference store, which stores preferences in the Ibrain_preferences table, with a plain in memory hash. This is faster and less error prone.
This will set all preferences to their default values.
These won’t be loaded from or persisted to the database, so any desired changes must be made each time the application is started, such as in an initializer.
93 94 95 |
# File 'lib/ibrain/preferences/configuration.rb', line 93 def use_static_preferences! @preference_store = default_preferences end |