Class: PRRD::Database
- Inherits:
-
Object
- Object
- PRRD::Database
- Defined in:
- lib/prrd/database.rb,
lib/prrd/database/archive.rb,
lib/prrd/database/datasource.rb
Overview
PRRD Database class
Defined Under Namespace
Classes: Archive, Datasource
Instance Attribute Summary collapse
-
#archives ⇒ Object
Returns the value of attribute archives.
-
#datasources ⇒ Object
Returns the value of attribute datasources.
-
#path ⇒ Object
Accessors.
-
#start ⇒ Object
Returns the value of attribute start.
-
#step ⇒ Object
Returns the value of attribute step.
Instance Method Summary collapse
-
#<<(object) ⇒ Object
Add an object.
-
#add_archive(archive) ⇒ Object
Add an archive object.
-
#add_archives(archives) ⇒ Object
Add archive objects.
-
#add_datasource(datasource) ⇒ Object
Add a datasource object.
-
#add_datasources(datasources) ⇒ Object
Add datasource objects.
-
#check_file ⇒ Object
Check database existence.
-
#create ⇒ String
Create a database.
-
#exists? ⇒ Boolean
Does database file exist?.
-
#initialize(values = nil) ⇒ Database
constructor
Constructor.
-
#update(timestamp = nil, *values) ⇒ String
Update a database.
Constructor Details
#initialize(values = nil) ⇒ Database
Constructor
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/prrd/database.rb', line 15 def initialize(values = nil) @datasources = [] @archives = [] @start = Time.now.to_i - 86_400 @step = 300 unless values.nil? values.each do |k, v| m = "#{k}=".to_sym next unless respond_to? m send m, v end end end |
Instance Attribute Details
#archives ⇒ Object
Returns the value of attribute archives.
12 13 14 |
# File 'lib/prrd/database.rb', line 12 def archives @archives end |
#datasources ⇒ Object
Returns the value of attribute datasources.
12 13 14 |
# File 'lib/prrd/database.rb', line 12 def datasources @datasources end |
#path ⇒ Object
Accessors
10 11 12 |
# File 'lib/prrd/database.rb', line 10 def path @path end |
#start ⇒ Object
Returns the value of attribute start.
11 12 13 |
# File 'lib/prrd/database.rb', line 11 def start @start end |
#step ⇒ Object
Returns the value of attribute step.
11 12 13 |
# File 'lib/prrd/database.rb', line 11 def step @step end |
Instance Method Details
#<<(object) ⇒ Object
Add an object
66 67 68 69 70 71 72 73 74 |
# File 'lib/prrd/database.rb', line 66 def <<(object) if object.is_a? PRRD::Database::Datasource add_datasource object elsif object.is_a? PRRD::Database::Archive add_archive object else fail 'Can not add this kind of object in PRRD::Database' end end |
#add_archive(archive) ⇒ Object
Add an archive object
54 55 56 |
# File 'lib/prrd/database.rb', line 54 def add_archive(archive) @archives << archive end |
#add_archives(archives) ⇒ Object
Add archive objects
60 61 62 |
# File 'lib/prrd/database.rb', line 60 def add_archives(archives) @archives = archives end |
#add_datasource(datasource) ⇒ Object
Add a datasource object
42 43 44 |
# File 'lib/prrd/database.rb', line 42 def add_datasource(datasource) @datasources << datasource end |
#add_datasources(datasources) ⇒ Object
Add datasource objects
48 49 50 |
# File 'lib/prrd/database.rb', line 48 def add_datasources(datasources) @datasources = datasources end |
#check_file ⇒ Object
Check database existence
36 37 38 |
# File 'lib/prrd/database.rb', line 36 def check_file fail 'Database path is missing' if @path.nil? || !exists? end |
#create ⇒ String
Create a database
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/prrd/database.rb', line 78 def create File.write @path, '' cmd = [] cmd << "#{PRRD.bin} create #{@path}" cmd << "--start #{@start} --step #{@step}" fail 'Datasources are missing' if @datasources.empty? @datasources.map { |e| cmd << e.to_s } fail 'Archives are missing' if @archives.empty? @archives.map { |e| cmd << e.to_s } # Execute PRRD.execute cmd.join ' ' end |
#exists? ⇒ Boolean
Does database file exist?
31 32 33 |
# File 'lib/prrd/database.rb', line 31 def exists? File.exist? @path end |
#update(timestamp = nil, *values) ⇒ String
Update a database
99 100 101 102 103 104 105 |
# File 'lib/prrd/database.rb', line 99 def update( = nil, *values) check_file ||= Time.now.to_i # Execute PRRD.execute "#{PRRD.bin} update #{@path} #{timestamp}:#{values.join ':'}" end |