Class: Dotdot::Base
- Inherits:
-
Object
- Object
- Dotdot::Base
- Defined in:
- lib/dotdot/base.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #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(object) ⇒ Base
constructor
A new instance of Base.
- #prefix(prefix, &block) ⇒ Object
- #set(key, value, group = nil) ⇒ Object
- #update(key, value, group = nil) ⇒ Object
Constructor Details
#initialize(object) ⇒ Base
Returns a new instance of Base.
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/dotdot/base.rb', line 5 def initialize(object) @database = nil @object = object @options = object. @logger = Logger.new(STDOUT) @sql_options = { :logger => @logger, :sql_log_level => :info } if @options[: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/dotdot/base.rb', line 3 def database @database end |
Instance Method Details
#get(key, group = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dotdot/base.rb', line 63 def get(key, group = nil) group ||= @object.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
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dotdot/base.rb', line 85 def get_children(key, group = nil) group ||= @object.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
79 80 81 82 83 |
# File 'lib/dotdot/base.rb', line 79 def get_if_key_exists(key, group = nil) group ||= @object.group.to_s get(key, group) if has_key?(key, group) end |
#globals(&block) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/dotdot/base.rb', line 34 def globals(&block) @group = "globals" @database.transaction do yield end end |
#group(group = nil, &block) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/dotdot/base.rb', line 26 def group(group = nil, &block) @group = group @database.transaction do yield end end |
#has_key?(key, group) ⇒ Boolean
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dotdot/base.rb', line 49 def has_key?(key, group) group ||= @object.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
42 43 44 45 46 47 |
# File 'lib/dotdot/base.rb', line 42 def prefix(prefix, &block) @prefix_stack.push(prefix) yield stack_pop end |
#set(key, value, group = nil) ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/dotdot/base.rb', line 110 def set(key, value, group = nil) group ||= @group key = final_key(key) @dataset.insert(:key => key, :value => value, :group => group) stack_pop end |
#update(key, value, group = nil) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/dotdot/base.rb', line 17 def update(key, value, group = nil) group ||= @group key = final_key(key) @dataset.filter(:key => key, :group => group).update(:value => value) stack_pop end |