Class: Rx
- Inherits:
-
Object
show all
- Defined in:
- lib/rx/ruby/Rx.rb
Defined Under Namespace
Classes: Exception, Helper, Type, ValidationError
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(opt = {}) ⇒ Rx
Returns a new instance of Rx.
7
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/rx/ruby/Rx.rb', line 7
def initialize(opt={})
@type_registry = {}
@prefix = {
'' => 'tag:codesimply.com,2008:rx/core/',
'.meta' => 'tag:codesimply.com,2008:rx/meta/',
}
if opt[:load_core] then
Type::Core.core_types.each { |t| register_type(t) }
end
end
|
Class Method Details
.schema(schema) ⇒ Object
3
4
5
|
# File 'lib/rx/ruby/Rx.rb', line 3
def self.schema(schema)
Rx.new(:load_core => true).make_schema(schema)
end
|
Instance Method Details
#add_prefix(name, base) ⇒ Object
60
61
62
63
64
65
66
|
# File 'lib/rx/ruby/Rx.rb', line 60
def add_prefix(name, base)
if @prefix.has_key?(name) then
throw Rx::Exception.new("the prefix '#{name}' is already registered")
end
@prefix[name] = base
end
|
#expand_uri(name) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/rx/ruby/Rx.rb', line 45
def expand_uri(name)
if name.match(/\A\w+:/) then; return name; end;
match = name.match(/\A\/(.*?)\/(.+)\z/)
if ! match then
raise Rx::Exception.new("couldn't understand Rx type name: #{name}")
end
if ! @prefix.has_key?(match[1]) then
raise Rx::Exception.new("unknown prefix '#{match[1]}' in name 'name'")
end
return @prefix[ match[1] ] + match[2]
end
|
#learn_type(uri, schema) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/rx/ruby/Rx.rb', line 31
def learn_type(uri, schema)
if @type_registry.has_key?(uri) then
raise Rx::Exception.new(
"attempted to learn type for already-registered uri #{uri}"
)
end
make_schema(schema)
@type_registry[ uri ] = { 'schema' => schema }
end
|
#make_schema(schema) ⇒ Object
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rx/ruby/Rx.rb', line 68
def make_schema(schema)
schema = { 'type' => schema } if schema.instance_of?(String)
if not (schema.instance_of?(Hash) and schema['type']) then
raise Rx::Exception.new('invalid type')
end
uri = expand_uri(schema['type'])
if ! @type_registry.has_key?(uri) then
raise Rx::Exception.new('unknown type')
end
type_class = @type_registry[uri]
if type_class.instance_of?(Hash) then
if schema.keys != [ 'type' ] then
raise Rx::Exception.new('composed type does not take check arguments')
end
return make_schema(type_class['schema'])
else
return type_class.new(schema, self)
end
end
|
#register_type(type) ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/rx/ruby/Rx.rb', line 19
def register_type(type)
uri = type.uri
if @type_registry.has_key?(uri) then
raise Rx::Exception.new(
"attempted to register already-known type #{uri}"
)
end
@type_registry[ uri ] = type
end
|