Class: Ralf::Config

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ralf/config.rb', line 37

def initialize(options = {})
  @options = options.dup
  
  # assign defaults
  @options[:now]       ||= nil
  @options[:range]     ||= 'today'
  @options[:cache_dir] ||= (0 == Process.uid ? ROOT_DEFAULT_CACHE_DIR : File.expand_path(USER_DEFAULT_CACHE_DIR))
  @options[:translate_options] ||= {}

  assign_options(@options)
end

Instance Attribute Details

#aws_access_key_idObject

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_keyObject

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

#bucketsObject

Returns the value of attribute buckets.



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

def buckets
  @buckets
end

#cache_dir(variables) ⇒ Object



119
120
121
# File 'lib/ralf/config.rb', line 119

def cache_dir(variables)
  Ralf::Interpolation.interpolate(@cache_dir, variables, [:bucket])
end

#debug=(value) ⇒ Object (writeonly)

Sets the attribute debug

Parameters:

  • value

    the value to set the attribute debug to.



19
20
21
# File 'lib/ralf/config.rb', line 19

def debug=(value)
  @debug = value
end

#errorsObject (readonly)

Returns the value of attribute errors.



24
25
26
# File 'lib/ralf/config.rb', line 24

def errors
  @errors
end

#nowObject

Returns the value of attribute now.



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

def now
  @now
end

#output_file(variables) ⇒ Object



111
112
113
# File 'lib/ralf/config.rb', line 111

def output_file(variables)
  Ralf::Interpolation.interpolate(@output_file, variables)
end

#translate_optionsObject

Returns the value of attribute translate_options.



25
26
27
# File 'lib/ralf/config.rb', line 25

def translate_options
  @translate_options
end

Class Method Details

.load_file(filepath) ⇒ Object



33
34
35
# File 'lib/ralf/config.rb', line 33

def self.load_file(filepath)
  self.new(YAML.load_file(filepath))
end

Instance Method Details

#==(other) ⇒ Object

compare two configurations



60
61
62
# File 'lib/ralf/config.rb', line 60

def ==(other)
  @options == other.options
end

#cache_dir_formatObject



123
124
125
# File 'lib/ralf/config.rb', line 123

def cache_dir_format
  @cache_dir
end

#debug?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ralf/config.rb', line 55

def debug?
  @debug || false
end

#empty?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/ralf/config.rb', line 127

def empty?
  @options.empty?
end

#merge!(options) ⇒ Object



49
50
51
52
53
# File 'lib/ralf/config.rb', line 49

def merge!(options)
  @options.merge!(options)

  assign_options(options)
end

#output_file_formatObject



115
116
117
# File 'lib/ralf/config.rb', line 115

def output_file_format
  @output_file
end

#output_file_missing?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/ralf/config.rb', line 149

def output_file_missing?
  !@output_file
end

#rangeObject

return the range

Raises:

  • (ArgumentError)


65
66
67
68
# File 'lib/ralf/config.rb', line 65

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

Raises:

  • (ArgumentError)


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
104
105
# File 'lib/ralf/config.rb', line 71

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
    
    chronic_options = { :context => :past, :guess => false }
    if self.now
      chronic_options.merge!(:now => Chronic.parse(self.now, :context => :past))
    end
    
    if span = Chronic.parse(expr, chronic_options)
      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

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
# File 'lib/ralf/config.rb', line 131

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



142
143
144
145
146
147
# File 'lib/ralf/config.rb', line 142

def validate!
  valid?
  unless @errors.empty?
    raise ConfigurationError.new(@errors.join(', '))
  end
end