class Riak::Crdt::TypedCollection
A collection of elements of a given type inside a {Map}.
Constants
- ALREADY_WRAPPED
- INITIALIZE_NIL
- NEEDS_NAME
Public Class Methods
@api private
# File lib/riak/crdt/typed_collection.rb, line 10 def initialize(type, parent, contents = {}) @type = type @parent = parent contents = {} if contents.nil? stringified_contents = contents.stringify_keys @contents = materialize_contents stringified_contents end
Public Instance Methods
Get the value for a given key
@param [String] key the key to get the value for @return the value for the given key
# File lib/riak/crdt/typed_collection.rb, line 76 def [](key) key = normalize_key key if include? key candidate = @contents[key] return candidate unless candidate.respond_to? :parent return candidate if candidate.parent == self end return nil if initialize_nil? new_instance = @type.new self new_instance.name = key if needs_name? new_instance end
Set
the value for a given key. Operation
of this method is only defined for {InnerCounter}, {InnerRegister}, and {InnerFlag} types.
@param [String] key the key to set a new value for @param [Boolean, String
, Integer] value the value to set at the key,
or in the case of counters, the amount to increment
# File lib/riak/crdt/typed_collection.rb, line 99 def []=(key, value) key = normalize_key key operation = @type.update value operation.name = key result = @parent.operate operation @contents[key] = @type.new self, value @contents[key].name = key if needs_name? result end
# File lib/riak/crdt/typed_collection.rb, line 53 def content_name @type.name end
Does this set have the context necessary to remove elements?
@return [Boolean] if the set has a defined context
# File lib/riak/crdt/typed_collection.rb, line 140 def context? !!@parent.context? end
Remove the entry from the map.
@param [String] key the key to remove from the map
# File lib/riak/crdt/typed_collection.rb, line 118 def delete(key) key = normalize_key key operation = @type.delete operation.name = key @parent.operate operation @contents.delete key end
Check if a value for a given key exists in this map.
@param [String] key the key to check for @return [Boolean] if the key is inside this collection
# File lib/riak/crdt/typed_collection.rb, line 68 def include?(key) @contents.include? normalize_key(key) end
# File lib/riak/crdt/typed_collection.rb, line 43 def inspect_name "contains=#{content_name}" end
@api private
# File lib/riak/crdt/typed_collection.rb, line 129 def operate(key, inner_operation) key = normalize_key key inner_operation.name = key @parent.operate inner_operation end
# File lib/riak/crdt/typed_collection.rb, line 18 def pretty_print(pp) pp.object_group self do pp.breakable pp.text inspect_name pp.comma_breakable pp.text 'parent=' @parent.pretty_print_cycle(pp) pp.comma_breakable pp.text 'contents=' pp.pp @contents end # buf = [] # buf << inspect_name # buf << # buf << "contents={#{inspect_contents}}" # "#<#{self.class.name} #{buf.join ' '}>" end
# File lib/riak/crdt/typed_collection.rb, line 47 def pretty_print_contents(_pp) @contents.map do |k, v| "#{k}=>#{v.inspect}" end.join ', ' end
# File lib/riak/crdt/typed_collection.rb, line 36 def pretty_print_cycle(pp) pp.object_group self do pp.breakable @parent.pretty_print_cycle(pp) end end
@api private
# File lib/riak/crdt/typed_collection.rb, line 58 def reparent(new_parent) self.class.new(@type, new_parent, @contents) end
# File lib/riak/crdt/typed_collection.rb, line 144 def to_value_h return @contents unless NEEDS_NAME.include? @type @contents.map do |k, v| [k, v.value] end.to_h end
Private Instance Methods
# File lib/riak/crdt/typed_collection.rb, line 158 def initialize_nil? INITIALIZE_NIL.include? @type end
# File lib/riak/crdt/typed_collection.rb, line 166 def materialize_contents(stringified_contents) stringified_contents.keys.inject(Hash.new) do |new_contents, key| new_contents.tap do |c| content = stringified_contents[key] if ALREADY_WRAPPED.include? content.class c[key] = content else c[key] = @type.new self, content end c[key].name = key if needs_name? end end end
# File lib/riak/crdt/typed_collection.rb, line 162 def needs_name? NEEDS_NAME.include? @type end
# File lib/riak/crdt/typed_collection.rb, line 154 def normalize_key(unnormalized_key) unnormalized_key.to_s end