Class: RailsSettings::Fields::Base
- Inherits:
-
Struct
- Object
- Struct
- RailsSettings::Fields::Base
- Defined in:
- lib/rails-settings/fields/base.rb
Constant Summary collapse
- SEPARATOR_REGEXP =
/[\n,;]+/
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
-
#key ⇒ Object
Returns the value of attribute key.
-
#options ⇒ Object
Returns the value of attribute options.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#readonly ⇒ Object
Returns the value of attribute readonly.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#separator ⇒ Object
Returns the value of attribute separator.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #default_value ⇒ Object
- #deserialize(value) ⇒ Object
-
#initialize(scope:, key:, default:, parent:, readonly:, separator:, type:, options:) ⇒ Base
constructor
A new instance of Base.
- #read ⇒ Object
- #save!(value:) ⇒ Object
- #saved_value ⇒ Object
- #serialize(value) ⇒ Object
- #table_exists? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(scope:, key:, default:, parent:, readonly:, separator:, type:, options:) ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 |
# File 'lib/rails-settings/fields/base.rb', line 6 def initialize(scope:, key:, default:, parent:, readonly:, separator:, type:, options:) super self.readonly = !!readonly self.type ||= :string self.separator ||= SEPARATOR_REGEXP end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def default @default end |
#key ⇒ Object
Returns the value of attribute key
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def key @key end |
#options ⇒ Object
Returns the value of attribute options
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def @options end |
#parent ⇒ Object
Returns the value of attribute parent
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def parent @parent end |
#readonly ⇒ Object
Returns the value of attribute readonly
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def readonly @readonly end |
#scope ⇒ Object
Returns the value of attribute scope
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def scope @scope end |
#separator ⇒ Object
Returns the value of attribute separator
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def separator @separator end |
#type ⇒ Object
Returns the value of attribute type
3 4 5 |
# File 'lib/rails-settings/fields/base.rb', line 3 def type @type end |
Class Method Details
.generate(**args) ⇒ Object
60 61 62 |
# File 'lib/rails-settings/fields/base.rb', line 60 def generate(**args) fetch_field_class(args[:type]).new(**args) end |
Instance Method Details
#default_value ⇒ Object
31 32 33 |
# File 'lib/rails-settings/fields/base.rb', line 31 def default_value default.is_a?(Proc) ? default.call : default end |
#deserialize(value) ⇒ Object
41 42 43 |
# File 'lib/rails-settings/fields/base.rb', line 41 def deserialize(value) raise NotImplementedError end |
#read ⇒ Object
35 36 37 38 39 |
# File 'lib/rails-settings/fields/base.rb', line 35 def read return deserialize(default_value) if readonly || saved_value.nil? deserialize(saved_value) end |
#save!(value:) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/rails-settings/fields/base.rb', line 13 def save!(value:) serialized_value = serialize(value) parent_record = parent.find_by(var: key) || parent.new(var: key) parent_record.value = serialized_value parent_record.save! parent_record.value end |
#saved_value ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/rails-settings/fields/base.rb', line 21 def saved_value return parent.send(:_all_settings)[key] if table_exists? # Fallback to default value if table was not ready (before migrate) puts( "WARNING: table: \"#{parent.table_name}\" does not exist or not database connection, `#{parent.name}.#{key}` fallback to returns the default value." ) nil end |
#serialize(value) ⇒ Object
45 46 47 |
# File 'lib/rails-settings/fields/base.rb', line 45 def serialize(value) raise NotImplementedError end |
#table_exists? ⇒ Boolean
53 54 55 56 57 |
# File 'lib/rails-settings/fields/base.rb', line 53 def table_exists? parent.table_exists? rescue StandardError false end |
#to_h ⇒ Object
49 50 51 |
# File 'lib/rails-settings/fields/base.rb', line 49 def to_h super.slice(:scope, :key, :default, :type, :readonly, :options) end |