Class: RailsSettings::Fields::Base

Inherits:
Struct
  • Object
show all
Defined in:
lib/rails-settings/fields/base.rb

Direct Known Subclasses

Array, BigDecimal, Boolean, Float, Hash, Integer, String

Constant Summary collapse

SEPARATOR_REGEXP =
/[\n,;]+/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def default
  @default
end

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def key
  @key
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def options
  @options
end

#parentObject

Returns the value of attribute parent

Returns:

  • (Object)

    the current value of parent



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def parent
  @parent
end

#readonlyObject

Returns the value of attribute readonly

Returns:

  • (Object)

    the current value of readonly



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def readonly
  @readonly
end

#scopeObject

Returns the value of attribute scope

Returns:

  • (Object)

    the current value of scope



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def scope
  @scope
end

#separatorObject

Returns the value of attribute separator

Returns:

  • (Object)

    the current value of separator



3
4
5
# File 'lib/rails-settings/fields/base.rb', line 3

def separator
  @separator
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of 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_valueObject



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

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/rails-settings/fields/base.rb', line 41

def deserialize(value)
  raise NotImplementedError
end

#readObject



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_valueObject



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

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/rails-settings/fields/base.rb', line 45

def serialize(value)
  raise NotImplementedError
end

#table_exists?Boolean

Returns:



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_hObject



49
50
51
# File 'lib/rails-settings/fields/base.rb', line 49

def to_h
  super.slice(:scope, :key, :default, :type, :readonly, :options)
end