Class: Familia::Counter
- Defined in:
- lib/familia/data_type/types/counter.rb
Instance Attribute Summary
Attributes inherited from DataType
#dump_method, #keystring, #load_method, #opts, #parent
Attributes included from Features
Instance Method Summary collapse
-
#atomic_increment_and_get(amount = 1) ⇒ Object
-
#increment_if_less_than(threshold, amount = 1) ⇒ Object
-
#initialize(*args) ⇒ Counter
constructor
A new instance of Counter.
-
#reset(val = 0) ⇒ Object
Enhanced counter semantics.
-
#value ⇒ Object
-
#value=(val) ⇒ Object
Override to ensure integer serialization.
Methods inherited from String
#append, #char_count, #decrement, #decrementby, #del, #empty?, #getbit, #getrange, #getset, #increment, #incrementby, #init, #nil?, #setbit, #setnx, #setrange, #to_i, #to_s
Methods inherited from DataType
#class?, #dbclient, #dbkey, #logical_database, #parent?, #parent_class?, #parent_instance?, #uri
Methods included from Features
Methods included from DataType::ClassMethods
#has_relations?, #inherited, #logical_database, #register, #uri, #valid_keys_only
Methods included from DataType::Serialization
#deserialize_value, #deserialize_values, #deserialize_values_with_nil, #serialize_value
Methods included from DataType::Commands
#current_expiration, #delete!, #echo, #exists?, #expire, #expireat, #move, #persist, #rename, #renamenx, #type
Methods included from Base
add_feature, #generate_id, #to_s, #update_expiration, #uuid
Constructor Details
#initialize(*args) ⇒ Counter
Returns a new instance of Counter.
5 6 7 8 |
# File 'lib/familia/data_type/types/counter.rb', line 5 def initialize(*args) super @opts[:default] ||= 0 end |
Instance Method Details
#atomic_increment_and_get(amount = 1) ⇒ Object
23 24 25 |
# File 'lib/familia/data_type/types/counter.rb', line 23 def atomic_increment_and_get(amount = 1) incrementby(amount) end |
#increment_if_less_than(threshold, amount = 1) ⇒ Object
15 16 17 18 19 20 21 |
# File 'lib/familia/data_type/types/counter.rb', line 15 def increment_if_less_than(threshold, amount = 1) current = to_i return false if current >= threshold incrementby(amount) true end |
#reset(val = 0) ⇒ Object
Enhanced counter semantics
11 12 13 |
# File 'lib/familia/data_type/types/counter.rb', line 11 def reset(val = 0) set(val).to_s.eql?('OK') end |
#value ⇒ Object
32 33 34 |
# File 'lib/familia/data_type/types/counter.rb', line 32 def value super.to_i end |
#value=(val) ⇒ Object
Override to ensure integer serialization
28 29 30 |
# File 'lib/familia/data_type/types/counter.rb', line 28 def value=(val) super(val.to_i) end |