Class: Hanover::TwoPSet
- Inherits:
-
Object
- Object
- Hanover::TwoPSet
- Defined in:
- lib/hanover/two_p_set.rb
Instance Attribute Summary collapse
-
#added ⇒ Object
Returns the value of attribute added.
-
#removed ⇒ Object
Returns the value of attribute removed.
Class Method Summary collapse
Instance Method Summary collapse
- #add(atom) ⇒ Object
- #include?(atom) ⇒ Boolean
-
#initialize ⇒ TwoPSet
constructor
A new instance of TwoPSet.
- #members ⇒ Object
- #merge(other) ⇒ Object
- #remove(atom) ⇒ Object
- #to_json(*args) ⇒ Object
Constructor Details
Instance Attribute Details
#added ⇒ Object
Returns the value of attribute added.
3 4 5 |
# File 'lib/hanover/two_p_set.rb', line 3 def added @added end |
#removed ⇒ Object
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
21 22 23 |
# File 'lib/hanover/two_p_set.rb', line 21 def include?(atom) members.include? atom end |
#members ⇒ Object
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 |