Module: Rapid::Settings

Defined in:
lib/rapid/settings.rb,
lib/rapid/setting/module.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_nested_namespace(full_name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/rapid/setting/module.rb', line 23

def extract_nested_namespace full_name
  return nil, nil if full_name.nil?
  
  full_name = full_name.to_s
  if full_name.to_s =~ /^([^\.]+)\.(.+)$/
    return $1, $2
  else
    return nil, full_name
  end
end

.included(base) ⇒ Object



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

def self.included base
  base.send :include, ActiveModel::Validations
  base.send :include, Rapid::Setting::NestedValidations
  base.send :include, Rapid::Setting::Comments
  
  base.extend ClassMethods
  
  base.validate :validate_settings
  
  base.delegate :to_hash,     :to => :settings
  base.delegate :to_dot_hash, :to => :settings
  base.delegate :set?,        :to => :settings
end

.reserved_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rapid/setting/module.rb', line 19

def reserved_name? name
  reserved_names.include? name.to_s
end

.reserved_namesObject



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rapid/setting/module.rb', line 7

def reserved_names
  # ActiveModel::Validations
  @reserved_words ||= %w(_validate_callbacks _validators errors read_attribute_for_validation 
    run_callbacks valid validates_acceptance_of validates_confirmation_of validates_exclusion_of 
    validates_format_of validates_inclusion_of validates_length_of validates_numericality_of 
    validates_presence_of validates_size_of validates_with validation_context) +
  
  # internal
  %w(_root empty namespaces scalars validate_nested_settings validate_no_assignment_exceptions
     load_yaml load_hash to_yaml to_dot_hash to_hash parse_yaml get_binding)
end

.to_dot_hash(nested_hash) ⇒ Object



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

def to_dot_hash nested_hash
  dot_hash = {}
  flatten = proc { |key, value|
    
    if value.is_a?(Hash)
      value.each do |k2, v2|
        flatten[key + [k2], v2]
      end
    else
      namespace = key.join('.')
      dot_hash[namespace] = value
    end
  }
  
  flatten[[], nested_hash]
  dot_hash
end

Instance Method Details

#[](full_name) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rapid/settings.rb', line 43

def [] full_name
  namespace_name, child_name = Rapid::Settings.extract_nested_namespace(full_name)
  
  if namespace_name.nil?
    setting = self.class.settings[full_name]
    if setting
      settings[full_name]
    elsif respond_to?(full_name)
      send full_name
    else
      nil
    end
  else
    settings[full_name]
  end
end

#[]=(full_name, value) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rapid/settings.rb', line 60

def []= full_name, value
  namespace_name, child_name = Rapid::Settings.extract_nested_namespace(full_name)
  
  if namespace_name
    settings[full_name] = value
    
  elsif namespace_name.nil?
    setting = self.class.settings[full_name]
    if setting
      settings[full_name] = value
    else
      writer = "#{full_name}="
      if respond_to?(writer)
        send writer, value
      else
        settings[full_name] = value
      end
    end
  end
end

#get_bindingObject



39
40
41
# File 'lib/rapid/settings.rb', line 39

def get_binding
  binding
end

#load(hash) ⇒ Object



81
82
83
84
85
# File 'lib/rapid/settings.rb', line 81

def load hash
  hash.each do |key, value|
    self[key] = value
  end
end

#settingsObject



31
32
33
# File 'lib/rapid/settings.rb', line 31

def settings
  @settings ||= Setting::Namespace::Instance.new(self.class.settings, self)
end

#settings=(settings) ⇒ Object



35
36
37
# File 'lib/rapid/settings.rb', line 35

def settings= settings
  self.settings.send :_load, settings
end