Class: Riak::Crdt::InnerSet

Inherits:
Object show all
Defined in:
lib/riak/crdt/inner_set.rb

Overview

The InnerSet is similar to a Set, except it is part of a Map (or an InnerMap inside of a Map). It is usually accessed through a TypedCollection.

Just like a Set, it’s a set of Strings that can be added to or removed from.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, value = []) ⇒ InnerSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of InnerSet.



41
42
43
44
45
46
# File 'lib/riak/crdt/inner_set.rb', line 41

def initialize(parent, value = [])
  @parent = parent
  frozen_value = value.to_a.tap{ |v| v.each(&:freeze) }
  @value = ::Set.new frozen_value
  @value.freeze
end

Instance Attribute Details

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name of this set inside a map.



27
28
29
# File 'lib/riak/crdt/inner_set.rb', line 27

def name
  @name
end

#parentObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The parent of this counter.



38
39
40
# File 'lib/riak/crdt/inner_set.rb', line 38

def parent
  @parent
end

#value::Set (readonly) Also known as: members

The Set value of this Riak::Crdt::InnerSet.

Returns:



32
33
34
# File 'lib/riak/crdt/inner_set.rb', line 32

def value
  @value
end

Class Method Details

.deleteObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



109
110
111
112
113
# File 'lib/riak/crdt/inner_set.rb', line 109

def self.delete
  Operation::Delete.new.tap do |op|
    op.type = :set
  end
end

Instance Method Details

#add(element) ⇒ Object

Parameters:

  • element (String)

    the element to add



73
74
75
# File 'lib/riak/crdt/inner_set.rb', line 73

def add(element)
  @parent.operate name, update(add: element)
end

#context?Boolean

Does the map containing this set have the context necessary to remove elements?

Returns:

  • (Boolean)

    if the set has a defined context



89
90
91
# File 'lib/riak/crdt/inner_set.rb', line 89

def context?
  @parent.context?
end

#empty?Boolean

Check if this Riak::Crdt::InnerSet is empty.

Returns:

  • (Boolean)

    whether this structure is empty or not



58
59
60
# File 'lib/riak/crdt/inner_set.rb', line 58

def empty?
  value.empty?
end

#include?(element) ⇒ Boolean

Check if a given string is in this structure.

Parameters:

  • element (String)

    candidate string to check for inclusion

Returns:

  • (Boolean)

    whether the candidate is in this set or not



66
67
68
# File 'lib/riak/crdt/inner_set.rb', line 66

def include?(element)
  value.include? element
end

#pretty_print(pp) ⇒ Object



93
94
95
96
97
98
# File 'lib/riak/crdt/inner_set.rb', line 93

def pretty_print(pp)
  pp.object_group self do
    pp.breakable
    pp.pp to_a
  end
end

#remove(element) ⇒ Object

Remove a String from this set

Parameters:

  • element (String)

    the element to remove

Raises:



80
81
82
83
# File 'lib/riak/crdt/inner_set.rb', line 80

def remove(element)
  raise CrdtError::SetRemovalWithoutContextError unless context?
  @parent.operate name, update(remove: element)
end

#to_aArray

Casts this Riak::Crdt::InnerSet to an Array.

Returns:

  • (Array)

    an array of all the members of this set



51
52
53
# File 'lib/riak/crdt/inner_set.rb', line 51

def to_a
  value.to_a
end

#update(changes) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



101
102
103
104
105
106
# File 'lib/riak/crdt/inner_set.rb', line 101

def update(changes)
  Operation::Update.new.tap do |op|
    op.value = changes.symbolize_keys
    op.type = :set
  end
end