chemistry 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -0
- data/Rakefile +1 -1
- data/lib/chemistry/element.rb +41 -0
- data/lib/chemistry/element/dsl.rb +15 -0
- data/lib/chemistry/version.rb +1 -1
- data/test/chemistry/element/test_dsl.rb +43 -0
- data/test/chemistry/test_element.rb +101 -0
- data/test/test_helper.rb +6 -0
- metadata +9 -3
data/Gemfile
CHANGED
data/Rakefile
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Chemistry
|
2
|
+
class Element
|
3
|
+
class << self
|
4
|
+
def symbol symbol
|
5
|
+
if symbol.is_a? String
|
6
|
+
const_set :SYMBOL, symbol
|
7
|
+
else
|
8
|
+
raise TypeError, "can't convert #{symbol.class} into String"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def atomic_number atomic_number
|
13
|
+
if atomic_number.is_a? Integer
|
14
|
+
const_set :ATOMIC_NUMBER, atomic_number
|
15
|
+
else
|
16
|
+
raise TypeError, "can't convert #{atomic_number.class} into Integer"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def atomic_weight atomic_weight
|
21
|
+
if atomic_weight.is_a? Numeric
|
22
|
+
const_set :ATOMIC_WEIGHT, atomic_weight
|
23
|
+
else
|
24
|
+
raise TypeError, "can't convert #{atomic_weight.class} into Numeric"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def symbol
|
30
|
+
self.class::SYMBOL
|
31
|
+
end
|
32
|
+
|
33
|
+
def atomic_number
|
34
|
+
self.class::ATOMIC_NUMBER
|
35
|
+
end
|
36
|
+
|
37
|
+
def atomic_weight
|
38
|
+
self.class::ATOMIC_WEIGHT
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'chemistry/element'
|
2
|
+
|
3
|
+
module Chemistry::Element::DSL
|
4
|
+
extend self
|
5
|
+
|
6
|
+
def element(name, &definition)
|
7
|
+
if block_given?
|
8
|
+
element_class = Class.new(Chemistry::Element)
|
9
|
+
Chemistry::Element.const_set(name, element_class)
|
10
|
+
element_class.instance_eval &definition if block_given?
|
11
|
+
else
|
12
|
+
raise ArgumentError, "`element` must be given a block"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/chemistry/version.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ElementTest < MiniTest::Unit::TestCase
|
4
|
+
def test_raises_error_without_name
|
5
|
+
assert_raises ArgumentError do
|
6
|
+
::Chemistry::Element::DSL.element
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_raises_error_without_block
|
11
|
+
assert_raises ArgumentError do
|
12
|
+
Chemistry::Element::DSL.element "Hydrogen"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_defines_an_element
|
17
|
+
assert_raises NameError do
|
18
|
+
::Chemistry::Element::Foobar
|
19
|
+
end
|
20
|
+
|
21
|
+
::Chemistry::Element::DSL.element "Foobar" do
|
22
|
+
end
|
23
|
+
|
24
|
+
assert_equal ::Chemistry::Element <=> ::Chemistry::Element::Foobar, 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class DefinitionTest < MiniTest::Unit::TestCase
|
29
|
+
include Chemistry::Element::DSL
|
30
|
+
|
31
|
+
def test_full_definition
|
32
|
+
element "Hydrogen" do
|
33
|
+
symbol 'H'
|
34
|
+
atomic_number 1
|
35
|
+
atomic_weight 1.00794
|
36
|
+
end
|
37
|
+
|
38
|
+
h_atom = Chemistry::Element::Hydrogen.new
|
39
|
+
|
40
|
+
assert_equal h_atom.symbol, 'H'
|
41
|
+
assert_equal h_atom.atomic_number, 1
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ClassLevelAtomicNumberTest < ElementTestCase
|
4
|
+
def test_defines_a_constant
|
5
|
+
assert_raises NameError do
|
6
|
+
element::ATOMIC_NUMBER
|
7
|
+
end
|
8
|
+
|
9
|
+
element.atomic_number 14
|
10
|
+
|
11
|
+
assert_equal element::ATOMIC_NUMBER, 14
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_must_be_an_integer
|
15
|
+
assert_raises TypeError do
|
16
|
+
element.atomic_number "a"
|
17
|
+
end
|
18
|
+
|
19
|
+
assert_raises TypeError do
|
20
|
+
element.atomic_number 1.2
|
21
|
+
end
|
22
|
+
|
23
|
+
assert element.atomic_number 12
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class InstanceLevelAtomicNumberTest < ElementTestCase
|
28
|
+
def test_requires_atomic_number_to_be_defined_on_the_class_level
|
29
|
+
assert_raises NameError do
|
30
|
+
element.new.atomic_number
|
31
|
+
end
|
32
|
+
|
33
|
+
element.atomic_number 123
|
34
|
+
|
35
|
+
assert_equal element.new.atomic_number, 123
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class ClassLevelAtomicWeightTest < ElementTestCase
|
40
|
+
def test_defines_a_constant
|
41
|
+
assert_raises NameError do
|
42
|
+
element::ATOMIC_WEIGHT
|
43
|
+
end
|
44
|
+
|
45
|
+
element.atomic_weight 1.12342314
|
46
|
+
|
47
|
+
assert_equal element::ATOMIC_WEIGHT, 1.12342314
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_must_be_a_number
|
51
|
+
assert_raises TypeError do
|
52
|
+
element.atomic_weight "a"
|
53
|
+
end
|
54
|
+
|
55
|
+
assert element.atomic_weight 1.2
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
class InstanceLevelAtomicWeightTest < ElementTestCase
|
60
|
+
def test_requires_atomic_weight_to_be_defined_on_the_class_level
|
61
|
+
assert_raises NameError do
|
62
|
+
element.new.atomic_weight
|
63
|
+
end
|
64
|
+
|
65
|
+
element.atomic_weight 1.12342314
|
66
|
+
|
67
|
+
assert_equal element.new.atomic_weight, 1.12342314
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class ClassLevelSymbolTest < ElementTestCase
|
72
|
+
def test_defines_a_constant
|
73
|
+
assert_raises NameError do
|
74
|
+
element::SYMBOL
|
75
|
+
end
|
76
|
+
|
77
|
+
element.symbol "H"
|
78
|
+
|
79
|
+
assert_equal element::SYMBOL, "H"
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_must_be_a_string_or_symbol
|
83
|
+
assert_raises TypeError do
|
84
|
+
element.symbol 1
|
85
|
+
end
|
86
|
+
|
87
|
+
assert element.symbol 'H'
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
class InstanceLevelSymbolTest < ElementTestCase
|
92
|
+
def test_requires_symbol_to_be_defined_on_the_class_level
|
93
|
+
assert_raises NameError do
|
94
|
+
element.new.symbol
|
95
|
+
end
|
96
|
+
|
97
|
+
element.symbol 'He'
|
98
|
+
|
99
|
+
assert_equal element.new.symbol, 'He'
|
100
|
+
end
|
101
|
+
end
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chemistry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -26,7 +26,11 @@ files:
|
|
26
26
|
- Rakefile
|
27
27
|
- chemistry.gemspec
|
28
28
|
- lib/chemistry.rb
|
29
|
+
- lib/chemistry/element.rb
|
30
|
+
- lib/chemistry/element/dsl.rb
|
29
31
|
- lib/chemistry/version.rb
|
32
|
+
- test/chemistry/element/test_dsl.rb
|
33
|
+
- test/chemistry/test_element.rb
|
30
34
|
- test/test_chemistry.rb
|
31
35
|
- test/test_helper.rb
|
32
36
|
homepage: http://isaacbfsanders.com/chemistry
|
@@ -43,7 +47,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
47
|
version: '0'
|
44
48
|
segments:
|
45
49
|
- 0
|
46
|
-
hash: -
|
50
|
+
hash: -3193914091140684152
|
47
51
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
52
|
none: false
|
49
53
|
requirements:
|
@@ -52,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
56
|
version: '0'
|
53
57
|
segments:
|
54
58
|
- 0
|
55
|
-
hash: -
|
59
|
+
hash: -3193914091140684152
|
56
60
|
requirements: []
|
57
61
|
rubyforge_project:
|
58
62
|
rubygems_version: 1.8.24
|
@@ -60,5 +64,7 @@ signing_key:
|
|
60
64
|
specification_version: 3
|
61
65
|
summary: Chemistry is a toolkit for dealing with compounds
|
62
66
|
test_files:
|
67
|
+
- test/chemistry/element/test_dsl.rb
|
68
|
+
- test/chemistry/test_element.rb
|
63
69
|
- test/test_chemistry.rb
|
64
70
|
- test/test_helper.rb
|