Class: Rapid::Setting::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rapid/setting/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rapid/setting/base.rb', line 8

def initialize name, options = {}
  @name = name.to_sym if name
  @namespace = options[:namespace]
  @default_value = options[:default]
  
  if @namespace
    @full_name = "#{@namespace.full_name}.#{name}"
  elsif @name
    @full_name = name.to_s
  end
  
  if Rapid::Settings.reserved_name? @name
    raise ArgumentError.new("#{@name} is a reserved setting name")
  end
  
  if @namespace
    @namespace.send :add_child, self
  end
end

Instance Attribute Details

#default_valueObject (readonly)

Returns the value of attribute default_value.



6
7
8
# File 'lib/rapid/setting/base.rb', line 6

def default_value
  @default_value
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



6
7
8
# File 'lib/rapid/setting/base.rb', line 6

def full_name
  @full_name
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/rapid/setting/base.rb', line 6

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



6
7
8
# File 'lib/rapid/setting/base.rb', line 6

def namespace
  @namespace
end

Instance Method Details

#define_helpers(klass) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rapid/setting/base.rb', line 32

def define_helpers klass
  name = self.name
  getter = name
  setter = "#{name}="
  
  klass.class_eval do
    define_method getter do
      self[name]
    end
    
    define_method "#{getter}?" do
      self[name] != nil
    end
    
    define_method setter do |value|
      self[name] = value
    end
  end
  
  true
end

#load(value) ⇒ Object



28
29
30
# File 'lib/rapid/setting/base.rb', line 28

def load value
  raise "Subclass should override this method"
end