Class: Rapid::Setting::Namespace::Instance

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/rapid/setting/namespace/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(namespace, parent, value = {}) ⇒ Instance

Returns a new instance of Instance.



11
12
13
14
15
16
17
18
19
# File 'lib/rapid/setting/namespace/instance.rb', line 11

def initialize namespace, parent, value = {}
  @namespace = namespace
  @parent = parent
  @assignment_exceptions = {}
  @delegate = HashWithIndifferentAccess.new
  
  _load value
  _load_scalar_defaults
end

Instance Method Details

#[](full_name) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rapid/setting/namespace/instance.rb', line 57

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

#[]=(key, value) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rapid/setting/namespace/instance.rb', line 74

def []= key, value
  namespace_name, child_name = Rapid::Settings.extract_nested_namespace(key)

  if namespace_name.nil?
    _set_base key, value
  else
    _set_child namespace_name, child_name, value
  end

rescue UnknownSettingError => e
  @assignment_exceptions[key] = e
  nil
rescue InvalidSettingError => e
  @assignment_exceptions[key] = e
  nil
end

#empty?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/rapid/setting/namespace/instance.rb', line 21

def empty?
  @is_empty
end

#namespacesObject



91
92
93
94
# File 'lib/rapid/setting/namespace/instance.rb', line 91

def namespaces
  values = @delegate.values.delete_if {|v| not v.is_a? Namespace::Instance }
  values
end

#on!Object



35
36
37
# File 'lib/rapid/setting/namespace/instance.rb', line 35

def on!
  @is_on = true
end

#on=(bool) ⇒ Object



30
31
32
33
# File 'lib/rapid/setting/namespace/instance.rb', line 30

def on= bool
  _load bool
  bool
end

#on?Boolean Also known as: on

Returns:

  • (Boolean)


25
26
27
# File 'lib/rapid/setting/namespace/instance.rb', line 25

def on?
  @is_on
end

#scalarsObject



96
97
98
99
# File 'lib/rapid/setting/namespace/instance.rb', line 96

def scalars
  values = @delegate.values.delete_if {|v| v.is_a? Namespace::Instance }
  values
end

#set?(full_name) ⇒ Boolean

Returns:

  • (Boolean)


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

def set? full_name
  names = full_name.split('.')
  name = names.pop
  
  namespace = self
  names.each do |n|
    namespace = namespace[n] if namespace.is_a?(Namespace::Instance)
  end
  
  value = namespace[name] if namespace.is_a?(Namespace::Instance)
  
  if value.is_a?(Namespace::Instance)
    value.on?
  else
    value != nil && value != false
  end
end

#to_dot_hashObject



116
117
118
# File 'lib/rapid/setting/namespace/instance.rb', line 116

def to_dot_hash
  Rapid::Settings.to_dot_hash to_hash
end

#to_hashObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rapid/setting/namespace/instance.rb', line 101

def to_hash
  hash = {}
  
  @delegate.each do |key, value|
    if value.is_a? Namespace::Instance
      child = value.to_hash
      hash[key] = child unless child.empty?
    else
      hash[key] = value
    end
  end
  
  hash
end