-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.rb
executable file
·82 lines (66 loc) · 1.24 KB
/
test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
require 'json'
$LOAD_PATH.unshift 'lib'
require 'represent'
module AliceNumber
def self.new input
Represent::Value.new self, input
end
def self.validate input
fail "#{input.inspect} must be Fixnum" unless input.class == Fixnum
end
def self.normalize input
input
end
def self.to_serializable input
format '%x', input
end
end
module Alice
def self.new input
Represent::Collection.new AliceNumber, input
end
end
module TedString
def self.new input
Represent::Value.new self, input
end
def self.validate input
fail "#{input.inspect} must be String" unless input.class == String
end
def self.normalize input
input
end
def self.to_serializable input
input
end
end
module Ted
def self.new input
Represent::Entity.new(
{
name: TedString
},
input
)
end
end
module Bob
def self.new input
Represent::Entity.new(
{
alice: Alice, # collection
ted: Ted, # entity
number: AliceNumber # value
},
input
)
end
end
bob =
Bob.new alice: [ 1, 2, 3 ],
ted: {
name: 'Ted'
},
number: 171
puts JSON.generate(bob.to_serializable)