Class: Fino::Definition::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/fino/definition/setting.rb

Constant Summary collapse

TYPE_CLASSES =
[
  Fino::Settings::String,
  Fino::Settings::Integer,
  Fino::Settings::Float,
  Fino::Settings::Boolean,
  Fino::Settings::Select
].freeze
SETTING_TYPE_TO_TYPE_CLASS_MAPPING =
TYPE_CLASSES.each_with_object({}) do |klass, hash|
  hash[klass.type_identifier] = klass
end.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, setting_name:, section_definition: nil, **options) ⇒ Setting

Returns a new instance of Setting.



18
19
20
21
22
23
# File 'lib/fino/definition/setting.rb', line 18

def initialize(type:, setting_name:, section_definition: nil, **options)
  @setting_name = setting_name
  @section_definition = section_definition
  @type = type
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



16
17
18
# File 'lib/fino/definition/setting.rb', line 16

def options
  @options
end

#section_definitionObject (readonly)

Returns the value of attribute section_definition.



16
17
18
# File 'lib/fino/definition/setting.rb', line 16

def section_definition
  @section_definition
end

#setting_nameObject (readonly)

Returns the value of attribute setting_name.



16
17
18
# File 'lib/fino/definition/setting.rb', line 16

def setting_name
  @setting_name
end

#typeObject (readonly)

Returns the value of attribute type.



16
17
18
# File 'lib/fino/definition/setting.rb', line 16

def type
  @type
end

Instance Method Details

#defaultObject



39
40
41
# File 'lib/fino/definition/setting.rb', line 39

def default
  defined?(@default) ? @default : @default = deserialize(options[:default])
end

#descriptionObject



43
44
45
# File 'lib/fino/definition/setting.rb', line 43

def description
  defined?(@description) ? @description : @description = options[:description]
end

#deserialize(raw_value) ⇒ Object



35
36
37
# File 'lib/fino/definition/setting.rb', line 35

def deserialize(raw_value)
  type_class.deserialize(self, raw_value)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


59
60
61
# File 'lib/fino/definition/setting.rb', line 59

def eql?(other)
  self.class.eql?(other.class) && key == other.key
end

#hashObject



64
65
66
# File 'lib/fino/definition/setting.rb', line 64

def hash
  key.hash
end

#keyObject



55
56
57
# File 'lib/fino/definition/setting.rb', line 55

def key
  @key ||= path.join("/")
end

#pathObject



51
52
53
# File 'lib/fino/definition/setting.rb', line 51

def path
  @path ||= [section_definition&.name, setting_name].compact
end

#serialize(value) ⇒ Object



31
32
33
# File 'lib/fino/definition/setting.rb', line 31

def serialize(value)
  type_class.serialize(self, value)
end

#tagsObject



47
48
49
# File 'lib/fino/definition/setting.rb', line 47

def tags
  defined?(@tags) ? @tags : @tags = options[:tags] || []
end

#type_classObject



25
26
27
28
29
# File 'lib/fino/definition/setting.rb', line 25

def type_class
  @type_class ||= SETTING_TYPE_TO_TYPE_CLASS_MAPPING.fetch(type) do
    raise ArgumentError, "Unknown setting type #{type}"
  end
end