Class: Setsy::Configuration

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Model, ActiveModel::Serializers::JSON
Defined in:
lib/setsy/configuration.rb

Constant Summary collapse

SETTING_PREFIX =
'setting__'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, settings, readers = {}) ⇒ Configuration

Returns a new instance of Configuration.



30
31
32
33
34
# File 'lib/setsy/configuration.rb', line 30

def initialize(record, settings, readers = {})
  @record = record
  @settings = settings
  write_readers(readers)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/setsy/configuration.rb', line 36

def method_missing(m, *args, &block)
  if @settings.key?(m)
    @settings[m]
  elsif respond_to?("#{SETTING_PREFIX}#{m}")
    send("#{SETTING_PREFIX}#{m}")
  else
    super
  end
end

Class Method Details

.from_set(record, set, readers = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/setsy/configuration.rb', line 17

def self.from_set(record, set, readers = {})
  settings = {}
  set = set.dup.with_indifferent_access
  record.class.setsy_default.each do |k, v|
    result = {}
    result[:value] = v.is_a?(Hash) ? v[:value] : v
    result[:default] = result[:value]
    result[:value] = set[k] if set[k]
    settings[k] = Attribute.new(result)
  end
  new(record, settings, readers)
end

.reader(name, &block) ⇒ Object



12
13
14
15
# File 'lib/setsy/configuration.rb', line 12

def self.reader(name, &block)
  readers[name] = block
  readers
end

.readersObject



8
9
10
# File 'lib/setsy/configuration.rb', line 8

def self.readers
  @readers ||= {}
end

Instance Method Details

#attributesObject



46
47
48
49
50
51
52
53
54
# File 'lib/setsy/configuration.rb', line 46

def attributes
  keys = @settings.keys
  keys.push(*methods.select { |m| m.to_s.starts_with?(SETTING_PREFIX) }.map { |m| m.to_s.gsub(SETTING_PREFIX, '') })
  h = {}
  keys.each do |k|
    h[k.to_sym] = k
  end
  h
end