Class: Singlettings::Base

Inherits:
Hash
  • Object
show all
Defined in:
lib/singlettings/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory_or_hash, default_namespace = nil) ⇒ Base

class self



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/singlettings/base.rb', line 66

def initialize(directory_or_hash, default_namespace= nil)
  @ns = self.class.ns default_namespace

  raise FileNotSpecifiedError, "You should choose a file to initialize" unless directory_or_hash

  if directory_or_hash.is_a? Hash
    @source = directory_or_hash
  else
    @source = self.class.source directory_or_hash
  end

  self.replace current_branch
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/singlettings/base.rb', line 98

def method_missing(method, *args, &block)
  key = method.to_s
  if current_branch.keys.include? key
    value = current_branch[key]
    if value.is_a? Hash
      self.class.new value
    else
      value
    end
  else
    raise NoSuchKeyError, "#{key} does not exist"
  end
end

Instance Attribute Details

#nsObject (readonly)

Returns the value of attribute ns.



7
8
9
# File 'lib/singlettings/base.rb', line 7

def ns
  @ns
end

#sourceObject (readonly)

Returns the value of attribute source.



7
8
9
# File 'lib/singlettings/base.rb', line 7

def source
  @source
end

Class Method Details

.[](key) ⇒ Object

Override the hash method



27
28
29
30
31
32
33
34
# File 'lib/singlettings/base.rb', line 27

def [](key)
  value = current_branch.fetch key.to_s, nil
  if value.is_a? Hash
    self.new value
  else
    value
  end
end

.current_branchObject



36
37
38
39
40
41
42
43
44
# File 'lib/singlettings/base.rb', line 36

def current_branch
  source unless @source
  return @source unless @ns
  if @source[@ns] == nil
    raise NoSuchNamespaceError, "Source is empty. The namespace does not exist"
  else
    @source[@ns]
  end
end

.method_missing(method, *args, &block) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/singlettings/base.rb', line 46

def method_missing(method, *args, &block)
  key = method.to_s
  if current_branch.keys.include? key
    value = current_branch[key]
    if value.is_a? Hash
      self.new value
    else
      value
    end
  else
    raise NoSuchKeyError, "#{key} does not exist"
  end
end

.ns(value = nil) ⇒ Object

Set the default namespace Basic Usage of ns in class: ns :development ns Rails.env



22
23
24
# File 'lib/singlettings/base.rb', line 22

def ns(value = nil)
  @ns = value.to_s if value
end

.respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/singlettings/base.rb', line 60

def respond_to_missing?(method, include_private = false)
  current_branch.keys.include? method.to_s ||
    (raise NoSuchKeyError, "#{method} does not exist")
end

.source(directory = nil) ⇒ Object

Basic Usage of source in class

source "config/setting.yml"
source


12
13
14
15
16
# File 'lib/singlettings/base.rb', line 12

def source(directory = nil)
  filename = directory ? directory : "#{self.ancestors[0].to_s.downcase}.yml"
  @erb_content = ERB.new(File.read(filename)).result # Set it in memory
  @source = @erb_content ? YAML.load(@erb_content) : {}
end

Instance Method Details

#[](key) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/singlettings/base.rb', line 89

def [](key)
  value = current_branch.fetch key.to_s, nil
  if value.is_a? Hash
    self.class.new value
  else
    value
  end
end

#current_branchObject



80
81
82
83
84
85
86
87
# File 'lib/singlettings/base.rb', line 80

def current_branch
  return source unless ns
  if source[ns] == nil
    raise NoSuchNamespaceError, "Source is empty. The namespace does not exist"
  else
    source[ns]
  end
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
# File 'lib/singlettings/base.rb', line 112

def respond_to_missing?(method, include_private = false)
  current_branch.keys.include? method.to_s ||
    (raise NoSuchKeyError, "#{method} does not exist")
end