union 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (8) hide show
  1. data/CHANGES +2 -0
  2. data/MANIFEST +7 -0
  3. data/README +31 -0
  4. data/Rakefile +22 -0
  5. data/lib/union.rb +47 -0
  6. data/test/test_union.rb +50 -0
  7. data/union.gemspec +22 -0
  8. metadata +65 -0
data/CHANGES ADDED
@@ -0,0 +1,2 @@
1
+ = 1.0.0 - 10-Jun-2008
2
+ * Initial release
data/MANIFEST ADDED
@@ -0,0 +1,7 @@
1
+ * CHANGES
2
+ * MANIFEST
3
+ * README
4
+ * Rakefile
5
+ * facade.gemspec
6
+ * lib/union.rb
7
+ * test/test_union.rb
data/README ADDED
@@ -0,0 +1,31 @@
1
+ = Description
2
+ A struct-like C union class for Ruby.
3
+
4
+ = Installation
5
+ rake test (optional)
6
+ rake install (standard) or rake install_gem (rubygems)
7
+
8
+ = Synopsis
9
+ require 'union'
10
+
11
+ Union.new('Human', :name, :age, :height)
12
+ h = Union::Human.new
13
+
14
+ # Only one attribute of the union may be set
15
+ h.name = 'Daniel' # => #<struct Union::Human name="Daniel", age=nil>
16
+ h.age = 38 # => #<struct Union::Human name=nil, age=38>
17
+
18
+ = Known issues or bugs
19
+ None that I'm aware of. Please report any bugs you find on the project page
20
+ at http://www.rubyforge.org/projects/shards
21
+
22
+ = License
23
+ Ruby's
24
+
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.
29
+
30
+ = Author
31
+ * Daniel J. Berger
data/Rakefile ADDED
@@ -0,0 +1,22 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rbconfig'
4
+ include Config
5
+
6
+ desc 'Install the union library (non-gem)'
7
+ task :install do
8
+ sitelibdir = CONFIG["sitelibdir"]
9
+ file = "lib/union.rb"
10
+ FileUtils.cp(file, sitelibdir, :verbose => true)
11
+ end
12
+
13
+ task :install_gem do
14
+ ruby 'union.gemspec'
15
+ file = Dir["*.gem"].first
16
+ sh "gem install #{file}"
17
+ end
18
+
19
+ Rake::TestTask.new do |t|
20
+ t.verbose = true
21
+ t.warning = true
22
+ end
data/lib/union.rb ADDED
@@ -0,0 +1,47 @@
1
+ # The Union class mimics a C union, where only one member of a Union
2
+ # object can be set to a non-nil value at a time.
3
+ #
4
+ class Union < Struct
5
+ # The version of this library
6
+ VERSION = '1.0.0'
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
28
+ }
29
+ instance_eval(m)
30
+ }
31
+ end
32
+
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 == symbol.to_s }
46
+ end
47
+ end
@@ -0,0 +1,50 @@
1
+ require 'union'
2
+ require 'test/unit'
3
+
4
+ class TC_Union < Test::Unit::TestCase
5
+ def setup
6
+ Union.new('Human', :name, :age, :height) unless defined? Union::Human
7
+ @union = Union::Human.new
8
+ end
9
+
10
+ def test_union_version
11
+ assert_equal('1.0.0', Union::VERSION)
12
+ end
13
+
14
+ def test_union_constructor
15
+ assert_raise(ArgumentError){ Union::Human.new('Matz') }
16
+ end
17
+
18
+ def test_union_attribute_assignment_basic
19
+ assert_nothing_raised{ @union.name = 'Daniel' }
20
+ assert_equal('Daniel', @union.name)
21
+ end
22
+
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
+
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
+
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
+
47
+ def teardown
48
+ @union = nil
49
+ end
50
+ end
data/union.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ require "rubygems"
2
+
3
+ spec = Gem::Specification.new do |gem|
4
+ gem.name = "union"
5
+ gem.version = "1.0.0"
6
+ gem.author = "Daniel J. Berger"
7
+ gem.email = "djberg96@gmail.com"
8
+ gem.homepage = "http://www.rubyforge.org/projects/shards"
9
+ gem.platform = Gem::Platform::RUBY
10
+ gem.summary = "A struct-like C union for Ruby"
11
+ gem.test_file = "test/test_union.rb"
12
+ gem.has_rdoc = true
13
+ gem.files = Dir['lib/*.rb'] + Dir['[A-Z]*'] + Dir['test/*']
14
+ gem.files.reject! { |fn| fn.include? "CVS" }
15
+ gem.require_path = "lib"
16
+ gem.extra_rdoc_files = ["README", "CHANGES", "MANIFEST"]
17
+ end
18
+
19
+ if $0 == __FILE__
20
+ Gem.manage_gems
21
+ Gem::Builder.new(spec).build
22
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: union
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel J. Berger
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-06-10 00:00:00 -06:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: djberg96@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ - CHANGES
25
+ - MANIFEST
26
+ files:
27
+ - lib/union.rb
28
+ - CHANGES
29
+ - CVS
30
+ - lib
31
+ - MANIFEST
32
+ - Rakefile
33
+ - README
34
+ - test
35
+ - union.gemspec
36
+ - test/CVS
37
+ - test/test_union.rb
38
+ has_rdoc: true
39
+ homepage: http://www.rubyforge.org/projects/shards
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.1.1
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: A struct-like C union for Ruby
64
+ test_files:
65
+ - test/test_union.rb