Class: Rapid::Setting::Namespace::Base

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

Instance Attribute Summary collapse

Attributes inherited from Base

#default_value, #full_name, #name, #namespace

Instance Method Summary collapse

Methods inherited from Base

#load

Constructor Details

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

Returns a new instance of Base.



9
10
11
12
13
14
15
16
# File 'lib/rapid/setting/namespace/base.rb', line 9

def initialize name, options = {}
  super name, options
  @delegate = HashWithIndifferentAccess.new {}
  
  # class option isn't provided for the root namespace
  klass = options[:class]
  @instance_class = define_instance_class klass if klass
end

Instance Attribute Details

#instance_classObject

Returns the value of attribute instance_class.



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

def instance_class
  @instance_class
end

Instance Method Details

#[](key) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/rapid/setting/namespace/base.rb', line 30

def [] key
  namespace_name, child_name = Rapid::Settings.extract_nested_namespace(key)
  
  if namespace_name
    namespace = @delegate[namespace_name]
    namespace[child_name] if namespace
  else
    @delegate[key]
  end
end

#define_helpers(klass) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rapid/setting/namespace/base.rb', line 41

def define_helpers klass
  super
  name = self.name
  getter = self.name
  
  klass.class_eval do
    define_method "#{getter}?" do
      val = self[name]
      val != nil && val.on?
    end
  end
  
  true
end

#namespacesObject



18
19
20
21
22
# File 'lib/rapid/setting/namespace/base.rb', line 18

def namespaces
  values = @delegate.values
  values.delete_if {|c| !c.is_a?(Namespace::Base) }
  values
end

#scalarsObject



24
25
26
27
28
# File 'lib/rapid/setting/namespace/base.rb', line 24

def scalars
  values = @delegate.values
  values.delete_if {|c| c.is_a?(Namespace::Base) }
  values
end