xdr 2.0.0 → 3.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +26 -7
  3. data/README.md +21 -19
  4. data/lib/xdr/array.rb +3 -3
  5. data/lib/xdr/bool.rb +7 -7
  6. data/lib/xdr/concerns/array_converter.rb +2 -2
  7. data/lib/xdr/concerns/converts_to_xdr.rb +40 -41
  8. data/lib/xdr/concerns/float_converter.rb +1 -1
  9. data/lib/xdr/concerns/integer_converter.rb +1 -1
  10. data/lib/xdr/concerns/reads_bytes.rb +10 -1
  11. data/lib/xdr/concerns/string_converter.rb +1 -1
  12. data/lib/xdr/double.rb +2 -3
  13. data/lib/xdr/dsl/enum.rb +8 -9
  14. data/lib/xdr/dsl/struct.rb +1 -2
  15. data/lib/xdr/dsl/union.rb +12 -12
  16. data/lib/xdr/dsl.rb +1 -1
  17. data/lib/xdr/enum.rb +6 -6
  18. data/lib/xdr/float.rb +2 -3
  19. data/lib/xdr/hyper.rb +2 -2
  20. data/lib/xdr/int.rb +2 -2
  21. data/lib/xdr/namespace.rb +7 -9
  22. data/lib/xdr/opaque.rb +5 -5
  23. data/lib/xdr/option.rb +5 -5
  24. data/lib/xdr/quadruple.rb +1 -1
  25. data/lib/xdr/rpc/record.rb +2 -2
  26. data/lib/xdr/rpc/record_reader.rb +7 -7
  27. data/lib/xdr/string.rb +4 -4
  28. data/lib/xdr/struct.rb +15 -16
  29. data/lib/xdr/struct_validator.rb +1 -1
  30. data/lib/xdr/union.rb +44 -36
  31. data/lib/xdr/union_validator.rb +1 -1
  32. data/lib/xdr/unsigned_hyper.rb +2 -2
  33. data/lib/xdr/unsigned_int.rb +2 -2
  34. data/lib/xdr/var_array.rb +4 -4
  35. data/lib/xdr/var_opaque.rb +3 -3
  36. data/lib/xdr/version.rb +1 -1
  37. data/lib/xdr/void.rb +1 -1
  38. data/lib/xdr.rb +16 -10
  39. metadata +37 -150
  40. data/.gitignore +0 -15
  41. data/.travis.yml +0 -9
  42. data/.yardopts +0 -7
  43. data/Gemfile +0 -4
  44. data/Guardfile +0 -5
  45. data/Rakefile +0 -9
  46. data/examples/enum.rb +0 -30
  47. data/examples/struct.rb +0 -24
  48. data/examples/union.rb +0 -29
  49. data/spec/lib/xdr/array_spec.rb +0 -73
  50. data/spec/lib/xdr/bool_spec.rb +0 -43
  51. data/spec/lib/xdr/concerns/converts_to_xdr_spec.rb +0 -88
  52. data/spec/lib/xdr/concerns/reads_bytes_spec.rb +0 -31
  53. data/spec/lib/xdr/double_spec.rb +0 -38
  54. data/spec/lib/xdr/dsl/enum_spec.rb +0 -44
  55. data/spec/lib/xdr/dsl/struct_spec.rb +0 -29
  56. data/spec/lib/xdr/dsl/union_spec.rb +0 -69
  57. data/spec/lib/xdr/enum_spec.rb +0 -70
  58. data/spec/lib/xdr/float_spec.rb +0 -37
  59. data/spec/lib/xdr/hyper_spec.rb +0 -40
  60. data/spec/lib/xdr/int_spec.rb +0 -40
  61. data/spec/lib/xdr/opaque_spec.rb +0 -36
  62. data/spec/lib/xdr/option_spec.rb +0 -36
  63. data/spec/lib/xdr/quadruple_spec.rb +0 -14
  64. data/spec/lib/xdr/rpc/record_reader_spec.rb +0 -27
  65. data/spec/lib/xdr/string_spec.rb +0 -41
  66. data/spec/lib/xdr/struct_spec.rb +0 -101
  67. data/spec/lib/xdr/union_spec.rb +0 -248
  68. data/spec/lib/xdr/unsigned_hyper_spec.rb +0 -36
  69. data/spec/lib/xdr/unsigned_int_spec.rb +0 -36
  70. data/spec/lib/xdr/var_array_spec.rb +0 -71
  71. data/spec/lib/xdr/var_opaque_spec.rb +0 -43
  72. data/spec/lib/xdr/void_spec.rb +0 -46
  73. data/spec/spec_helper.rb +0 -15
  74. data/spec/support/matchers/eq_bytes.rb +0 -6
  75. data/xdr.gemspec +0 -28
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 841aa4d6061ac4e1989f6eb231c3b78fdc69b43f
4
- data.tar.gz: d3342bce08c239465be256c53311d242d15fe658
2
+ SHA256:
3
+ metadata.gz: 5e73176f8adac880de7eb0ed3761730773c9dadac9f572e9a9fd1f75ff396488
4
+ data.tar.gz: ea7c2d4f8fc312f33d4fb81be7a9074a6ed2f6e5dec2cccb41f7c00cdabff248
5
5
  SHA512:
