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.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ralf/config.rb', line 36 def initialize( = {}) @options = .dup # assign defaults @options[:now] ||= nil @options[:range] ||= 'today' @options[:cache_dir] ||= (0 == Process.uid ? ROOT_DEFAULT_CACHE_DIR : File.(USER_DEFAULT_CACHE_DIR)) (@options) end |
Instance Attribute Details
#aws_access_key_id ⇒ Object
Returns the value of attribute aws_access_key_id.
12 13 14 |
# File 'lib/ralf/config.rb', line 12 def aws_access_key_id @aws_access_key_id end |
#aws_secret_access_key ⇒ Object
Returns the value of attribute aws_secret_access_key.
12 13 14 |
# File 'lib/ralf/config.rb', line 12 def aws_secret_access_key @aws_secret_access_key end |
#buckets ⇒ Object
Returns the value of attribute buckets.
12 13 14 |
# File 'lib/ralf/config.rb', line 12 def buckets @buckets end |
#cache_dir(variables) ⇒ Object
113 114 115 |
# File 'lib/ralf/config.rb', line 113 def cache_dir(variables) Ralf::Interpolation.interpolate(@cache_dir, variables, [:bucket]) end |
#debug=(value) ⇒ Object (writeonly)
Sets the attribute debug
19 20 21 |
# File 'lib/ralf/config.rb', line 19 def debug=(value) @debug = value end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
24 25 26 |
# File 'lib/ralf/config.rb', line 24 def errors @errors end |
#now ⇒ Object
Returns the value of attribute now.
12 13 14 |
# File 'lib/ralf/config.rb', line 12 def now @now end |
#output_file(variables) ⇒ Object
105 106 107 |
# File 'lib/ralf/config.rb', line 105 def output_file(variables) Ralf::Interpolation.interpolate(@output_file, variables) end |
Class Method Details
.load_file(filepath) ⇒ Object
32 33 34 |
# File 'lib/ralf/config.rb', line 32 def self.load_file(filepath) self.new(YAML.load_file(filepath)) end |
Instance Method Details
#==(other) ⇒ Object
compare two configurations
58 59 60 |
# File 'lib/ralf/config.rb', line 58 def ==(other) @options == other. end |
#cache_dir_format ⇒ Object
117 118 119 |
# File 'lib/ralf/config.rb', line 117 def cache_dir_format @cache_dir end |
#debug? ⇒ Boolean
53 54 55 |
# File 'lib/ralf/config.rb', line 53 def debug? @debug || false end |
#empty? ⇒ Boolean
121 122 123 |
# File 'lib/ralf/config.rb', line 121 def empty? @options.empty? end |
#merge!(options) ⇒ Object
47 48 49 50 51 |
# File 'lib/ralf/config.rb', line 47 def merge!() @options.merge!() () end |
#output_file_format ⇒ Object
109 110 111 |
# File 'lib/ralf/config.rb', line 109 def output_file_format @output_file end |
#output_file_missing? ⇒ Boolean
143 144 145 |
# File 'lib/ralf/config.rb', line 143 def output_file_missing? !@output_file end |
#range ⇒ Object
return the range
63 64 65 66 |
# File 'lib/ralf/config.rb', line 63 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
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 103 |
# File 'lib/ralf/config.rb', line 69 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
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/ralf/config.rb', line 125 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
136 137 138 139 140 141 |
# File 'lib/ralf/config.rb', line 136 def validate! valid? unless @errors.empty? raise ConfigurationError.new(@errors.join(', ')) end end |