lib/super_settings/setting.rb in super_settings-1.0.0 vs lib/super_settings/setting.rb in super_settings-1.0.1
- old
+ new
@@ -20,10 +20,13 @@
VALUE_TYPES = [STRING, INTEGER, FLOAT, BOOLEAN, DATETIME, ARRAY].freeze
ARRAY_DELIMITER = /[\n\r]+/.freeze
+ NOT_SET = Object.new.freeze
+ private_constant :NOT_SET
+
# Exception raised if you try to save with invalid data.
class InvalidRecordError < StandardError
end
include Attributes
@@ -31,43 +34,44 @@
# The changed_by attribute is used to temporarily store an identifier for the user
# who made a change to a setting to be stored in the history table. This value is optional
# and is cleared after the record is saved.
attr_accessor :changed_by
+ @storage = NOT_SET
+ @after_save_blocks = []
+
class << self
# Set a cache to use for caching values. This feature is optional. The cache must respond
# to +delete(key)+ and +fetch(key, &block)+. If you are running in a Rails environment,
# you can use +Rails.cache+ or any ActiveSupport::Cache::Store object.
attr_accessor :cache
# Set the storage class to use for persisting data.
attr_writer :storage
+ attr_reader :after_save_blocks
+
# @return [Class] The storage class to use for persisting data.
# @api private
def storage
- if defined?(@storage)
- @storage
- elsif defined?(::SuperSettings::Storage::ActiveRecordStorage)
- ::SuperSettings::Storage::ActiveRecordStorage
+ if @storage == NOT_SET
+ if defined?(::SuperSettings::Storage::ActiveRecordStorage)
+ ::SuperSettings::Storage::ActiveRecordStorage
+ else
+ raise ArgumentError.new("No storage class defined for #{name}")
+ end
else
- raise ArgumentError.new("No storage class defined for #{name}")
+ @storage
end
end
# Add a block of code that will be called when a setting is saved. The block will be
# called with a Setting object. The object will have been saved, but the `changes`
# hash will still be set indicating what was changed. You can define multiple after_save blocks.
#
# @yieldparam setting [SuperSetting::Setting]
def after_save(&block)
after_save_blocks << block
- end
-
- # @return [Array<Proc>] Blocks to be called after a setting is saved.
- # @api private
- def after_save_blocks
- @after_save_blocks ||= []
end
# Create a new setting with the specified attributes.
#
# @param attributes [Hash] hash of attribute names and values