xdr 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
  SHA1:
3
- metadata.gz: 96b9bbb13d9fa767c8f769b365c01e053e81e136
4
- data.tar.gz: e9ec8112aa3942de27e64d7492b7e5b899ca831b
3
+ metadata.gz: 338a81fa5cb0c2cc1f5db4449c4b7794a6e96083
4
+ data.tar.gz: dee4b7e4edc80cebe761a8ea2588936abb559d56
5
5
  SHA512:
6
- metadata.gz: 923b026461a762e8820337eb785580e18619148a2fa33ff0768416e4ce26f18f671fe4d7edc6b2f10ccc6eceb1b54aaedee9ed605f253467f97b0a46f7547adb
7
- data.tar.gz: 03d37401a170246dd8ad37ae4ff288191d040e2a01cbd86e0bce95bbefca662ddf9592f5a1ac9830c61c0cf673f2f3cfa5e14346c97dfa5432a47118e886aba6
6
+ metadata.gz: c9ffd4fcccdcb2cd41a4992cad1900add89645e54c164d4a5879374625e0c3e77701bc8cf94cfb8bef6e1d12a1ec155d7dce4708b4e8df074b0cc3554062ad50
7
+ data.tar.gz: c82033a24062fcbfd46ac04b9fc7dd0a650bcd5e734bcc42f04c1ac2f2af6c54270687b6f33bdc7ad86f50f038ce56ccc84f105b63cf1d198b600cbe260cdb4e
@@ -2,12 +2,19 @@ module XDR::DSL::Struct
2
2
  def attribute(name, type)
3
3
 
4
4
  unless type.is_a?(XDR::Concerns::ConvertsToXDR)
5
- raise ArgumentError, "#{type} does not convert to xdr"
5
+ raise ArgumentError, "#{type} does not convert to xdr"
6
6
  end
7
7
 
8
8
  self.fields = self.fields.merge(name => type)
9
9
 
10
- attr_accessor name
10
+ define_method name do
11
+ read_attribute(name)
12
+ end
13
+
14
+ define_method "#{name}=" do |v|
15
+ write_attribute(name, v)
16
+ end
17
+
11
18
  define_attribute_methods name
12
19
  end
13
- end
20
+ end
data/lib/xdr/struct.rb CHANGED
@@ -7,11 +7,16 @@ class XDR::Struct
7
7
  extend XDR::Concerns::ConvertsToXDR
8
8
  extend XDR::DSL::Struct
9
9
 
10
+ attribute_method_prefix 'read_'
11
+ attribute_method_suffix 'write_'
12
+
10
13
  class_attribute :fields
11
14
  self.fields = ActiveSupport::OrderedHash.new
12
15
 
13
16
  validates_with XDR::StructValidator
14
17
 
18
+ attr_reader :attributes
19
+
15
20
  def self.read(io)
16
21
  new.tap do |result|
17
22
  fields.each do |name, type|
@@ -26,16 +31,22 @@ class XDR::Struct
26
31
  type.write(field_val, io)
27
32
  end
28
33
  end
29
-
34
+
30
35
  def self.valid?(val)
31
36
  val.is_a?(self)
32
37
  end
33
38
 
34
- #
39
+ def initialize(attributes={})
40
+ @attributes = {}
41
+ super
42
+ end
43
+
44
+
45
+ #
35
46
  # Serializes struct to xdr, return a string of bytes
36
- #
47
+ #
37
48
  # @param format=:raw [Symbol] The encoding used for the bytes produces, one of (:raw, :hex, :base64)
38
- #
49
+ #
39
50
  # @return [String] The encoded bytes of this struct
40
51
  def to_xdr(format=:raw)
41
52
  raw = self.class.to_xdr(self)
@@ -44,8 +55,25 @@ class XDR::Struct
44
55
  when :raw ; raw
45
56
  when :hex ; raw.unpack("H*").first
46
57
  when :base64 ; Base64.strict_encode64(raw)
47
- else ;
58
+ else ;
48
59
  raise ArgumentError, "Invalid format #{format.inspect}; must be :raw, :hex, or :base64"
49
60
  end
50
61
  end
51
- end
62
+
63
+ #
64
+ # Compares two structs for equality
65
+ #
66
+ def == (other)
67
+ return false unless other.is_a?(self.class)
68
+ other.attributes == self.attributes
69
+ end
70
+
71
+
72
+ def read_attribute(attr)
73
+ @attributes[attr]
74
+ end
75
+
76
+ def write_attribute(attr, value)
77
+ @attributes[attr] = value
78
+ end
79
+ end
data/lib/xdr/union.rb CHANGED
@@ -87,6 +87,16 @@ class XDR::Union
87
87
  get
88
88
  end
89
89
 
90
+
91
+ #
92
+ # Compares two unions for equality
93
+ #
94
+ def == (other)
95
+ return false unless other.is_a?(self.class)
96
+ return false unless other.switch == self.switch
97
+ other.value == self.value
98
+ end
99
+
90
100
  private
91
101
  def valid_for_arm_type(value, arm)
92
102
  arm_type = arms[@arm]
@@ -96,6 +106,3 @@ class XDR::Union
96
106
  arm_type.valid?(value)
97
107
  end
98
108
  end
99
-
100
-
101
-
data/lib/xdr/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module XDR
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xdr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Fleckenstein
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-27 00:00:00.000000000 Z
11
+ date: 2015-06-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport