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(key, group = nil) ⇒ Object
- #final_key(key) ⇒ Object
- #get(key, group = nil) ⇒ Object
- #get_children(key, group = nil) ⇒ Object
- #get_if_key_exists(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
147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/palmade/cableguy/db.rb', line 147 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(key, group = nil) ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/palmade/cableguy/db.rb', line 41 def delete_key(key, group = nil) group ||= @group key = final_key(key) @dataset.filter(:key => key, :group => group).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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/palmade/cableguy/db.rb', line 100 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
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/palmade/cableguy/db.rb', line 122 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 |
#get_if_key_exists(key, group = nil) ⇒ Object
116 117 118 119 120 |
# File 'lib/palmade/cableguy/db.rb', line 116 def get_if_key_exists(key, group = nil) group ||= @cabler.group.to_s get(key, group) if has_key?(key, group) end |
#globals(&block) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/palmade/cableguy/db.rb', line 67 def globals(&block) @group = "globals" @database.transaction do yield end end |
#group(group = nil, &block) ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/palmade/cableguy/db.rb', line 59 def group(group = nil, &block) @group = group @database.transaction do yield end end |
#has_key?(key, group) ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/palmade/cableguy/db.rb', line 86 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
75 76 77 78 79 80 |
# File 'lib/palmade/cableguy/db.rb', line 75 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
82 83 84 |
# File 'lib/palmade/cableguy/db.rb', line 82 def stack_pop @prefix_stack.pop end |
#update(key, value, group = nil) ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/palmade/cableguy/db.rb', line 50 def update(key, value, group = nil) group ||= @group key = final_key(key) @dataset.filter(:key => key, :group => group).update(:value => value) stack_pop end |