6
- metadata.gz: faaa298dac8ceed46a0f9d7c654e5e4daad1bf8fad2dfdd1cee350c84ec54c653b193653289ef4fecbc92922f72e79c32a41285a59b392f98c14e596861afd6a
7
- data.tar.gz: 1bc9c094fec32a7e39338122696fb58cb3058099f29929abf7b87da9972d3ddab2e1e58ca691ac3ab8956694ffb767eb354ec6f39b3c80b9245be3f74123757b
6
+ metadata.gz: 9f6825750389ef2f19882533f169c9de4c05a264fa19228fcdbed3f3276196473d6e6e2cac75c420926a097f2543a71100cc17ad0784864e1c18c24da66aa455
7
+ data.tar.gz: 3c04035a3532da2c732aa47664a9bf4cfe79616806f3193948d5c8c37aa2c2862153cb2a74fe13b0afcb30d25f2f1f39729f510ac67655891a9d466de767ba64
data/CHANGELOG.md CHANGED
@@ -1,17 +1,36 @@
1
- # Changelog
1
+ # Changelog
2
2
 
3
- All notable changes to this project will be documented in this
4
- file. This project adheres to [Semantic Versioning](http://semver.org/).
3
+ All notable changes to this project will be documented in this file.
5
4
 
6
- As this project is pre 1.0, breaking changes may happen for minor version
7
- bumps. A breaking change will get clearly notified in this log.
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
8
7
 
9
- ## [1.0.0](https://github.com/stellar/ruby-stellar-base/compare/v0.1.0...v1.0.0)
8
+ ### [3.0.3](https://github.com/astroband/ruby-xdr/compare/v3.0.2...v3.0.3) (2022-02-18)
9
+ ### Bug Fixes
10
+ - make `XDR::Option` write empty strings as values ([#15](https://github.com/astroband/ruby-xdr/issues/15)) ([ccab229](https://github.com/astroband/ruby-xdr/commit/ccab22928a1104dee525211b51d1d567079d27ba))
10
11
 
12
+
13
+ ## [3.0.2](https://github.com/astroband/ruby-xdr/compare/v3.0.1...v3.0.2) - 2020-10-20
14
+ ### Changed
15
+ - Relax lower bound for ActiveSupport dependency to support usage in Rails 4.2 ([#12](https://github.com/astroband/ruby-xdr/pull/12))
16
+
17
+ ### Bug Fixes
18
+ - Fix non-bang arm accessors for XDR::Union ([#13](https://github.com/astroband/ruby-xdr/pull/13))
19
+ - Fix .from_xdr encoding param to accept symbols ([#14](https://github.com/astroband/ruby-xdr/pull/14))
20
+
21
+
22
+ ## [3.0.1](https://github.com/astroband/ruby-xdr/compare/v3.0.0...v3.0.1) - 2020-06-10
23
+ ### Added
24
+ - Add encoding parameter to Union#to_xdr ([#7](https://github.com/astroband/ruby-xdr/pull/7)).
25
+
26
+ ### Bug Fixes
27
+ - Padding bytes are now properly validated when reading xdr values. According to the XDR spec, padding must be zeros.
28
+
29
+
30
+ ## [1.0.0](https://github.com/astroband/ruby-xdr/compare/v0.1.0...v1.0.0) - 2015-10-02
11
31
  ### Added
12
32
  - the `to_xdr` helpers can take a second parameter that will encode the resulting output to hex or base64 when requested.
13
33
  - the `from_xdr` helpers can take a second parameter that will trigger a decode from hex or base64 of the provided string before decoding from xdr.
14
34
 
15
35
  ### Changed
16
-
17
36
  - `from_xdr` raises an ArgumentError when the input data is not fully consumed
data/README.md CHANGED
@@ -3,7 +3,9 @@
3
3
  [![Build Status](https://travis-ci.org/stellar/ruby-xdr.svg?branch=master)](https://travis-ci.org/stellar/ruby-xdr)
4
4
  [![Code Climate](https://codeclimate.com/github/stellar/ruby-xdr/badges/gpa.svg)](https://codeclimate.com/github/stellar/ruby-xdr)
5
5
 
6
- XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/rfc4506.html). This library provides a way to read and write XDR data from ruby. It can read/write all of the primitive XDR types and also provides facilities to define readers for the compound XDR types (enums, structs and unions)
6
+ XDR is an open data format, specified in [RFC 4506](http://tools.ietf.org/html/rfc4506.html). This library provides a
7
+ way to read and write XDR data from ruby. It can read/write all of the primitive XDR types and also provides facilities
8
+ to define readers for the compound XDR types (enums, structs and unions)
7
9
 
8
10
  ## Installation
9
11
 
@@ -15,28 +17,28 @@ gem 'xdr'
15
17
 
16
18
  And then execute:
17
19
 
18
- $ bundle
20
+ bundle
19
21
 
20
22
  Or install it yourself as:
21
23
 
22
- $ gem install xdr
24
+ gem install xdr
23
25
 
24
26
  ## Usage
25
27
 
26
28
  ```ruby
27
29
  # Reading/writing a primitive values
28
- XDR::Bool.to_xdr(false) # => "\x00\x00\x00\x00"
30
+ XDR::Bool.to_xdr(false) # => "\x00\x00\x00\x00"
29
31
  XDR::Bool.from_xdr("\x00\x00\x00\x01") # => true
30
32
 
31
33
  # Reading/writing arrays
32
- XDR::Array[XDR::Int,2].to_xdr([1,2]) # => "\x00\x00\x00\x01\x00\x00\x00\x02"
33
- XDR::Array[XDR::Int,2].from_xdr("\x00\x00\x00\x03\x00\x00\x00\x04") # => [3,4]
34
+ XDR::Array[XDR::Int, 2].to_xdr([1, 2]) # => "\x00\x00\x00\x01\x00\x00\x00\x02"
35
+ XDR::Array[XDR::Int, 2].from_xdr("\x00\x00\x00\x03\x00\x00\x00\x04") # => [3,4]
34
36
 
35
37
  # Defining an enum
36
38
 
37
39
  class ResultType < XDR::Enum
38
- member :ok, 0
39
- member :error, 1
40
+ member :ok, 0
41
+ member :error, 1
40
42
  seal
41
43
  end
42
44
 
@@ -88,22 +90,22 @@ Result.from_xdr("\x00\x00\x00\x00") # => #<Result ...>
88
90
 
89
91
  ## Thread safety
90
92
 
91
- Code generated by `xdrgen`, which targets this library, uses autoload extensively.
92
- Since autoloading is not thread-safe, neither is code generated from xdrgen. To
93
- work around this, any module including `XDR::Namespace` can be forced to load
93
+ Code generated by `xdrgen`, which targets this library, uses autoload extensively. Since autoloading is not thread-safe,
94
+ neither is code generated from xdrgen. To work around this, any module including `XDR::Namespace` can be forced to load
94
95
  all of it's children by calling `load_all!` on the module.
95
96
 
96
97
  ## Code generation
97
98
 
98
- ruby-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom data types. Instead, that is the responsibility of [xdrgen](http://github.com/stellar/xdrgen). xdrgen will take your .x files and produce a set of ruby files that target this library to allow for your own custom types.
99
+ ruby-xdr by itself does not have any ability to parse XDR IDL files and produce a parser for your custom data types.
100
+ Instead, that is the responsibility of [xdrgen](http://github.com/stellar/xdrgen). `xdrgen` will take your `.x` files
101
+ and produce a set of ruby files that target this library to allow for your own custom types.
99
102
 
100
- See [ruby-stellar-base](http://github.com/stellar/ruby-stellar-base) for an example (check out the generated directory)
103
+ See [ruby-stellar-base](http://github.com/astroband/ruby-stellar-sdk/tree/master/base/generated) for an example.
101
104
 
102
105
  ## Contributing
103
106
 
104
- 1. Sign the [Contributor License Agreement](http://goo.gl/forms/6r7I4S0K4z)
105
- 2. Fork it ( https://github.com/stellar/ruby-xdr/fork )
106
- 2. Create your feature branch (`git checkout -b my-new-feature`)
107
- 3. Commit your changes (`git commit -am 'Add some feature'`)
108
- 4. Push to the branch (`git push origin my-new-feature`)
109
- 5. Create a new Pull Request
107
+ 1. Fork the repo ( https://github.com/astroband/ruby-xdr/fork )
108
+ 1. Create your feature branch (`git checkout -b my-new-feature`)
109
+ 1. Commit your changes (`git commit -am 'Add some feature'`)
110
+ 1. Push to the branch (`git push origin my-new-feature`)
111
+ 1. Create a new Pull Request
data/lib/xdr/array.rb CHANGED
@@ -6,7 +6,7 @@ class XDR::Array
6
6
 
7
7
  def initialize(child_type, length)
8
8
  @child_type = child_type
9
- @length = length
9
+ @length = length
10
10
  end
11
11
 
12
12
  def write(val, io)
@@ -19,10 +19,10 @@ class XDR::Array
19
19
  end
20
20
 
21
21
  def read(io)
22
- @length.times.map{ @child_type.read(io) }
22
+ @length.times.map { @child_type.read(io) }
23
23
  end
24
24
 
25
25
  def valid?(val)
26
26
  super(val) && val.length == @length
27
27
  end
28
- end
28
+ end
data/lib/xdr/bool.rb CHANGED
@@ -3,22 +3,22 @@ module XDR::Bool
3
3
 
4
4
  def self.write(val, io)
5
5
  case val
6
- when true ; XDR::Int.write(1, io)
7
- when false ; XDR::Int.write(0, io)
8
- else ; raise XDR::WriteError, "Invalid bool value: #{val}"
6
+ when true then XDR::Int.write(1, io)
7
+ when false then XDR::Int.write(0, io)
8
+ else; raise XDR::WriteError, "Invalid bool value: #{val}"
9
9
  end
10
10
  end
11
11
 
12
12
  def self.read(io)
13
13
  val = XDR::Int.read(io)
14
14
  case val
15
- when 0 ; false
16
- when 1 ; true
17
- else ; raise XDR::ReadError, "Invalid bool value: #{val}"
15
+ when 0 then false
16
+ when 1 then true
17
+ else; raise XDR::ReadError, "Invalid bool value: #{val}"
18
18
  end
19
19
  end
20
20
 
21
21
  def self.valid?(val)
22
22
  val == true || val == false
23
23
  end
24
- end
24
+ end
@@ -1,5 +1,5 @@
1
1
  module XDR::Concerns::ArrayConverter
2
2
  def valid?(val)
3
- val.is_a?(Array) && val.all?{|v| @child_type.valid?(v)}
3
+ val.is_a?(Array) && val.all? { |v| @child_type.valid?(v) }
4
4
  end
5
- end
5
+ end
@@ -1,91 +1,90 @@
1
- require 'base64'
1
+ require "base64"
2
2
 
3
3
  module XDR::Concerns::ConvertsToXDR
4
4
  include XDR::Concerns::ReadsBytes
5
5
 
6
- #
6
+ #
7
7
  # Serialized the provided `val` to xdr and writes it to `io`
8
- #
8
+ #
9
+ # @abstract
9
10
  # @param val [Object] The object to serialize
10
- # @param io [IO] an IO object to write to
11
- #
11
+ # @param io [#write] an IO object to write to
12
+ # @return [void]
12
13
  def write(val, io)
13
14
  raise NotImplementedError, "implement in including class"
14
15
  end
15
16
 
16
- #
17
+ #
17
18
  # Reads from the provided IO an instance of the implementing class
18
- # @param io [IO] the io to read from
19
- #
19
+ #
20
+ # @abstract
21
+ # @param io [#read] the io to read from
20
22
  # @return [Object] the deserialized value
21
23
  def read(io)
22
24
  raise NotImplementedError, "implement in including class"
23
25
  end
24
26
 
25
- #
26
27
  # Returns true if the value provided is compatible with this serializer class
27
- #
28
+ #
29
+ # @abstract
28
30
  # @param value [Object] the value to test
29
- #
30
31
  # @return [Boolean] true if valid, false otherwise
31
32
  def valid?(value)
32
33
  raise NotImplementedError, "implement in including class"
33
34
  end
34
-
35
- #
35
+
36
36
  # Serialized the provided val to xdr, returning a string
37
37
  # of the serialized data
38
- #
38
+ #
39
39
  # @param val [Object] the value to serialize
40
- #
40
+ # @param encoding [:raw|:hex|:base64] encode the result with specified codec
41
41
  # @return [String] the produced bytes
42
- def to_xdr(val, encoding='raw')
43
- raw = StringIO.
44
- new.
45
- tap{|io| write(val, io)}.
46
- string.force_encoding("ASCII-8BIT")
42
+ def to_xdr(val, encoding = "raw")
43
+ io = StringIO.new
44
+ write(val, io)
45
+ raw = io.string.force_encoding("ASCII-8BIT")
47
46
 
48
- case encoding
49
- when 'raw' ; raw
50
- when 'base64' ; Base64.strict_encode64(raw)
51
- when 'hex' ; raw.unpack("H*").first
47
+ case String(encoding)
48
+ when "raw" then raw
49
+ when "hex" then raw.unpack1("H*")
50
+ when "base64" then Base64.strict_encode64(raw)
52
51
  else
53
- raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
52
+ raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
54
53
  end
55
54
  end
56
-
57
- #
55
+
58
56
  # Deserializes an object from the provided string of bytes
59
- #
57
+ #
60
58
  # @param string [String] the bytes to read from
61
- #
59
+ # @param encoding [:raw|:hex|:base64] decode the input before deserialization
62
60
  # @return [Object] the deserialized value
63
- def from_xdr(string, encoding='raw')
64
- raw = case encoding
65
- when 'raw' ; string
66
- when 'base64' ; Base64.strict_decode64(string)
67
- when 'hex' ; [string].pack("H*")
61
+ def from_xdr(string, encoding = "raw")
62
+ raw = case String(encoding)
63
+ when "raw" then string
64
+ when "hex" then [string].pack("H*")
65
+ when "base64" then Base64.strict_decode64(string)
68
66
  else
69
- raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
70
- end
67
+ raise ArgumentError, "Invalid encoding #{encoding.inspect}: must be 'raw', 'base64', or 'hex'"
68
+ end
71
69
 
72
70
  io = StringIO.new(raw)
73
71
  result = read(io)
74
72
 
75
73
  if io.pos != io.length
76
- raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
74
+ raise ArgumentError, "Input string not fully consumed! are you decoding the right xdr type?"
77
75
  end
78
76
 
79
77
  result
80
78
  end
81
79
 
82
80
  private
81
+
83
82
  def padding_for(length)
84
83
  case length % 4
85
- when 0 ; 0
86
- when 1 ; 3
87
- when 2 ; 2
88
- when 3 ; 1
84
+ when 0 then 0
85
+ when 1 then 3
86
+ when 2 then 2
87
+ when 3 then 1
89
88
  end
90
89
  end
91
90
  end
@@ -2,4 +2,4 @@ module XDR::Concerns::FloatConverter
2
2
  def valid?(val)
3
3
  val.is_a?(Float)
4
4
  end
5
- end
5
+ end
@@ -2,4 +2,4 @@ module XDR::Concerns::IntegerConverter
2
2
  def valid?(val)
3
3
  val.is_a?(Integer)
4
4
  end
5
- end
5
+ end
@@ -1,8 +1,17 @@
1
1
  module XDR::Concerns::ReadsBytes
2
2
  private
3
+
3
4
  def read_bytes(io, length)
4
5
  io.read(length).tap do |bytes|
5
6
  raise EOFError if bytes.nil? || bytes.length != length
6
7
  end
7
8
  end
8
- end
9
+
10
+ def read_zeros(io, length)
11
+ read_bytes(io, length).each_byte do |byte|
12
+ raise XDR::ReadError unless byte == 0
13
+ end
14
+
15
+ nil
16
+ end
17
+ end
@@ -2,4 +2,4 @@ module XDR::Concerns::StringConverter
2
2
  def valid?(val)
3
3
  val.is_a?(String)
4
4
  end
5
- end
5
+ end
data/lib/xdr/double.rb CHANGED
@@ -8,7 +8,6 @@ module XDR::Double
8
8
  end
9
9
 
10
10
  def self.read(io)
11
- read_bytes(io, 8).unpack("G").first
11
+ read_bytes(io, 8).unpack1("G")
12
12
  end
13
-
14
- end
13
+ end
data/lib/xdr/dsl/enum.rb CHANGED
@@ -1,17 +1,16 @@
1
1
  module XDR::DSL::Enum
2
-
3
2
  def member(name, value)
4
- raise ArgumentError, "#{self} is sealed" if self.sealed
5
- raise ArgumentError, "#{value} is not Integer" unless value.is_a?(Integer)
6
- raise ArgumentError, "#{value} is already used" unless
7
-
8
3
  name = name.to_s.underscore
9
4
 
5
+ raise ArgumentError, "#{self} is sealed" if sealed
6
+ raise ArgumentError, "#{value} is not Integer" unless value.is_a?(Integer)
7
+ raise ArgumentError, "#{value} is already used" if members.key?(name)
8
+
10
9
  instance = new(name, value)
11
- self.members = self.members.merge(name => instance)
12
- self.by_value = self.by_value.merge(instance.value => instance)
10
+ self.members = members.merge(name => instance)
11
+ self.by_value = by_value.merge(instance.value => instance)
13
12
 
14
- class_eval <<-EOS, __FILE__, __LINE__
13
+ class_eval <<-EOS, __FILE__, __LINE__ + 1
15
14
  def self.#{name}
16
15
  members["#{name}"]
17
16
  end
@@ -21,4 +20,4 @@ module XDR::DSL::Enum
21
20
  def seal
22
21
  self.sealed = true
23
22
  end
24
- end
23
+ end
@@ -1,11 +1,10 @@
1
1
  module XDR::DSL::Struct
2
2
  def attribute(name, type)
3
-
4
3
  unless type.is_a?(XDR::Concerns::ConvertsToXDR)
5
4
  raise ArgumentError, "#{type} does not convert to xdr"
6
5
  end
7
6
 
8
- self.fields = self.fields.merge(name => type)
7
+ self.fields = fields.merge(name => type)
9
8
 
10
9
  define_method name do
11
10
  read_attribute(name)
data/lib/xdr/dsl/union.rb CHANGED
@@ -1,39 +1,39 @@
1
1
  module XDR::DSL::Union
2
2
  def switch_on(type, name)
3
- raise ArgumentError if self.switch_type.present?
3
+ raise ArgumentError if switch_type.present?
4
4
  self.switch_type = type
5
5
  self.switch_name = name
6
6
 
7
7
  alias_method name, :switch
8
8
  end
9
9
 
10
- def switch(switch, arm=nil)
11
- raise ArgumentError, "`switch_on` not defined yet" if self.switch_type.nil?
10
+ def switch(switch, arm = nil)
11
+ raise ArgumentError, "`switch_on` not defined yet" if switch_type.nil?
12
12
 
13
13
  switch = normalize_switch_declaration(switch)
14
- self.switches = self.switches.merge(switch => arm)
14
+ self.switches = switches.merge(switch => arm)
15
15
  end
16
16
 
17
17
  def attribute(name, type)
18
18
  raise ArgumentError, "#{type} does not convert to xdr" unless type.is_a?(XDR::Concerns::ConvertsToXDR)
19
19
 
20
- self.arms = self.arms.merge(name => type)
20
+ self.arms = arms.merge(name => type)
21
21
  define_attribute_methods name
22
22
  end
23
23
 
24
24
  private
25
+
25
26
  def normalize_switch_declaration(switch)
26
- case
27
- when switch.is_a?(self.switch_type)
27
+ if switch.is_a?(switch_type)
28
28
  switch
29
- when self.switch_type.valid?(switch)
29
+ elsif switch_type.valid?(switch)
30
30
  switch
31
- when switch == :default
31
+ elsif switch == :default
32
32
  switch
33
- when self.switch_type.respond_to?(:from_name)
34
- self.switch_type.from_name(switch)
33
+ elsif switch_type.respond_to?(:from_name)
34
+ switch_type.from_name(switch)
35
35
  else
36
- raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{self.switch_type}"
36
+ raise ArgumentError, "Cannot normalize switch: #{switch.inspect} to type: #{switch_type}"
37
37
  end
38
38
  end
39
39
  end
data/lib/xdr/dsl.rb CHANGED
@@ -4,4 +4,4 @@ module XDR::DSL
4
4
  autoload :Struct
5
5
  autoload :Union
6
6
  autoload :Enum
7
- end
7
+ end
data/lib/xdr/enum.rb CHANGED
@@ -7,7 +7,7 @@ class XDR::Enum
7
7
  class_attribute :sealed
8
8
  self.members = ActiveSupport::OrderedHash.new.with_indifferent_access
9
9
  self.by_value = ActiveSupport::OrderedHash.new
10
- self.sealed = false
10
+ self.sealed = false
11
11
 
12
12
  def self.write(val, io)
13
13
  raise XDR::WriteError, "Invalid enum value: #{val.inspect}" unless val.is_a?(self)
@@ -21,14 +21,14 @@ class XDR::Enum
21
21
  raise XDR::EnumValueError, "Unknown #{name} member: #{value}" if result.blank?
22
22
  end
23
23
  end
24
-
24
+
25
25
  def self.valid?(val)
26
26
  val.is_a?(self)
27
27
  end
28
28
 
29
29
  def self.from_name(name)
30
30
  normalized = name.to_s.underscore
31
- members[normalized].tap do |r|
31
+ members[normalized].tap do |r|
32
32
  raise XDR::EnumNameError, "#{name} is not a member of #{self.name}" if r.blank?
33
33
  end
34
34
  end
@@ -37,12 +37,12 @@ class XDR::Enum
37
37
  attr_reader :value
38
38
 
39
39
  def initialize(name, value)
40
- raise ArgumentError, "#{self.class} is sealed" if self.sealed
41
- @name = name
40
+ raise ArgumentError, "#{self.class} is sealed" if sealed
41
+ @name = name
42
42
  @value = value
43
43
  end
44
44
 
45
45
  def to_s
46
46
  "#{self.class.name}.#{@name}(#{@value})"
47
47
  end
48
- end
48
+ end
data/lib/xdr/float.rb CHANGED
@@ -8,7 +8,6 @@ module XDR::Float
8
8
  end
9
9
 
10
10
  def self.read(io)
11
- read_bytes(io, 4).unpack("g").first
11
+ read_bytes(io, 4).unpack1("g")
12
12
  end
13
-
14
- end
13
+ end
data/lib/xdr/hyper.rb CHANGED
@@ -9,6 +9,6 @@ module XDR::Hyper
9
9
  end
10
10
 
11
11
  def self.read(io)
12
- read_bytes(io, 8).unpack("q>").first
12
+ read_bytes(io, 8).unpack1("q>")
13
13
  end
14
- end
14
+ end
data/lib/xdr/int.rb CHANGED
@@ -9,6 +9,6 @@ module XDR::Int
9
9
  end
10
10
 
11
11
  def self.read(io)
12
- read_bytes(io, 4).unpack("l>").first
12
+ read_bytes(io, 4).unpack1("l>")
13
13
  end
14
- end
14
+ end
data/lib/xdr/namespace.rb CHANGED
@@ -1,9 +1,9 @@
1
- #
1
+ #
2
2
  # A thin wrapper around ActiveSupport::Autoload configured to always eager_load
3
3
  # a child. By calling `load_all!` on the namespace module, all children will
4
4
  # be eager loaded, allowing us to more easily operate in a multi-threaded
5
5
  # environment.
6
- #
6
+ #
7
7
  module XDR::Namespace
8
8
  extend ActiveSupport::Concern
9
9
 
@@ -14,13 +14,11 @@ module XDR::Namespace
14
14
  module ClassMethods
15
15
  def load_all!
16
16
  constants.each do |const_name|
17
- begin
18
- const = const_get const_name
19
- const.public_send :load_all! if const.respond_to? :load_all!
20
- rescue NameError => e
21
- raise e unless e.message =~ /uninitialized constant #{const_name}/
22
- end
17
+ const = const_get const_name
18
+ const.public_send :load_all! if const.respond_to? :load_all!
19
+ rescue NameError => e
20
+ raise e unless /uninitialized constant #{const_name}/.match?(e.message)
23
21
  end
24
22
  end
25
23
  end
26
- end
24
+ end
data/lib/xdr/opaque.rb CHANGED
@@ -12,17 +12,17 @@ class XDR::Opaque
12
12
  def read(io)
13
13
  # read and return @length bytes
14
14
  # throw away @padding bytes
15
- read_bytes(io, @length).tap{ read_bytes(io, @padding) }
15
+ read_bytes(io, @length).tap { read_zeros(io, @padding) }
16
16
  end
17
17
 
18
- def write(val,io)
18
+ def write(val, io)
19
19
  length = val.bytesize
20
-
20
+
21
21
  if val.length != @length
22
- raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
22
+ raise XDR::WriteError, "Value length is #{length}, must be #{@length}"
23
23
  end
24
24
 
25
25
  io.write val
26
26
  io.write "\x00" * @padding
27
27
  end
28
- end
28
+ end
data/lib/xdr/option.rb CHANGED
@@ -6,16 +6,16 @@ class XDR::Option
6
6
  attr_reader :child_type
7
7
 
8
8
  def initialize(child_type)
9
- #TODO, raise an error if child_type is not ConvertToXDR
9
+ # TODO, raise an error if child_type is not ConvertToXDR
10
10
  @child_type = child_type
11
11
  end
12
12
 
13
13
  def write(val, io)
14
- if val.present?
14
+ if val.nil?
15
+ XDR::Bool.write(false, io)
16
+ else
15
17
  XDR::Bool.write(true, io)
16
18
  @child_type.write(val, io)
17
- else
18
- XDR::Bool.write(false, io)
19
19
  end
20
20
  end
21
21
 
@@ -27,4 +27,4 @@ class XDR::Option
27
27
  def valid?(val)
28
28
  val.nil? || @child_type.valid?(val)
29
29
  end
30
- end
30
+ end
data/lib/xdr/quadruple.rb CHANGED
@@ -2,4 +2,4 @@ module XDR::Quadruple
2
2
  extend XDR::Concerns::ConvertsToXDR
3
3
 
4
4
  # No support for quads, so we don't overide read/write
5
- end
5
+ end