-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathconfiguration.rb
More file actions
79 lines (71 loc) · 2.36 KB
/
configuration.rb
File metadata and controls
79 lines (71 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
require 'ostruct'
module Squid
# Provides an object to store global configuration settings.
#
# This class is typically not used directly, but by calling
# {Squid::Config#configure Squid.configure}, which creates and updates a
# single instance of {Squid::Configuration}.
#
# @example Set the default height for Squid graphs:
# Squid.configure do |config|
# config.height = 150
# config.steps = 4
# end
#
# @see Squid::Config for more examples.
#
# An alternative way to set global configuration settings is by storing
# them in the following environment variables:
#
# * +SQUID_HEIGHT+ to store the default graph height
#
# In case both methods are used together,
# {Squid::Config#configure Squid.configure} takes precedence.
#
# @example Set the default graph height:
# ENV['SQUID_HEIGHT'] = '150'
# ENV['SQUID_GRIDLINES'] = '4'
#
class Configuration < OpenStruct
def self.boolean
-> (value) { %w(1 t T true TRUE).include? value }
end
def self.integer
-> (value) { value.to_i }
end
def self.symbol
-> (value) { value.to_sym }
end
def self.float
-> (value) { value.to_f }
end
def self.array(proc = nil)
-> (values) { values.split.map{|value| proc ? proc.call(value) : value} }
end
ATTRIBUTES = {
baseline: {as: boolean, default: 'true'},
border: {as: boolean, default: 'false'},
chart: {as: boolean, default: 'true'},
colors: {as: array},
every: {as: integer, default: '1'},
formats: {as: array(symbol)},
height: {as: float, default: '250'},
labels: {as: array(boolean)},
legend: {as: boolean, default: 'true'},
line_widths: {as: array(float)},
col_max_width: {as: integer, default: '0'},
steps: {as: integer, default: '4'},
ticks: {as: boolean, default: 'true'},
type: {as: symbol, default: 'column'},
}
attr_accessor *ATTRIBUTES.keys
# Initialize the global configuration settings.
def initialize
ATTRIBUTES.each do |key, options|
var = "squid_#{key}".upcase
value = ENV.fetch var, options.fetch(:default, '')
public_send "#{key}=", options[:as].call(value)
end
end
end
end