Class: TJSON::DataType::Set
- Inherits:
-
NonScalar
- Object
- TJSON::DataType
- NonScalar
- TJSON::DataType::Set
- Defined in:
- lib/tjson/datatype/set.rb
Overview
TJSON sets
Constant Summary
Constants inherited from TJSON::DataType
Instance Attribute Summary
Attributes inherited from NonScalar
Class Method Summary collapse
-
.identify_type(array) ⇒ Object
Determine the type of a Ruby array (for serialization).
Instance Method Summary collapse
Methods inherited from NonScalar
Methods inherited from TJSON::DataType
Constructor Details
This class inherits a constructor from TJSON::DataType::NonScalar
Class Method Details
.identify_type(array) ⇒ Object
Determine the type of a Ruby array (for serialization)
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/tjson/datatype/set.rb', line 8 def self.identify_type(array) inner_type = nil array.each do |elem| t = TJSON::DataType.identify_type(elem) inner_type ||= t raise TJSON::TypeError, "set contains heterogenous types: #{array.inspect}" unless inner_type == t end new(inner_type) end |
Instance Method Details
#decode(array) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/tjson/datatype/set.rb', line 24 def decode(array) raise TJSON::TypeError, "expected Array, got #{array.class}" unless array.is_a?(::Array) if @inner_type result = ::Set.new(array.map { |o| @inner_type.decode(o) }) raise TJSON::ParseError, "set contains duplicate items" if result.size < array.size return result end return ::Set.new if array.empty? raise TJSON::ParseError, "no inner type specified for non-empty set: #{array.inspect}" end |
#encode(set) ⇒ Object
37 38 39 |
# File 'lib/tjson/datatype/set.rb', line 37 def encode(set) set.map { |o| TJSON::DataType.generate(o) } end |
#tag ⇒ Object
20 21 22 |
# File 'lib/tjson/datatype/set.rb', line 20 def tag "S<#{@inner_type.tag}>" end |