Class: Rosetta::Serializers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rosetta/serializers/base.rb

Direct Known Subclasses

CSV

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ Base

Returns a new instance of Base.



31
32
33
34
# File 'lib/rosetta/serializers/base.rb', line 31

def initialize(elements)
  @elements = elements.dup.freeze
  validate_input!
end

Instance Attribute Details

#elementsObject (readonly)

Returns the value of attribute elements.



8
9
10
# File 'lib/rosetta/serializers/base.rb', line 8

def elements
  @elements
end

Class Method Details

.call(elements) ⇒ Object



19
20
21
22
23
# File 'lib/rosetta/serializers/base.rb', line 19

def call(elements)
  new(elements).call
rescue StandardError
  raise SerializationError
end

.inherited(new_serializer) ⇒ Object



13
14
15
16
17
# File 'lib/rosetta/serializers/base.rb', line 13

def inherited(new_serializer)
  key = new_serializer.name.match(/^(.*?)(Serializer)?$/)[1]
  key = key.split("::").last
  Serializers.register(key.underscore.to_sym, new_serializer)
end

.serializeObject



24
25
26
27
28
# File 'lib/rosetta/serializers/base.rb', line 24

def call(elements)
  new(elements).call
rescue StandardError
  raise SerializationError
end

.to_procObject



26
27
28
# File 'lib/rosetta/serializers/base.rb', line 26

def to_proc
  proc { |*args, &block| self.call(*args, &block) }
end

Instance Method Details

#callObject Also known as: serialize

Raises:

  • (NotImplementedError)


36
37
38
# File 'lib/rosetta/serializers/base.rb', line 36

def call
  raise NotImplementedError
end

#to_procObject



41
42
43
# File 'lib/rosetta/serializers/base.rb', line 41

def to_proc
  proc { |*args, &block| self.call(*args, &block) }
end