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.



7
8
9
10
# File 'lib/riemann/dash/config.rb', line 7

def initialize
  self.store = {}
  setup_default_values
end

Instance Attribute Details

#config_pathObject

Returns the value of attribute config_path.



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

def config_path
  @config_path
end

#storeObject

Returns the value of attribute store.



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

def store
  @store
end

Class Method Details

.instanceObject



12
13
14
# File 'lib/riemann/dash/config.rb', line 12

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

.reset!Object



16
17
18
# File 'lib/riemann/dash/config.rb', line 16

def self.reset!
  @instance = nil
end

Instance Method Details

#[](k) ⇒ Object

backwards compatible forwarder to store-ivar



34
35
36
# File 'lib/riemann/dash/config.rb', line 34

def [](k)
  store[k]
end

#[]=(k, v) ⇒ Object



38
39
40
# File 'lib/riemann/dash/config.rb', line 38

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

#load_config(path) ⇒ Object

Executes the configuration file.



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

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



54
55
56
# File 'lib/riemann/dash/config.rb', line 54

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

#load_controllers_from(dir) ⇒ Object

Load controllers.



87
88
89
90
91
# File 'lib/riemann/dash/config.rb', line 87

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

#setup_default_valuesObject



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

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



62
63
64
65
# File 'lib/riemann/dash/config.rb', line 62

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

#setup_storage_backendObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/riemann/dash/config.rb', line 67

def setup_storage_backend
  uri = URI.parse(ws_config_file)
  backend = case uri.scheme
          when "s3"
            begin
              require 'riemann/dash/browser_config/s3'
              Riemann::Dash::BrowserConfig::S3.new(uri.host, uri.path.sub(/^\//, ''), store[:s3_config])
            rescue LoadError
              raise Exception.new 'Fog library required to save to S3. Run: "gem install fog"'
            end
          when nil, "file"
            require 'riemann/dash/browser_config/file'
            Riemann::Dash::BrowserConfig::File.new(uri.path)
          else
            raise Exception.new "Unknown backend for #{ws_config_file}"
          end
  Riemann::Dash::BrowserConfig.backend = backend
end

#setup_viewsObject



58
59
60
# File 'lib/riemann/dash/config.rb', line 58

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.



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/riemann/dash/config.rb', line 99

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

#ws_config_fileObject



29
30
31
# File 'lib/riemann/dash/config.rb', line 29

def ws_config_file
  store[:ws_config]
end