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.



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

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.



13
14
15
# File 'lib/riak/crdt/inner_set.rb', line 13

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.



24
25
26
# File 'lib/riak/crdt/inner_set.rb', line 24

def parent
  @parent
end

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

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

Returns:



18
19
20
# File 'lib/riak/crdt/inner_set.rb', line 18

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.



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

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



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

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



75
76
77
# File 'lib/riak/crdt/inner_set.rb', line 75

def context?
  @parent.context?
end

#empty?Boolean

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

Returns:

  • (Boolean)

    whether this structure is empty or not



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

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



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

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

#pretty_print(pp) ⇒ Object



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

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:



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

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



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

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.



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

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