emery 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e7526be75b8c44c624947657c22214dd7715170e1bbdb8b46a8369a6ca66d4fc
4
- data.tar.gz: 5483955dd2c563ca2c6c04662aea8bc1f9b4a5e34817b9d60a046afa32ca5a04
3
+ metadata.gz: 629dad4de96ce6b80b94e883ac9dee5dff07c677d431727e83a7c379c229524b
4
+ data.tar.gz: 3dc8b2cc93ff053470be638ca64659f02de7c7f07894427212c603b3f36a8ad0
5
5
  SHA512:
6
- metadata.gz: e1e408199c42d0c877c1ca9be978f698357449ac9f4c909c6157ec190bfb83f19434184e68bf7ab6cf96b93007e6658f1eae1f744c57a97d8fae0231eed03a76
7
- data.tar.gz: 80f702c3cd1bd7edd8359a03576519db0002efcedd3db144ef1c3e94192ccd6fb129dd0ac8a25177f9d9dd96f016f3cf84c4237e69fe5d2e15c12f2790530456
6
+ metadata.gz: fed3812431f78ab7bb66066c19c788babe478675a1108ef038c237a2e47734dce7e572b53d3d781430da4d72eb4e4c29ab36f0cf80721395d3d971acaa6be83d
7
+ data.tar.gz: 9dfc4c611a1da41452861b565c7a4042a1ea8f685ebc79d87a6186fabc0cf96ba1e005a5c6e4617a50d22619799eb46e49c42238d348f6687d8b80cacd47f661
@@ -1,86 +1,84 @@
1
- module Emery
2
- module DataClass
3
- def initialize(params)
4
- self.class.json_attributes.each do |attr, attr_type|
5
- attr_value = params[attr]
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
- def ==(other)
11
- begin
12
- T.check(self.class, other)
13
- self.class.json_attributes.keys.each do |attr|
14
- if self.instance_variable_get("@#{attr}") != other.instance_variable_get("@#{attr}")
15
- return false
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
- def copy(params)
25
- params.each do |attr, attr_value|
26
- if !self.class.json_attributes.key?(attr)
27
- raise TypeError.new("Non existing attribute #{attr}")
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
- def self.included(base)
44
- base.extend ClassMethods
45
- end
42
+ def self.included(base)
43
+ base.extend ClassMethods
44
+ end
46
45
 
47
- module ClassMethods
48
- def json_attributes
49
- @json_attributes
50
- end
46
+ module ClassMethods
47
+ def json_attributes
48
+ @json_attributes
49
+ end
51
50
 
52
- def val(name, type)
53
- if @json_attributes == nil
54
- @json_attributes = {}
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
- def var(name, type)
61
- if @json_attributes == nil
62
- @json_attributes = {}
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
- def jsoner_deserialize(json_value)
69
- T.check(T.hash(String, NilableUntyped), json_value)
70
- parameters = @json_attributes.map do |attr, attr_type|
71
- attr_value = json_value[attr.to_s]
72
- [attr, Jsoner.deserialize(attr_type, attr_value)]
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
- def jsoner_serialize(value)
78
- T.check(self, value)
79
- attrs = @json_attributes.map do |attr, attr_type|
80
- [attr, Jsoner.serialize(attr_type, value.send(attr))]
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 Emery
2
- module Enum
3
- attr_reader :key, :value
1
+ module Enum
2
+ attr_reader :key, :value
4
3
 
5
- def initialize(key, value)
6
- @key = key
7
- @value = value
8
- end
4
+ def initialize(key, value)
5
+ @key = key
6
+ @value = value
7
+ end
9
8
 
10
- def self.included(base)
11
- base.extend Enumerable
12
- base.extend ClassMethods
9
+ def self.included(base)
10
+ base.extend Enumerable
11
+ base.extend ClassMethods
13
12
 
14
- base.private_class_method(:new)
15
- end
13
+ base.private_class_method(:new)
14
+ end
16
15
 
17
- module ClassMethods
18
- def check(value)
19
- T.check_not_nil(self, value)
20
- if !value?(value)
21
- raise TypeError.new("Value '#{value.inspect.to_s}' is not a member of enum #{self}")
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
- def jsoner_deserialize(json_value)
26
- T.check(self, json_value)
27
- end
24
+ def jsoner_deserialize(json_value)
25
+ T.check(self, json_value)
26
+ end
28
27
 
29
- def jsoner_serialize(value)
30
- T.check(self, value)
31
- end
28
+ def jsoner_serialize(value)
29
+ T.check(self, value)
30
+ end
32
31
 
33
- def define(key, value)
34
- @_enum_hash ||= {}
35
- @_enums_by_value ||= {}
32
+ def define(key, value)
33
+ @_enum_hash ||= {}
34
+ @_enums_by_value ||= {}
36
35
 
37
- if @_enum_hash.key?(key) then
38
- raise TypeError.new("Duplicate key: #{key}")
39
- end
36
+ if @_enum_hash.key?(key) then
37
+ raise TypeError.new("Duplicate key: #{key}")
38
+ end
40
39
 
41
- if @_enums_by_value.key?(value) then
42
- raise TypeError.new("Duplicate value: #{value}")
43
- end
40
+ if @_enums_by_value.key?(value) then
41
+ raise TypeError.new("Duplicate value: #{value}")
42
+ end
44
43
 
45
- new_instance = new(key, value)
46
- @_enum_hash[key] = new_instance
47
- @_enums_by_value[value] = new_instance
44
+ new_instance = new(key, value)
45
+ @_enum_hash[key] = new_instance
46
+ @_enums_by_value[value] = new_instance
48
47
 
49
- if key.to_s == key.to_s.upcase
50
- const_set key, value
51
- else
52
- define_singleton_method(key) { value }
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
- def each(&block)
57
- @_enum_hash.each(&block)
58
- end
55
+ def each(&block)
56
+ @_enum_hash.each(&block)
57
+ end
59
58
 
60
- def parse(k)
61
- k = k.to_s.upcase
62
- each do |key, enum|
63
- return enum.value if key.to_s.upcase == k
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
- def key?(k)
69
- @_enum_hash.key?(k)
70
- end
67
+ def key?(k)
68
+ @_enum_hash.key?(k)
69
+ end
71
70
 
72
- def value(k)
73
- enum = @_enum_hash[k]
74
- enum.value if enum
75
- end
71
+ def value(k)
72
+ enum = @_enum_hash[k]
73
+ enum.value if enum
74
+ end
76
75
 
77
- def value?(v)
78
- @_enums_by_value.key?(v)
79
- end
76
+ def value?(v)
77
+ @_enums_by_value.key?(v)
78
+ end
80
79
 
81
- def key(v)
82
- enum = @_enums_by_value[v]
83
- enum.key if enum
84
- end
80
+ def key(v)
81
+ enum = @_enums_by_value[v]
82
+ enum.key if enum
83
+ end
85
84
 
86
- def keys
87
- @_enum_hash.values.map(&:key)
88
- end
85
+ def keys
86
+ @_enum_hash.values.map(&:key)
87
+ end
89
88
 
90
- def values
91
- @_enum_hash.values.map(&:value)
92
- end
89
+ def values
90
+ @_enum_hash.values.map(&:value)
91
+ end
93
92
 
94
- def to_h
95
- Hash[@_enum_hash.map do |key, enum|
96
- [key, enum.value]
97
- end]
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