Class: Riemann::Dash::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/riemann/dash/config.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
8
# File 'lib/riemann/dash/config.rb', line 5

def initialize
  self.store = {}
  setup_default_values
end

Instance Attribute Details

#config_pathObject

Returns the value of attribute config_path.



2
3
4
# File 'lib/riemann/dash/config.rb', line 2

def config_path
  @config_path
end

#storeObject

Returns the value of attribute store.



3
4
5
# File 'lib/riemann/dash/config.rb', line 3

def store
  @store
end

Class Method Details

.instanceObject



10
11
12
# File 'lib/riemann/dash/config.rb', line 10

def self.instance
  @instance ||= Riemann::Dash::Config.new
end

.reset!Object



14
15
16
# File 'lib/riemann/dash/config.rb', line 14

def self.reset!
  @instance = nil
end

Instance Method Details

#[](k) ⇒ Object

backwards compatible forwarder to store-ivar



32
33
34
# File 'lib/riemann/dash/config.rb', line 32

def [](k)
  store[k]
end

#[]=(k, v) ⇒ Object



36
37
38
# File 'lib/riemann/dash/config.rb', line 36

def []=(k,v)
  store[k] = v
end

#load_config(path) ⇒ Object

Executes the configuration file.



42
43
44
45
46
47
48
49
50
# File 'lib/riemann/dash/config.rb', line 42

def load_config(path)
  self.config_path = path
  begin
    Riemann::Dash::App.instance_eval File.read(config_path)
    true
  rescue Errno::ENOENT
    false
  end
end

#load_controllersObject



52
53
54
# File 'lib/riemann/dash/config.rb', line 52

def load_controllers
  store[:controllers].each { |d| load_controllers_from(d) }
end

#load_controllers_from(dir) ⇒ Object

Load controllers.



66
67
68
69
70
# File 'lib/riemann/dash/config.rb', line 66

def load_controllers_from(dir)
  sorted_controller_list(dir).each do |r|
    require r
  end
end

#read_ws_configObject



122
123
124
125
126
127
128
# File 'lib/riemann/dash/config.rb', line 122

def read_ws_config
  if File.exists? ws_config_file
    File.read(ws_config_file)
  else
    MultiJson.encode({})
  end
end

#setup_default_valuesObject



18
19
20
21
22
23
24
25
# File 'lib/riemann/dash/config.rb', line 18

def setup_default_values
  store.merge!({
    :controllers => [File.join(File.dirname(__FILE__), 'controller')],
    :views       => File.join(File.dirname(__FILE__), 'views'),
    :ws_config   => File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'config.json')),
    :public      => File.join(File.dirname(__FILE__), 'public')
  })
end

#setup_public_dirObject



60
61
62
63
# File 'lib/riemann/dash/config.rb', line 60

def setup_public_dir
  require 'riemann/dash/rack/static'
  Riemann::Dash::App.use Riemann::Dash::Static, :root => store[:public]
end

#setup_viewsObject



56
57
58
# File 'lib/riemann/dash/config.rb', line 56

def setup_views
  Riemann::Dash::App.set :views, File.expand_path(store[:views])
end

#sorted_controller_list(dir) ⇒ Object

Controllers can be regular old one-file-per-class, but if you prefer a little more modularity, this method will allow you to define all controller methods in their own files. For example, get “/posts/*/edit” can live in controller/posts/_/edit.rb. The sorting system provided here requires files in the correct order to handle wildcards appropriately.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/riemann/dash/config.rb', line 78

def sorted_controller_list(dir)
  rbs = []
  Find.find(
    File.expand_path(dir)
  ) do |path|
    rbs << path if path =~ /\.rb$/
  end

  # Sort paths with _ last, becase those are wildcards.
  rbs.sort! do |a, b|
    as = a.split File::SEPARATOR
    bs = b.split File::SEPARATOR

    # Compare common subpaths
    l = [as.size, bs.size].min
    catch :x do
      (0...l).each do |i|
        a, b = as[i], bs[i]
        if a[/^_/] and not b[/^_/]
          throw :x, 1
        elsif b[/^_/] and not a[/^_/]
          throw :x, -1
        elsif ord = (a <=> b) and ord != 0
          throw :x, ord
        end
      end

      # All subpaths are identical; sort longest first
      if as.size > bs.size
        throw :x, -1
      elsif as.size < bs.size
        throw :x, -1
      else
        throw :x, 0
      end
    end
  end
end

#update_ws_config(update) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/riemann/dash/config.rb', line 130

def update_ws_config(update)
  update = MultiJson.decode(update)

  # Read old config
  if File.exists? ws_config_file
    old = MultiJson.decode File.read(ws_config_file)
  else
    old = {}
  end

  new_config = {}

  # Server
  new_config['server'] = update['server'] or old['server']

  #p update['workspaces']
  new_config['workspaces'] = update['workspaces'] or old['workspaces']

  # Save new config
  FileUtils.mkdir_p File.dirname(ws_config_file)
  File.open(ws_config_file, 'w') do |f|
    f.write(MultiJson.encode(new_config, :pretty => true))
  end
end

#ws_config_fileObject



27
28
29
# File 'lib/riemann/dash/config.rb', line 27

def ws_config_file
  store[:ws_config]
end