Class: IknowParams::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/iknow_params/serializer.rb

Defined Under Namespace

Classes: ActsAsEnum, ArrayOf, Boolean, CaseSensitiveStringEnum, Date, DumpError, Duration, Float, HashOf, ISO8601, Integer, JsonWithSchema, LoadError, Nullable, Numeric, Renum, String, StringEnum, Time, Timezone, UUID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz) ⇒ Serializer

Returns a new instance of Serializer.



16
17
18
# File 'lib/iknow_params/serializer.rb', line 16

def initialize(clazz)
  @clazz = clazz
end

Instance Attribute Details

#clazzObject (readonly)

Returns the value of attribute clazz.



14
15
16
# File 'lib/iknow_params/serializer.rb', line 14

def clazz
  @clazz
end

Class Method Details

.for(name) ⇒ Object



56
57
58
# File 'lib/iknow_params/serializer.rb', line 56

def for(name)
  @registry[name.to_s]
end

.for!(name) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
# File 'lib/iknow_params/serializer.rb', line 60

def for!(name)
  s = self.for(name)
  raise ArgumentError.new("No serializer registered with name: '#{name}'") if s.nil?
  s
end

.json_value?Boolean

Returns:



52
53
54
# File 'lib/iknow_params/serializer.rb', line 52

def json_value?
  false
end

.singletonObject

Raises:

  • (ArgumentError)


48
49
50
# File 'lib/iknow_params/serializer.rb', line 48

def singleton
  raise ArgumentError.new("Singleton instance not defined for abstract serializer '#{self.name}'")
end

Instance Method Details

#dump(val, json: false) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/iknow_params/serializer.rb', line 20

def dump(val, json: false)
  matches_type!(val)
  if json && self.class.json_value?
    val
  else
    val.to_s
  end
end

#load(_val) ⇒ Object

Raises:

  • (StandardError)


29
30
31
# File 'lib/iknow_params/serializer.rb', line 29

def load(_val)
  raise StandardError.new('unimplemented')
end

#matches_type!(val, err: DumpError) ⇒ Object



37
38
39
40
41
42
# File 'lib/iknow_params/serializer.rb', line 37

def matches_type!(val, err: DumpError)
  unless matches_type?(val)
    raise err.new("Incorrect type for #{self.class.name}: #{val.inspect}:#{val.class.name}")
  end
  true
end

#matches_type?(val) ⇒ Boolean

Returns:



33
34
35
# File 'lib/iknow_params/serializer.rb', line 33

def matches_type?(val)
  val.is_a?(clazz)
end