emery 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/emery/dataclass.rb +63 -65
- data/lib/emery/enum.rb +73 -75
- data/lib/emery/jsoner.rb +162 -164
- data/lib/emery/tod.rb +202 -204
- data/lib/emery/type.rb +149 -151
- data/lib/emery.rb +1 -2
- data/test/dataclass_test.rb +80 -83
- data/test/enum_test.rb +29 -33
- data/test/jsoner_test.rb +189 -192
- data/test/tod_test.rb +23 -28
- data/test/type_test.rb +153 -155
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 629dad4de96ce6b80b94e883ac9dee5dff07c677d431727e83a7c379c229524b
|
4
|
+
data.tar.gz: 3dc8b2cc93ff053470be638ca64659f02de7c7f07894427212c603b3f36a8ad0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fed3812431f78ab7bb66066c19c788babe478675a1108ef038c237a2e47734dce7e572b53d3d781430da4d72eb4e4c29ab36f0cf80721395d3d971acaa6be83d
|
7
|
+
data.tar.gz: 9dfc4c611a1da41452861b565c7a4042a1ea8f685ebc79d87a6186fabc0cf96ba1e005a5c6e4617a50d22619799eb46e49c42238d348f6687d8b80cacd47f661
|
data/lib/emery/dataclass.rb
CHANGED
@@ -1,86 +1,84 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
self.instance_variable_set("@#{attr}", T.check_var(attr, attr_type, attr_value))
|
7
|
-
end
|
1
|
+
module DataClass
|
2
|
+
def initialize(params)
|
3
|
+
self.class.json_attributes.each do |attr, attr_type|
|
4
|
+
attr_value = params[attr]
|
5
|
+
self.instance_variable_set("@#{attr}", T.check_var(attr, attr_type, attr_value))
|
8
6
|
end
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
9
|
+
def ==(other)
|
10
|
+
begin
|
11
|
+
T.check(self.class, other)
|
12
|
+
self.class.json_attributes.keys.each do |attr|
|
13
|
+
if self.instance_variable_get("@#{attr}") != other.instance_variable_get("@#{attr}")
|
14
|
+
return false
|
17
15
|
end
|
18
|
-
return true
|
19
|
-
rescue
|
20
|
-
return false
|
21
16
|
end
|
17
|
+
return true
|
18
|
+
rescue
|
19
|
+
return false
|
22
20
|
end
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
end
|
23
|
+
def copy(params)
|
24
|
+
params.each do |attr, attr_value|
|
25
|
+
if !self.class.json_attributes.key?(attr)
|
26
|
+
raise TypeError.new("Non existing attribute #{attr}")
|
29
27
|
end
|
30
|
-
new_params =
|
31
|
-
self.class.json_attributes.map do |attr, attr_type|
|
32
|
-
attr_value =
|
33
|
-
if params.key?(attr)
|
34
|
-
params[attr]
|
35
|
-
else
|
36
|
-
self.instance_variable_get("@#{attr}")
|
37
|
-
end
|
38
|
-
[attr, attr_value]
|
39
|
-
end.to_h
|
40
|
-
return self.class.new(new_params)
|
41
28
|
end
|
29
|
+
new_params =
|
30
|
+
self.class.json_attributes.map do |attr, attr_type|
|
31
|
+
attr_value =
|
32
|
+
if params.key?(attr)
|
33
|
+
params[attr]
|
34
|
+
else
|
35
|
+
self.instance_variable_get("@#{attr}")
|
36
|
+
end
|
37
|
+
[attr, attr_value]
|
38
|
+
end.to_h
|
39
|
+
return self.class.new(new_params)
|
40
|
+
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
def self.included(base)
|
43
|
+
base.extend ClassMethods
|
44
|
+
end
|
46
45
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
module ClassMethods
|
47
|
+
def json_attributes
|
48
|
+
@json_attributes
|
49
|
+
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
end
|
56
|
-
@json_attributes[name] = type
|
57
|
-
attr_reader name
|
51
|
+
def val(name, type)
|
52
|
+
if @json_attributes == nil
|
53
|
+
@json_attributes = {}
|
58
54
|
end
|
55
|
+
@json_attributes[name] = type
|
56
|
+
attr_reader name
|
57
|
+
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
end
|
64
|
-
@json_attributes[name] = type
|
65
|
-
attr_accessor name
|
59
|
+
def var(name, type)
|
60
|
+
if @json_attributes == nil
|
61
|
+
@json_attributes = {}
|
66
62
|
end
|
63
|
+
@json_attributes[name] = type
|
64
|
+
attr_accessor name
|
65
|
+
end
|
67
66
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
end
|
74
|
-
return self.new parameters.to_h
|
67
|
+
def jsoner_deserialize(json_value)
|
68
|
+
T.check(T.hash(String, NilableUntyped), json_value)
|
69
|
+
parameters = @json_attributes.map do |attr, attr_type|
|
70
|
+
attr_value = json_value[attr.to_s]
|
71
|
+
[attr, Jsoner.deserialize(attr_type, attr_value)]
|
75
72
|
end
|
73
|
+
return self.new parameters.to_h
|
74
|
+
end
|
76
75
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
end
|
82
|
-
return attrs.to_h
|
76
|
+
def jsoner_serialize(value)
|
77
|
+
T.check(self, value)
|
78
|
+
attrs = @json_attributes.map do |attr, attr_type|
|
79
|
+
[attr, Jsoner.serialize(attr_type, value.send(attr))]
|
83
80
|
end
|
81
|
+
return attrs.to_h
|
84
82
|
end
|
85
83
|
end
|
86
84
|
end
|
data/lib/emery/enum.rb
CHANGED
@@ -1,101 +1,99 @@
|
|
1
|
-
module
|
2
|
-
|
3
|
-
attr_reader :key, :value
|
1
|
+
module Enum
|
2
|
+
attr_reader :key, :value
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
def initialize(key, value)
|
5
|
+
@key = key
|
6
|
+
@value = value
|
7
|
+
end
|
9
8
|
|
10
|
-
|
11
|
-
|
12
|
-
|
9
|
+
def self.included(base)
|
10
|
+
base.extend Enumerable
|
11
|
+
base.extend ClassMethods
|
13
12
|
|
14
|
-
|
15
|
-
|
13
|
+
base.private_class_method(:new)
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
end
|
16
|
+
module ClassMethods
|
17
|
+
def check(value)
|
18
|
+
T.check_not_nil(self, value)
|
19
|
+
if !value?(value)
|
20
|
+
raise TypeError.new("Value '#{value.inspect.to_s}' is not a member of enum #{self}")
|
23
21
|
end
|
22
|
+
end
|
24
23
|
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
def jsoner_deserialize(json_value)
|
25
|
+
T.check(self, json_value)
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
def jsoner_serialize(value)
|
29
|
+
T.check(self, value)
|
30
|
+
end
|
32
31
|
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
def define(key, value)
|
33
|
+
@_enum_hash ||= {}
|
34
|
+
@_enums_by_value ||= {}
|
36
35
|
|
37
|
-
|
38
|
-
|
39
|
-
|
36
|
+
if @_enum_hash.key?(key) then
|
37
|
+
raise TypeError.new("Duplicate key: #{key}")
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
if @_enums_by_value.key?(value) then
|
41
|
+
raise TypeError.new("Duplicate value: #{value}")
|
42
|
+
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
new_instance = new(key, value)
|
45
|
+
@_enum_hash[key] = new_instance
|
46
|
+
@_enums_by_value[value] = new_instance
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
end
|
48
|
+
if key.to_s == key.to_s.upcase
|
49
|
+
const_set key, value
|
50
|
+
else
|
51
|
+
define_singleton_method(key) { value }
|
54
52
|
end
|
53
|
+
end
|
55
54
|
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
def each(&block)
|
56
|
+
@_enum_hash.each(&block)
|
57
|
+
end
|
59
58
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
end
|
65
|
-
nil
|
59
|
+
def parse(k)
|
60
|
+
k = k.to_s.upcase
|
61
|
+
each do |key, enum|
|
62
|
+
return enum.value if key.to_s.upcase == k
|
66
63
|
end
|
64
|
+
nil
|
65
|
+
end
|
67
66
|
|
68
|
-
|
69
|
-
|
70
|
-
|
67
|
+
def key?(k)
|
68
|
+
@_enum_hash.key?(k)
|
69
|
+
end
|
71
70
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
71
|
+
def value(k)
|
72
|
+
enum = @_enum_hash[k]
|
73
|
+
enum.value if enum
|
74
|
+
end
|
76
75
|
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
def value?(v)
|
77
|
+
@_enums_by_value.key?(v)
|
78
|
+
end
|
80
79
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
80
|
+
def key(v)
|
81
|
+
enum = @_enums_by_value[v]
|
82
|
+
enum.key if enum
|
83
|
+
end
|
85
84
|
|
86
|
-
|
87
|
-
|
88
|
-
|
85
|
+
def keys
|
86
|
+
@_enum_hash.values.map(&:key)
|
87
|
+
end
|
89
88
|
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
def values
|
90
|
+
@_enum_hash.values.map(&:value)
|
91
|
+
end
|
93
92
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
93
|
+
def to_h
|
94
|
+
Hash[@_enum_hash.map do |key, enum|
|
95
|
+
[key, enum.value]
|
96
|
+
end]
|
99
97
|
end
|
100
98
|
end
|
101
99
|
end
|