canoser 0.1.1 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 89fbb5634526ecc83a890a3f2598d834938028b509c0a0500eef8f17707cb178
4
- data.tar.gz: 99b14d822421d7bf26b5eddff461c68efeddceb0c36511a1c34d594bc1699fd7
3
+ metadata.gz: dd5d2302b41f369cd2aa3f7bbb4602735fd2be0e3523347ed438da306e224cac
4
+ data.tar.gz: 5dc3bc174725cc60ec2a03841841a583701ead4358547750b44a865e1d2570ef
5
5
  SHA512:
6
- metadata.gz: 70f34f92da5ba6330baabbd7d37e2ce1070f2d8216fb47a6ca04314d49920c9aaed91b4906a872eddbbed64ceac983dd94be7e2468874e1b97f93d9b5b7d0590
7
- data.tar.gz: 2b105656dea17c9a1692da8cae2d82349d1e6b9780da33cae8e99f46c8a9221caca635bce96ba1473f8279f3253ef89ad35b70eb392068de1b36d6964f7b5402
6
+ metadata.gz: 1ac39e842339805ae312a43731673aa22e1540466966ca1d47b85382df34c6019a4d7db291bcd3fa92cdc23ff3601fe2829ae029b7bd26e862c646db92961c72
7
+ data.tar.gz: 26dae443e65e75f56fa582dbb5ea917cd16f46ed1ec7a5bba339d77db237e1546f973946abd60ed0a207899ea369a1a528f5581374c108d3d8034aa9fc360ee4
@@ -145,10 +145,10 @@ bytes = obj.serialize
145
145
  obj = AccountResource.deserialize(bytes)
146
146
  ```
147
147
  ### 从Struct对象中读取字段的值
148
- 对于所有通过define_field方法定义的字段,可以通过[field_name]获取该字段的值。比如:
148
+ 对于所有通过define_field方法定义的字段,可以通过field_name获取该字段的值。比如:
149
149
 
150
150
  ```ruby
151
- obj[:authentication_key]
151
+ obj.authentication_key
152
152
  ```
153
153
 
154
154
 
data/README.md CHANGED
@@ -150,17 +150,15 @@ After defining Canoser::Struct, you don't need to implement serialization and de
150
150
  obj = AccountResource.new(authentication_key:[...],...)
151
151
  bytes = obj.serialize
152
152
 
153
- # deserialize an object form bytes
153
+ # deserialize an object from bytes
154
154
  obj = AccountResource.deserialize(bytes)
155
155
  ```
156
156
 
157
- ### Get field value form object
158
- For all fields defined by the "define_field" method, the value of this field of an object can be obtained via [field_name]. such as:
157
+ ### Get field value from object
158
+ For all fields defined by the "define_field" method, the value of this field of an object can be obtained via field_name. such as:
159
159
 
160
160
  ```ruby
161
- obj[:authentication_key]
162
- #or
163
- obj["authentication_key"]
161
+ obj.authentication_key
164
162
  ```
165
163
 
166
164
 
@@ -16,12 +16,12 @@ module Canoser
16
16
  @@names << #{name}
17
17
  @@types ||= []
18
18
  @@types << type
19
+ attr_accessor(#{name})
19
20
  }
20
21
  class_eval str
21
22
  end
22
23
 
23
24
  def initialize(hash={})
24
- @values = {}
25
25
  hash.each do |k,v|
26
26
  idx = self.class.class_variable_get("@@names").find_index{|x| x==k}
27
27
  raise "#{k} is not a field of #{self}" unless idx
@@ -30,22 +30,14 @@ module Canoser
30
30
  len = type.fixed_len
31
31
  raise "fix-length array #{k}: #{len} != #{v.size}" if len && v.size != len
32
32
  end
33
- @values[k] = v
33
+ instance_variable_set("@#{k}", v)
34
34
  end
35
35
  end
36
36
 
37
- def [](name)
38
- @values[name.to_sym]
39
- end
40
-
41
- def []=(name, value)
42
- @values[name.to_sym] = value
43
- end
44
-
45
37
  def ==(other)
46
38
  return true if self.equal?(other)
47
39
  self.class.class_variable_get("@@names").each_with_index do |name, idx|
48
- unless @values[name] == other[name]
40
+ unless instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
49
41
  return false
50
42
  end
51
43
  end
@@ -60,7 +52,7 @@ module Canoser
60
52
  output = ""
61
53
  self.class.class_variable_get("@@names").each_with_index do |name, idx|
62
54
  type = self.class.class_variable_get("@@types")[idx]
63
- value = @values[name]
55
+ value = instance_variable_get("@#{name}")
64
56
  output << type.encode(value)
65
57
  end
66
58
  output
@@ -80,7 +72,7 @@ module Canoser
80
72
  def decode(cursor)
81
73
  self.class.class_variable_get("@@names").each_with_index do |name, idx|
82
74
  type = self.class.class_variable_get("@@types")[idx]
83
- @values[name] = type.decode(cursor)
75
+ instance_variable_set("@#{name}", type.decode(cursor))
84
76
  end
85
77
  self
86
78
  end
@@ -1,3 +1,3 @@
1
1
  module Canoser
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: canoser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - yuan xinyu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-30 00:00:00.000000000 Z
11
+ date: 2019-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler