-
Notifications
You must be signed in to change notification settings - Fork 1
/
readme.rb
executable file
·138 lines (108 loc) · 2.61 KB
/
readme.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env ruby
# frozen_string_literal: true
# a small script following the code samples in the README
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'lib'))
require 'jsi'
puts "creating a schema and assigning its schema module to a constant"
puts
contact_schema = JSI.new_schema({
"$schema" => "http://json-schema.org/draft-07/schema",
"description" => "A Contact",
"type" => "object",
"properties" => {
"name" => {
"type" => "string",
},
"phone" => {
"type" => "array",
"items" => {
"type" => "object",
"properties" => {
"location" => {
"type" => "string",
},
"number" => {
"type" => "string",
}
}
}
}
}
})
print 'contact_schema: '
pp contact_schema
print 'contact_schema.jsi_schema_module: '
pp contact_schema.jsi_schema_module
puts "constant assignment: Contact = contact_schema.jsi_schema_module"
Contact = contact_schema.jsi_schema_module
print 'contact_schema.jsi_schema_module: '
pp contact_schema.jsi_schema_module
puts
puts "creating and using an instance described by the schema"
puts
bill = Contact.new_jsi({
"name" => "bill",
"phone" => [
{
"location" => "home",
"number" => "555",
}
],
"nickname" => "big b",
}, mutable: true)
print 'bill: '
pp bill
print 'bill.name: '
pp bill.name
print 'bill.phone.map(&:location): '
pp bill.phone.map(&:location)
print 'bill.jsi_valid?: '
pp bill.jsi_valid?
bad = Contact.new_jsi({'phone' => [{'number' => [5, 5, 5]}]})
print 'bad: '
pp bad
print 'bad.phone.jsi_validate: '
pp bad.phone.jsi_validate
print 'bill.transform_values(&:size): '
pp bill.transform_values(&:size) if Hash.method_defined?(:transform_values)
print "bill['nickname']: "
pp bill['nickname']
puts
puts "OOP: application-defined methods on schema modules apply to their instances"
puts
module Contact
def phone_numbers
phone.map(&:number)
end
def name
super + ' esq.'
end
def name=(name)
super(name.chomp(' esq.'))
end
end
print 'bill.name: '
pp bill.name
puts "bill.name = 'rob esq.'"
bill.name = 'rob esq.'
print "bill['name']: "
pp bill['name']
print 'bill.phone_numbers: '
pp bill.phone_numbers
puts
puts "OOP on subschemas"
puts
print "Contact.properties['phone'].items: "
pp Contact.properties['phone'].items
module Contact
Phone = properties['phone'].items
module Phone
def number_with_dashes
number.split(//).join('-')
end
end
end
print "Contact.properties['phone'].items: "
pp Contact.properties['phone'].items
print 'bill.phone.first.number_with_dashes: '
pp bill.phone.first.number_with_dashes