Class: Hanover::TwoPSet

Inherits:
Object
  • Object
show all
Defined in:
lib/hanover/two_p_set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTwoPSet

Returns a new instance of TwoPSet.



5
6
7
8
# File 'lib/hanover/two_p_set.rb', line 5

def initialize
  self.added = GSet.new
  self.removed = GSet.new
end

Instance Attribute Details

#addedObject

Returns the value of attribute added.



3
4
5
# File 'lib/hanover/two_p_set.rb', line 3

def added
  @added
end

#removedObject

Returns the value of attribute removed.



3
4
5
# File 'lib/hanover/two_p_set.rb', line 3

def removed
  @removed
end

Class Method Details

.from_json(json) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/hanover/two_p_set.rb', line 10

def self.from_json(json)
  h = JSON.parse json, symbolize_names: true
  raise ArgumentError.new 'unexpected type field in JSON' unless h[:type] == 'TwoPSet'

  tps = new
  tps.added = GSet.from_json h[:a].to_json
  tps.removed = GSet.from_json h[:r].to_json
  
  return tps
end

Instance Method Details

#add(atom) ⇒ Object



29
30
31
32
33
# File 'lib/hanover/two_p_set.rb', line 29

def add(atom)
  raise ArgumentError.new 'already removed' if self.removed.include? atom
  
  self.added.add atom
end

#include?(atom) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/hanover/two_p_set.rb', line 21

def include?(atom)
  members.include? atom
end

#membersObject



25
26
27
# File 'lib/hanover/two_p_set.rb', line 25

def members
  self.added.members - self.removed.members
end

#merge(other) ⇒ Object



39
40
41
42
# File 'lib/hanover/two_p_set.rb', line 39

def merge(other)
  self.added.merge other.added
  self.removed.merge other.removed
end

#remove(atom) ⇒ Object



35
36
37
# File 'lib/hanover/two_p_set.rb', line 35

def remove(atom)
  self.removed.add atom
end

#to_json(*args) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/hanover/two_p_set.rb', line 44

def to_json(*args)
  {
    type: 'TwoPSet',
    a: self.added,
    r: self.removed
    }.to_json *args
end