Class: Ralf::Config
- Inherits:
-
Object
- Object
- Ralf::Config
- Defined in:
- lib/ralf/config.rb
Defined Under Namespace
Classes: ConfigurationError, RangeError
Constant Summary collapse
- USER_DEFAULT_CACHE_DIR =
'~/.ralf/:bucket'- ROOT_DEFAULT_CACHE_DIR =
'/var/log/ralf/:bucket'
Instance Attribute Summary collapse
-
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
-
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
-
#buckets ⇒ Object
Returns the value of attribute buckets.
- #cache_dir(variables) ⇒ Object
-
#debug ⇒ Object
writeonly
Sets the attribute debug.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#now ⇒ Object
Returns the value of attribute now.
- #output_file(variables) ⇒ Object
Class Method Summary collapse
Instance Method Summary collapse
-
#==(other) ⇒ Object
compare two configurations.
- #cache_dir_format ⇒ Object
- #debug? ⇒ Boolean
- #empty? ⇒ Boolean
-
#initialize(options = {}) ⇒ Config
constructor
A new instance of Config.
- #merge!(options) ⇒ Object
- #output_file_format ⇒ Object
- #output_file_missing? ⇒ Boolean
-
#range ⇒ Object
return the range.
-
#range=(args) ⇒ Object
set a range by a single Chronic expression or an array of 1 or 2 Chronic expressions.
- #valid? ⇒ Boolean
- #validate! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Config
Returns a new instance of Config.
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/ralf/config.rb', line 35 def initialize( = {}) = .dup # assign defaults [:now] ||= nil [:range] ||= 'today' [:cache_dir] ||= (0 == Process.uid ? ROOT_DEFAULT_CACHE_DIR : File.(USER_DEFAULT_CACHE_DIR)) () end |
Instance Attribute Details
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
11 12 13 |
# File 'lib/ralf/config.rb', line 11 def aws_access_key_id @aws_access_key_id end |
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
11 12 13 |
# File 'lib/ralf/config.rb', line 11 def aws_secret_access_key @aws_secret_access_key end |
#buckets ⇒ Object
Returns the value of attribute buckets.
11 12 13 |
# File 'lib/ralf/config.rb', line 11 def buckets @buckets end |
#cache_dir(variables) ⇒ Object
112 113 114 |
# File 'lib/ralf/config.rb', line 112 def cache_dir(variables) Ralf::Interpolation.interpolate(@cache_dir, variables, [:bucket]) end |
#debug=(value) ⇒ Object (writeonly)
Sets the attribute debug
18 19 20 |
# File 'lib/ralf/config.rb', line 18 def debug=(value) @debug = value end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
23 24 25 |
# File 'lib/ralf/config.rb', line 23 def errors @errors end |
#now ⇒ Object
Returns the value of attribute now.
11 12 13 |
# File 'lib/ralf/config.rb', line 11 def now @now end |
#output_file(variables) ⇒ Object
104 105 106 |
# File 'lib/ralf/config.rb', line 104 def output_file(variables) Ralf::Interpolation.interpolate(@output_file, variables) end |
Class Method Details
.load_file(filepath) ⇒ Object
31 32 33 |
# File 'lib/ralf/config.rb', line 31 def self.load_file(filepath) self.new(YAML.load_file(filepath)) end |
Instance Method Details
#==(other) ⇒ Object
compare two configurations
57 58 59 |
# File 'lib/ralf/config.rb', line 57 def ==(other) == other. end |
#cache_dir_format ⇒ Object
116 117 118 |
# File 'lib/ralf/config.rb', line 116 def cache_dir_format @cache_dir end |
#debug? ⇒ Boolean
52 53 54 |
# File 'lib/ralf/config.rb', line 52 def debug? @debug || false end |
#empty? ⇒ Boolean
120 121 122 |
# File 'lib/ralf/config.rb', line 120 def empty? .empty? end |
#merge!(options) ⇒ Object
46 47 48 49 50 |
# File 'lib/ralf/config.rb', line 46 def merge!() .merge!() () end |
#output_file_format ⇒ Object
108 109 110 |
# File 'lib/ralf/config.rb', line 108 def output_file_format @output_file end |
#output_file_missing? ⇒ Boolean
142 143 144 |
# File 'lib/ralf/config.rb', line 142 def output_file_missing? !@output_file end |
#range ⇒ Object
return the range
62 63 64 65 |
# File 'lib/ralf/config.rb', line 62 def range raise ArgumentError unless 2 == @range.size Range.new(time_to_date(@range.first), time_to_date(@range.last)) # inclusive end |
#range=(args) ⇒ Object
set a range by a single Chronic expression or an array of 1 or 2 Chronic expressions
68 69 70 71 72 73 74 75 76 77 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 |
# File 'lib/ralf/config.rb', line 68 def range=(args) args ||= [] args = [args] unless args.is_a?(Array) @range_value = args raise ArgumentError.new("too many range items") if args.size > 2 range = [] args.each_with_index do |expr, i| raise RangeError if i > 1 # this should have been caught by ArgumentError before the loop = { :context => :past, :guess => false } if self.now .merge!(:now => Chronic.parse(self.now, :context => :past)) end if span = Chronic.parse(expr, ) if span.width <= 24 * 3600 # on same date range << span.begin else raise RangeError, "range end '#{expr}' is not a single date" if i > 0 range << span.begin range << span.end + (self.now ? 0 : -1) end else raise RangeError, "invalid expression '#{expr}'" end end range = [ Date.today ] if range.empty? # empty range means today range = range*2 if 1 == range.size # single day has begin == end @range = range end |
#valid? ⇒ Boolean
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/ralf/config.rb', line 124 def valid? @errors = [] unless (@aws_access_key_id || ENV['AWS_ACCESS_KEY_ID']) @errors << 'aws_access_key_id missing' end unless (@aws_secret_access_key || ENV['AWS_SECRET_ACCESS_KEY']) @errors << 'aws_secret_access_key missing' end end |
#validate! ⇒ Object
135 136 137 138 139 140 |
# File 'lib/ralf/config.rb', line 135 def validate! valid? unless @errors.empty? raise ConfigurationError.new(@errors.join(', ')) end end |