Class: Primalize::Single
- Inherits:
-
Object
show all
- Defined in:
- lib/primalize/single.rb
Defined Under Namespace
Modules: Type
Classes: All, Any, Array, Enum, Float, Integer, Match, Number, Object, Optional, Primalizer, String, Timestamp
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(object) ⇒ Single
Returns a new instance of Single.
115
116
117
118
|
# File 'lib/primalize/single.rb', line 115
def initialize object
raise ArgumentError, "#{self.class.inspect} cannot serialize `nil'" if object.nil?
@object = object
end
|
Instance Attribute Details
Returns the value of attribute object.
113
114
115
|
# File 'lib/primalize/single.rb', line 113
def object
@object
end
|
Class Method Details
._attributes(**attrs) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/primalize/single.rb', line 16
def _attributes **attrs
@attributes ||= if self.equal? Primalize::Single
{}
else
superclass._attributes.dup
end
add_attributes attrs
@attributes
end
|
.add_attributes(attrs) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/primalize/single.rb', line 28
def add_attributes attrs
return if attrs.none?
@attributes.merge! attrs
attrs.each do |attr, type|
define_method attr do
type.coerce(object.public_send(attr))
end
end
end
|
.all(*types, &coerce) ⇒ Object
84
85
86
|
# File 'lib/primalize/single.rb', line 84
def all *types, &coerce
All.new(types, &coerce)
end
|
.any(*types, &coerce) ⇒ Object
80
81
82
|
# File 'lib/primalize/single.rb', line 80
def any *types, &coerce
Any.new(types, &coerce)
end
|
.array(*types, &coerce) ⇒ Object
52
53
54
|
# File 'lib/primalize/single.rb', line 52
def array *types, &coerce
Array.new(types, &coerce)
end
|
.attributes(**attrs) ⇒ Object
12
13
14
|
# File 'lib/primalize/single.rb', line 12
def attributes **attrs
_attributes **attrs
end
|
.boolean(&coerce) ⇒ Object
48
49
50
|
# File 'lib/primalize/single.rb', line 48
def boolean &coerce
enum(true, false, &coerce)
end
|
.enum(*values, &coerce) ⇒ Object
72
73
74
|
# File 'lib/primalize/single.rb', line 72
def enum *values, &coerce
Enum.new(values, &coerce)
end
|
.float(&coerce) ⇒ Object
60
61
62
|
# File 'lib/primalize/single.rb', line 60
def float &coerce
Float.new(&coerce)
end
|
108
109
110
|
# File 'lib/primalize/single.rb', line 108
def inspect
"#{name}(#{attributes.map { |attr, type| "#{attr}: #{type.inspect}" }.join(', ') })"
end
|
.integer(&coerce) ⇒ Object
40
41
42
|
# File 'lib/primalize/single.rb', line 40
def integer &coerce
Integer.new(&coerce)
end
|
.match(matcher, &coercion) ⇒ Object
88
89
90
|
# File 'lib/primalize/single.rb', line 88
def match matcher, &coercion
Match.new(matcher, &coercion)
end
|
.number(&coerce) ⇒ Object
64
65
66
|
# File 'lib/primalize/single.rb', line 64
def number &coerce
Number.new(&coerce)
end
|
.object(**types, &coerce) ⇒ Object
56
57
58
|
# File 'lib/primalize/single.rb', line 56
def object **types, &coerce
Object.new(types, &coerce)
end
|
.optional(*types, &coerce) ⇒ Object
68
69
70
|
# File 'lib/primalize/single.rb', line 68
def optional *types, &coerce
Optional.new(types, &coerce)
end
|
.primalize(primalizer, &coerce) ⇒ Object
92
93
94
|
# File 'lib/primalize/single.rb', line 92
def primalize primalizer, &coerce
Primalizer.new(primalizer, &coerce)
end
|
.string(&coerce) ⇒ Object
44
45
46
|
# File 'lib/primalize/single.rb', line 44
def string &coerce
String.new(&coerce)
end
|
.timestamp(&coerce) ⇒ Object
76
77
78
|
# File 'lib/primalize/single.rb', line 76
def timestamp &coerce
Timestamp.new(&coerce)
end
|
.type_mismatch_handler ⇒ Object
100
101
102
103
104
105
106
|
# File 'lib/primalize/single.rb', line 100
def type_mismatch_handler
if @type_mismatch_handler
@type_mismatch_handler
else
superclass.type_mismatch_handler
end
end
|
.type_mismatch_handler=(handler) ⇒ Object
96
97
98
|
# File 'lib/primalize/single.rb', line 96
def type_mismatch_handler= handler
@type_mismatch_handler = handler
end
|
Instance Method Details
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/primalize/single.rb', line 120
def call
self.class._attributes.each_with_object({}) do |(attr, type), hash|
value = public_send(attr)
hash[attr] = if type === value
value
else
self.class.type_mismatch_handler.call(
self.class,
attr,
type,
value,
)
end
end
end
|
143
144
145
|
# File 'lib/primalize/single.rb', line 143
def
self.class._attributes.keys.map(&:to_s)
end
|
147
148
149
150
|
# File 'lib/primalize/single.rb', line 147
def to_csv
hash = call
CSV.generate { |csv| csv << hash.values }
end
|
#to_json(options = nil) ⇒ Object
139
140
141
|
# File 'lib/primalize/single.rb', line 139
def to_json(options=nil)
call.to_json(options)
end
|