union 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/README +16 -16
- data/Rakefile +16 -18
- data/lib/union.rb +40 -40
- data/test/test_union.rb +38 -38
- data/union.gemspec +16 -17
- metadata +19 -9
data/CHANGES
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
== 1.0.3 - 22-Sep-2011
|
2
|
+
* Refactored the Rakefile. Removed the old install task, added
|
3
|
+
a default task, and reorganized the gem related tasks.
|
4
|
+
* Minor fix/update for gemspec.
|
5
|
+
|
1
6
|
== 1.0.2 - 3-Oct-2009
|
2
7
|
* Some gemspec updates and one fix.
|
3
8
|
* Added the :gem rake task.
|
data/README
CHANGED
@@ -1,31 +1,31 @@
|
|
1
1
|
= Description
|
2
|
-
The union library
|
2
|
+
The union library provides the Ruby analog of a C union.
|
3
3
|
|
4
4
|
= Installation
|
5
|
-
|
6
|
-
rake install (standard) or rake install_gem (rubygems)
|
5
|
+
gem install union
|
7
6
|
|
8
7
|
= Synopsis
|
9
|
-
|
8
|
+
require 'union'
|
10
9
|
|
11
|
-
|
12
|
-
|
10
|
+
Union.new('Human', :name, :age, :height)
|
11
|
+
h = Union::Human.new
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
# Only one attribute of the union may be set
|
14
|
+
h.name = 'Daniel' # => #<struct Union::Human name="Daniel", age=nil>
|
15
|
+
h.age = 38 # => #<struct Union::Human name=nil, age=38>
|
17
16
|
|
18
17
|
= Known issues or bugs
|
19
|
-
None that I'm aware of. Please report any bugs you find on the project
|
20
|
-
at http://www.rubyforge.org/projects/shards
|
18
|
+
None that I'm aware of. Please report any bugs you find on the project
|
19
|
+
page at http://www.rubyforge.org/projects/shards or on the github
|
20
|
+
project page at https://github.com/djberg96/union.
|
21
21
|
|
22
22
|
= License
|
23
|
-
Artistic 2.0
|
23
|
+
Artistic 2.0
|
24
24
|
|
25
25
|
= Warranty
|
26
|
-
This package is provided "as is" and without any express or
|
27
|
-
implied warranties, including, without limitation, the implied
|
28
|
-
warranties of merchantability and fitness for a particular purpose.
|
26
|
+
This package is provided "as is" and without any express or
|
27
|
+
implied warranties, including, without limitation, the implied
|
28
|
+
warranties of merchantability and fitness for a particular purpose.
|
29
29
|
|
30
30
|
= Author
|
31
|
-
|
31
|
+
Daniel J. Berger
|
data/Rakefile
CHANGED
@@ -1,27 +1,25 @@
|
|
1
1
|
require 'rake'
|
2
|
+
require 'rake/clean'
|
2
3
|
require 'rake/testtask'
|
3
|
-
require 'rbconfig'
|
4
|
-
include Config
|
5
4
|
|
6
|
-
|
7
|
-
task :install do
|
8
|
-
sitelibdir = CONFIG["sitelibdir"]
|
9
|
-
file = "lib/union.rb"
|
10
|
-
FileUtils.cp(file, sitelibdir, :verbose => true)
|
11
|
-
end
|
5
|
+
CLEAN.include("**/*.gem", "**/*.rbc")
|
12
6
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
7
|
+
namespace :gem do
|
8
|
+
desc 'Build the union gem'
|
9
|
+
task :create do
|
10
|
+
spec = eval(IO.read('union.gemspec'))
|
11
|
+
Gem::Builder.new(spec).build
|
12
|
+
end
|
18
13
|
|
19
|
-
task :
|
20
|
-
|
21
|
-
|
14
|
+
task :install => [:create] do
|
15
|
+
file = Dir["*.gem"].first
|
16
|
+
sh "gem install #{file}"
|
17
|
+
end
|
22
18
|
end
|
23
19
|
|
24
20
|
Rake::TestTask.new do |t|
|
25
|
-
|
26
|
-
|
21
|
+
t.verbose = true
|
22
|
+
t.warning = true
|
27
23
|
end
|
24
|
+
|
25
|
+
task :default => :test
|
data/lib/union.rb
CHANGED
@@ -2,46 +2,46 @@
|
|
2
2
|
# a Union object can be set to a non-nil value at a time.
|
3
3
|
#
|
4
4
|
class Union < Struct
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
}
|
29
|
-
instance_eval(m)
|
5
|
+
# The version of the union library
|
6
|
+
VERSION = '1.0.3'
|
7
|
+
|
8
|
+
# Creates and returns a new Union. Unlike Struct::Class.new, this does not
|
9
|
+
# take any arguments. You must assign attributes individually.
|
10
|
+
#
|
11
|
+
# Union.new('Human', :name, :age)
|
12
|
+
# h = Union::Human.new
|
13
|
+
#
|
14
|
+
# h.name = 'Daniel' # => #<struct Union::Human name="Daniel", age=nil>
|
15
|
+
# h.age = 38 # => #<struct Union::Human name=nil, age=38>
|
16
|
+
#--
|
17
|
+
# The semantics of Union.new(arg1, arg2, ...) would be non-sensical. Which
|
18
|
+
# attribute should be set while the rest are ignored? The first one or the
|
19
|
+
# last one? I decided to disallow it altogether.
|
20
|
+
#
|
21
|
+
def initialize
|
22
|
+
super
|
23
|
+
members.each{ |attribute|
|
24
|
+
m = %Q{
|
25
|
+
def #{attribute}=(value)
|
26
|
+
self['#{attribute}'] = value
|
27
|
+
end
|
30
28
|
}
|
31
|
-
|
29
|
+
instance_eval(m)
|
30
|
+
}
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
33
|
+
# Identical to Struct attribute assignment, except that setting one instance
|
34
|
+
# variable sets all other instance variables to nil. Also, you cannot use a
|
35
|
+
# numeric index. You must use a string or symbol.
|
36
|
+
#
|
37
|
+
# Union.new('Human', :name, :age)
|
38
|
+
# h = Union::Human.new
|
39
|
+
#
|
40
|
+
# h[:name] = 'Daniel' # => #<struct Union::Human name="Daniel", age=nil>
|
41
|
+
# h[:age] = 38 # => #<struct Union::Human name=nil, age=38>
|
42
|
+
#
|
43
|
+
def []=(symbol, value)
|
44
|
+
super(symbol, value)
|
45
|
+
members.each{ |m| super(m, nil) unless m.to_s == symbol.to_s }
|
46
|
+
end
|
47
47
|
end
|
data/test/test_union.rb
CHANGED
@@ -2,49 +2,49 @@ require 'union'
|
|
2
2
|
require 'test/unit'
|
3
3
|
|
4
4
|
class TC_Union < Test::Unit::TestCase
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
def setup
|
6
|
+
Union.new('Human', :name, :age, :height) unless defined? Union::Human
|
7
|
+
@union = Union::Human.new
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
def test_union_version
|
11
|
+
assert_equal('1.0.3', Union::VERSION)
|
12
|
+
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
14
|
+
def test_union_constructor
|
15
|
+
assert_raise(ArgumentError){ Union::Human.new('Matz') }
|
16
|
+
end
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
def test_union_attribute_assignment_basic
|
19
|
+
assert_nothing_raised{ @union.name = 'Daniel' }
|
20
|
+
assert_equal('Daniel', @union.name)
|
21
|
+
end
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
def test_union_attribute_assignment_by_method_name
|
24
|
+
assert_nothing_raised{ @union.name = 'Daniel' }
|
25
|
+
assert_nothing_raised{ @union.age = 38 }
|
26
|
+
assert_nil(@union.name)
|
27
|
+
assert_nil(@union.height)
|
28
|
+
assert_equal(38, @union.age)
|
29
|
+
end
|
30
30
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
31
|
+
def test_union_attribute_assignment_by_string_ref
|
32
|
+
assert_nothing_raised{ @union['name'] = 'Daniel' }
|
33
|
+
assert_nothing_raised{ @union['age'] = 38 }
|
34
|
+
assert_nil(@union['name'])
|
35
|
+
assert_nil(@union['height'])
|
36
|
+
assert_equal(38, @union['age'])
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
39
|
+
def test_union_attribute_assignment_by_symbol_ref
|
40
|
+
assert_nothing_raised{ @union[:name] = 'Daniel' }
|
41
|
+
assert_nothing_raised{ @union[:age] = 38 }
|
42
|
+
assert_nil(@union[:name])
|
43
|
+
assert_nil(@union[:height])
|
44
|
+
assert_equal(38, @union[:age])
|
45
|
+
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
def teardown
|
48
|
+
@union = nil
|
49
|
+
end
|
50
50
|
end
|
data/union.gemspec
CHANGED
@@ -1,23 +1,22 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
gem.files = Dir['**/*'].reject{ |f| f.include?('CVS') }
|
4
|
+
gem.name = 'union'
|
5
|
+
gem.version = '1.0.3'
|
6
|
+
gem.author = 'Daniel J. Berger'
|
7
|
+
gem.license = 'Artistic 2.0'
|
8
|
+
gem.email = 'djberg96@gmail.com'
|
9
|
+
gem.homepage = 'http://www.rubyforge.org/projects/shards'
|
10
|
+
gem.summary = 'A struct-like C union for Ruby'
|
11
|
+
gem.test_file = 'test/test_union.rb'
|
12
|
+
gem.files = Dir['**/*'].reject{ |f| f.include?('git') }
|
14
13
|
|
15
|
-
|
14
|
+
gem.extra_rdoc_files = ['README', 'CHANGES', 'MANIFEST']
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
16
|
+
gem.description = <<-EOF
|
17
|
+
The union library provides an analog to a C/C++ union for Ruby.
|
18
|
+
In this implementation a union is a kind of struct where multiple
|
19
|
+
members may be defined, but only one member ever contains a non-nil
|
20
|
+
value at any given time.
|
21
|
+
EOF
|
23
22
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: union
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 3
|
10
|
+
version: 1.0.3
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Daniel J. Berger
|
@@ -9,11 +15,10 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
13
|
-
default_executable:
|
18
|
+
date: 2011-09-23 00:00:00 Z
|
14
19
|
dependencies: []
|
15
20
|
|
16
|
-
description: "
|
21
|
+
description: " The union library provides an analog to a C/C++ union for Ruby.\n In this implementation a union is a kind of struct where multiple\n members may be defined, but only one member ever contains a non-nil\n value at any given time.\n"
|
17
22
|
email: djberg96@gmail.com
|
18
23
|
executables: []
|
19
24
|
|
@@ -31,7 +36,6 @@ files:
|
|
31
36
|
- README
|
32
37
|
- test/test_union.rb
|
33
38
|
- union.gemspec
|
34
|
-
has_rdoc: true
|
35
39
|
homepage: http://www.rubyforge.org/projects/shards
|
36
40
|
licenses:
|
37
41
|
- Artistic 2.0
|
@@ -41,21 +45,27 @@ rdoc_options: []
|
|
41
45
|
require_paths:
|
42
46
|
- lib
|
43
47
|
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
44
49
|
requirements:
|
45
50
|
- - ">="
|
46
51
|
- !ruby/object:Gem::Version
|
52
|
+
hash: 3
|
53
|
+
segments:
|
54
|
+
- 0
|
47
55
|
version: "0"
|
48
|
-
version:
|
49
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
50
58
|
requirements:
|
51
59
|
- - ">="
|
52
60
|
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
53
64
|
version: "0"
|
54
|
-
version:
|
55
65
|
requirements: []
|
56
66
|
|
57
|
-
rubyforge_project:
|
58
|
-
rubygems_version: 1.
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.8.10
|
59
69
|
signing_key:
|
60
70
|
specification_version: 3
|
61
71
|
summary: A struct-like C union for Ruby
|