Class: IntercomRails::ConfigSingleton

Inherits:
Object
  • Object
show all
Defined in:
lib/intercom-rails/config.rb

Direct Known Subclasses

Config

Class Method Summary collapse

Class Method Details

.config_accessor(*args, &block) ⇒ Object


7
8
9
10
# File 'lib/intercom-rails/config.rb', line 7

def self.config_accessor(*args, &block)
  config_reader(*args)
  config_writer(*args, &block)
end

.config_group(name, &block) ⇒ Object


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/intercom-rails/config.rb', line 35

def self.config_group(name, &block)
  camelized_name = name.to_s.classify
  group = self.const_set(camelized_name, Class.new(self))

  meta_class.send(:define_method, name) do
    group
  end

  group.send(:instance_variable_set, :@underscored_class_name, name)
  group.instance_eval(&block)
end

.config_reader(name) ⇒ Object


16
17
18
19
20
# File 'lib/intercom-rails/config.rb', line 16

def self.config_reader(name)
  meta_class.send(:define_method, name) do
    instance_variable_get("@#{name}")
  end
end

.config_writer(name, &block) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/intercom-rails/config.rb', line 22

def self.config_writer(name, &block)
  meta_class.send(:define_method, "#{name}=") do |value|
    block.call(value) if block && (block.arity <= 1)

    if block && (block.arity > 1)
      field_name = underscored_class_name ? "#{underscored_class_name}.#{name}" : name
      block.call(value, field_name)
    end

    instance_variable_set("@#{name}", value)
  end
end

.meta_classObject


12
13
14
# File 'lib/intercom-rails/config.rb', line 12

def self.meta_class
  class << self; self end
end