Class: Palmade::Cableguy::DB
- Inherits:
-
Object
- Object
- Palmade::Cableguy::DB
- Defined in:
- lib/palmade/cableguy/db.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #boot ⇒ Object
- #create_table_if_needed ⇒ Object
- #delete(key, value) ⇒ Object
- #final_key(key) ⇒ Object
- #get(key, group = nil) ⇒ Object
- #get_children(key, group = nil) ⇒ Object
- #globals(&block) ⇒ Object
- #group(group = nil, &block) ⇒ Object
- #has_key?(key, group) ⇒ Boolean
-
#initialize(cabler) ⇒ DB
constructor
A new instance of DB.
- #prefix(prefix, &block) ⇒ Object
- #set(key, value, group = nil) ⇒ Object
- #stack_pop ⇒ Object
- #update(key, value, group = nil) ⇒ Object
Constructor Details
#initialize(cabler) ⇒ DB
Returns a new instance of DB.
5 6 7 8 9 10 11 12 13 |
# File 'lib/palmade/cableguy/db.rb', line 5 def initialize(cabler) @cabler = cabler @database = nil @sql_options = { :logger => @cabler.logger, :sql_log_level => :info } if @cabler.[:verbose] @sql_options[:sql_log_level] = :debug end end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
3 4 5 |
# File 'lib/palmade/cableguy/db.rb', line 3 def database @database end |
Instance Method Details
#boot ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/palmade/cableguy/db.rb', line 15 def boot @database = Sequel.sqlite(@cabler.db_path, @sql_options) @group = "" @prefix_stack = [] @dataset = @database[:cablingdatas] end |
#create_table_if_needed ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/palmade/cableguy/db.rb', line 138 def create_table_if_needed if @database.tables.include? :cablingdatas @database.drop_table :cablingdatas end @database.create_table :cablingdatas do String :key String :value String :group end end |
#delete(key, value) ⇒ Object
41 42 43 44 45 |
# File 'lib/palmade/cableguy/db.rb', line 41 def delete(key, value) key = final_key(key) @dataset.filter(:key => key).delete stack_pop end |
#final_key(key) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/palmade/cableguy/db.rb', line 23 def final_key(key) unless @prefix_stack.empty? @prefix_stack.push(key) key = nil end key ||= @prefix_stack.join('.') end |
#get(key, group = nil) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/palmade/cableguy/db.rb', line 97 def get(key, group = nil) group ||= @cabler.group.to_s val = @dataset.where(:key => key, :group => group) if val.empty? val = @dataset.where(:key => key, :group => "globals") end if val.count > 0 val.first[:value] else raise "key \'#{key}\' cannot be found!" end end |
#get_children(key, group = nil) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/palmade/cableguy/db.rb', line 113 def get_children(key, group = nil) group ||= @cabler.group.to_s values = [] res = @dataset.where(:key.like("#{key}%"), :group => group) if res.empty? res = @dataset.where(:key.like("#{key}%"), :group => "globals") end key = key.split('.') res.each do |r| res_key = r[:key].split('.') res_key = (res_key - key).shift values.push(res_key) end if values.count > 0 values & values else raise "no values for \'#{key}\'!" end end |
#globals(&block) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/palmade/cableguy/db.rb', line 64 def globals(&block) @group = "globals" @database.transaction do yield end end |
#group(group = nil, &block) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/palmade/cableguy/db.rb', line 56 def group(group = nil, &block) @group = group @database.transaction do yield end end |
#has_key?(key, group) ⇒ Boolean
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/palmade/cableguy/db.rb', line 83 def has_key?(key, group) group ||= @cabler.group.to_s val = @dataset.where(:key => key, :group => group).count if val == 0 val = @dataset.where(:key => key, :group => "globals").count val == 0 ? false : true else true end end |
#prefix(prefix, &block) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/palmade/cableguy/db.rb', line 72 def prefix(prefix, &block) @prefix_stack.push(prefix) yield stack_pop end |
#set(key, value, group = nil) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/palmade/cableguy/db.rb', line 32 def set(key, value, group = nil) group ||= @group key = final_key(key) @dataset.insert(:key => key, :value => value, :group => group) stack_pop end |
#stack_pop ⇒ Object
79 80 81 |
# File 'lib/palmade/cableguy/db.rb', line 79 def stack_pop @prefix_stack.pop end |
#update(key, value, group = nil) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/palmade/cableguy/db.rb', line 47 def update(key, value, group = nil) group ||= @group key = final_key(key) @dataset.filter(:key => key, :group => group).update(:value => value) stack_pop end |