Class: Deplate::Counters
- Inherits:
-
Object
- Object
- Deplate::Counters
- Defined in:
- lib/deplate/counters.rb
Overview
counters.rb @Author: Thomas Link (micathom AT gmail com) @License: GPL (see www.gnu.org/licenses/gpl.txt) @Created: 31-Dez-2005. @Revision: 0.17
Instance Method Summary collapse
- #def_counter(name, args = {}) ⇒ Object
- #get(name, compound = false) ⇒ Object
- #get_parent(name, compound = false) ⇒ Object
- #get_s(name, args = {}) ⇒ Object
-
#increase(name, args = {}) ⇒ Object
Increase a counter.
-
#initialize(deplate) ⇒ Counters
constructor
A new instance of Counters.
- #is_defined?(name) ⇒ Boolean
- #is_hierarchical?(name) ⇒ Boolean
- #reset(name = nil, args = {}) ⇒ Object
- #set(name, value, &etc) ⇒ Object
- #value_to_string(value, args = {}) ⇒ Object
Constructor Details
#initialize(deplate) ⇒ Counters
Returns a new instance of Counters.
10 11 12 13 |
# File 'lib/deplate/counters.rb', line 10 def initialize(deplate) @deplate = deplate @counters = {} end |
Instance Method Details
#def_counter(name, args = {}) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/deplate/counters.rb', line 15 def def_counter(name, args={}) parent_counter = args[:parent] if parent_counter parent, parent_level = parent_counter.split(/[.,:;@-_<> ]+/) parent_level = parent_level.to_i if parent_level > 0 parent_level -= 1 end pc = @counters[parent] if pc pc[:children] << {:name => name, :level => parent_level} else Deplate::Core.log(['Unknown counter', pc], :error) end else parent = nil parent_level = nil end init = args[:init] || [0] @counters[name] = { :value => init.dup, :init => init.dup, :children => [], :parent => parent, :parent_level => parent_level, } end |
#get(name, compound = false) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/deplate/counters.rb', line 47 def get(name, compound=false) c = @counters[name] if c if compound c else [get_parent(name), c[:value]].compact.flatten end else Deplate::Core.log(['Unknown counter', name], :error) nil end end |
#get_parent(name, compound = false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/deplate/counters.rb', line 61 def get_parent(name, compound=false) c = get(name, true) p = c[:parent] if p pl = c[:parent_level] v = get(p, compound) if compound or !pl v else v[0..pl] end else nil end end |
#get_s(name, args = {}) ⇒ Object
161 162 163 164 |
# File 'lib/deplate/counters.rb', line 161 def get_s(name, args={}) c = get(name) value_to_string(c, args) if c end |
#increase(name, args = {}) ⇒ Object
Increase a counter. Optional arguments:
+:by+:: The value to add
+:level+:: If the counter is hierarchical (an array), increase
this level & reset all sublevels
+:to_s+:: Return the counter as string
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/deplate/counters.rb', line 118 def increase(name, args={}) by = args[:by] || 1 cc = get(name, true) if cc c = cc[:value] mx = c.size - 1 lv = args[:level] lv = lv ? (lv.to_i - 1) : mx if lv < 0 lv = 0 end if lv < mx c = c[0..lv] end if lv > mx for i in (mx+1)..(lv-1) c[i] = 0 end c[lv] = by else c[lv] += by end set(name, c) do |c| c[:container] = args[:container] end cc[:children].each do |child| cn = child[:name] cl = child[:level] if cl >= lv reset(cn) end end if args[:to_s] return value_to_string(get(name)) else return self end else Deplate::Core.log(['Unknown counter', name], :error) return nil end end |
#is_defined?(name) ⇒ Boolean
43 44 45 |
# File 'lib/deplate/counters.rb', line 43 def is_defined?(name) @counters.keys.include?(name) end |
#is_hierarchical?(name) ⇒ Boolean
166 167 168 169 |
# File 'lib/deplate/counters.rb', line 166 def is_hierarchical?(name) c = get(name) c.kind_of?(Array) end |
#reset(name = nil, args = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/deplate/counters.rb', line 88 def reset(name=nil, args={}) if name c = get(name, true) if c c[:value] = c[:init].dup # i = c[:init].dup # l = args[:level] # if l # v = c[:value] # v = v[0..(l - 1)] # else # v = i # end # set(name, v) c[:children].each do |cn, cl| reset(cn, :level => l) end end else for name in @counters.keys reset(name) end end end |
#set(name, value, &etc) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/deplate/counters.rb', line 77 def set(name, value, &etc) c = get(name, true) if c c[:value] = value if etc etc.call(c) end end self end |
#value_to_string(value, args = {}) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/deplate/counters.rb', line 171 def value_to_string(value, args={}) delta = Deplate::Core.split_list(args['delta'] || '', '.', ',; ') if value.empty? delta.join('.') else value = value.dup depth = args['depth'].to_i - 1 if depth >= 0 value = value[0..depth] end delta.each_with_index do |d,i| value[i] = (value[i] || 0) + d.to_i end value.join('.') end end |