Class: JSONAPIonify::Types::BaseType

Inherits:
Object
  • Object
show all
Includes:
Callbacks
Defined in:
lib/jsonapionify/types.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Callbacks

#run_callbacks

Constructor Details

#initialize(**options) ⇒ BaseType

Returns a new instance of BaseType.



59
60
61
62
63
# File 'lib/jsonapionify/types.rb', line 59

def initialize(**options)
  run_callbacks :initialize do
    @options = options
  end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



57
58
59
# File 'lib/jsonapionify/types.rb', line 57

def options
  @options
end

Class Method Details

.dumper(&block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/jsonapionify/types.rb', line 27

def self.dumper(&block)
  meth = instance_method define_method(:dump, &block)
  define_method(:dump) do |value|
    return nil if value.nil? && !not_null?
    raise NotNullError if value.nil? && not_null?
    meth.bind(self).call(value)
  end
end

.loader(&block) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/jsonapionify/types.rb', line 36

def self.loader(&block)
  meth = instance_method define_method(:load, &block)
  define_method(:load) do |value|
    return nil if value.nil? && !not_null?
    raise NotNullError if value.nil? && not_null?
    meth.bind(self).call(value)
  end
end

Instance Method Details

#nameObject



53
54
55
# File 'lib/jsonapionify/types.rb', line 53

def name
  self.class.name.split('::').last.chomp('Type')
end

#not_null!Object



71
72
73
74
# File 'lib/jsonapionify/types.rb', line 71

def not_null!
  @not_null = true
  self
end

#not_null?Boolean

Returns:



76
77
78
# File 'lib/jsonapionify/types.rb', line 76

def not_null?
  !!@not_null
end

#to_sObject



65
66
67
68
69
# File 'lib/jsonapionify/types.rb', line 65

def to_s
  name = self.class.name.split('::').last.chomp('Type')
  name << "[#{options[:of].to_s}]" if options[:of]
  name
end