eulim 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
data/bin/console CHANGED
@@ -1,14 +1,14 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'eulim'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require 'pry'
11
- # Pry.start
12
-
13
- require 'irb'
14
- IRB.start(__FILE__)
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'eulim'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require 'pry'
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/eulim.gemspec CHANGED
@@ -1,43 +1,43 @@
1
- # coding: utf-8
2
-
3
- lib = File.expand_path('../lib', __FILE__)
4
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'eulim/version'
6
-
7
- Gem::Specification.new do |spec|
8
- spec.name = 'eulim'
9
- spec.version = Eulim::VERSION
10
- spec.authors = ['Syed Fazil Basheer']
11
- spec.email = ['fazil.basheer@quester.xyz']
12
-
13
- spec.summary = 'A gem for scientific data.'
14
- spec.description = 'A gem for scientific data.'
15
- spec.homepage = 'https://github.com/syedfazilbasheer-quester/eulim-gem'
16
- spec.license = 'MIT'
17
-
18
- # Prevent pushing this gem to RubyGems.org.
19
- # To allow pushes either set the 'allowed_push_host'
20
- # to allow pushing to a single host
21
- # or delete this section to allow pushing to any host.
22
- # if spec.respond_to?(:metadata)
23
- # spec.metadata['allowed_push_host'] =
24
- # 'https://github.com/syedfazilbasheer-quester/eulim-gem'
25
- # else
26
- # raise 'RubyGems 2.0 or newer is required to protect against ' \
27
- # 'public gem pushes.'
28
- # end
29
-
30
- spec.files = `git ls-files -z`.split("\x0").reject do |f|
31
- f.match(%r{^(test|spec|features)/})
32
- end
33
- spec.bindir = 'exe'
34
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
- spec.require_paths = ['lib']
36
-
37
- spec.add_development_dependency 'bundler', '~> 1.14'
38
- spec.add_development_dependency 'rake', '~> 10.0'
39
- spec.add_development_dependency 'rspec', '~> 3.0'
40
- spec.add_development_dependency 'rubocop'
41
-
42
- spec.add_dependency 'require_all', '~> 1.4.0'
43
- end
1
+ # coding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'eulim/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'eulim'
9
+ spec.version = Eulim::VERSION
10
+ spec.authors = ['Syed Fazil Basheer']
11
+ spec.email = ['fazil.basheer@quester.xyz']
12
+
13
+ spec.summary = 'A gem for scientific data.'
14
+ spec.description = 'A gem for scientific data.'
15
+ spec.homepage = 'https://github.com/syedfazilbasheer-quester/eulim-gem'
16
+ spec.license = 'MIT'
17
+
18
+ # Prevent pushing this gem to RubyGems.org.
19
+ # To allow pushes either set the 'allowed_push_host'
20
+ # to allow pushing to a single host
21
+ # or delete this section to allow pushing to any host.
22
+ # if spec.respond_to?(:metadata)
23
+ # spec.metadata['allowed_push_host'] =
24
+ # 'https://github.com/syedfazilbasheer-quester/eulim-gem'
25
+ # else
26
+ # raise 'RubyGems 2.0 or newer is required to protect against ' \
27
+ # 'public gem pushes.'
28
+ # end
29
+
30
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
31
+ f.match(%r{^(test|spec|features)/})
32
+ end
33
+ spec.bindir = 'exe'
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ['lib']
36
+
37
+ spec.add_development_dependency 'bundler', '~> 1.14'
38
+ spec.add_development_dependency 'rake', '~> 10.0'
39
+ spec.add_development_dependency 'rspec', '~> 3.0'
40
+ spec.add_development_dependency 'rubocop'
41
+
42
+ spec.add_dependency 'require_all', '~> 1.4.0'
43
+ end
data/lib/eulim.rb CHANGED
@@ -1,8 +1,8 @@
1
- require 'require_all'
2
-
3
- require_all '../eulim/lib'
4
-
5
- # Root module. Everything in the gem goes inside this
6
- module Eulim
7
- # Your code goes here...
8
- end
1
+ require 'require_all'
2
+
3
+ require_all '../eulim/lib'
4
+
5
+ # Root module. Everything in the gem goes inside this
6
+ module Eulim
7
+ # Your code goes here...
8
+ end
@@ -1,6 +1,6 @@
1
- module Eulim
2
- # This module will contain all chemistry related info
3
- # Ex: Elements, Compounds, Reactions, etc
4
- module Chemistry
5
- end
6
- end
1
+ module Eulim
2
+ # This module will contain all chemistry related info
3
+ # Ex: Elements, Compounds, Reactions, etc
4
+ module Chemistry
5
+ end
6
+ end
@@ -1,59 +1,59 @@
1
- module Eulim
2
- module Chemistry
3
- # This class has functionality for compounds
4
- # Ex: constituent elements, molecular mass, etc
5
- class Compound
6
- COMPOUND_REGEXP =
7
- /[A-Z][a-z]{0,2}\d*|\((?:[^()]*(?:\(.*\))?[^()]*)+\)\d*/
8
-
9
- attr_accessor :molecular_mass, :constituents, :formula
10
-
11
- def initialize(arg)
12
- @formula = arg
13
- @constituents = constituents
14
- @molecular_mass = molecular_mass
15
- end
16
-
17
- private
18
-
19
- def molecular_mass
20
- mass = 0
21
- @constituents.each do |const|
22
- mass += const[:element].atomic_mass * const[:atom_count]
23
- end
24
- mass
25
- end
26
-
27
- def constituents
28
- constituents = []
29
- get_const_atoms.each do |symbol, count|
30
- constituents << {
31
- element: Element.get_by_symbol(symbol),
32
- atom_count: count
33
- }
34
- end
35
- constituents
36
- end
37
-
38
- def get_const_atoms(formula = @formula, r = {})
39
- formula.scan(COMPOUND_REGEXP).each do |const|
40
- multipler = get_multipler const
41
- if const[0] != '(' && multipler.zero?
42
- r[const] = r[const] ? r[const] + 1 : 1
43
- else
44
- (multipler.zero? ? 1 : multipler).times do
45
- sub_const = const.match(/^\(?(.*?)\)?($|\d*$)/).to_a
46
- get_const_atoms sub_const[const == sub_const.first ? 1 : 0], r
47
- end
48
- end
49
- end
50
- r
51
- end
52
-
53
- def get_multipler(const)
54
- multipler = const.match(/\d*$/).to_a.first.to_i
55
- multipler
56
- end
57
- end
58
- end
59
- end
1
+ module Eulim
2
+ module Chemistry
3
+ # This class has functionality for compounds
4
+ # Ex: constituent elements, molecular mass, etc
5
+ class Compound
6
+ COMPOUND_REGEXP =
7
+ /[A-Z][a-z]{0,2}\d*|\((?:[^()]*(?:\(.*\))?[^()]*)+\)\d*/
8
+
9
+ attr_accessor :molecular_mass, :constituents, :formula
10
+
11
+ def initialize(arg)
12
+ @formula = arg
13
+ build_constituents
14
+ calculate_molecular_mass
15
+ end
16
+
17
+ private
18
+
19
+ def calculate_molecular_mass
20
+ @molecular_mass = 0
21
+ @constituents.each do |_symbol, info|
22
+ @molecular_mass += info[:element].atomic_mass * info[:atom_count]
23
+ end
24
+ @molecular_mass
25
+ end
26
+
27
+ def build_constituents
28
+ @constituents = {}
29
+ get_const_atoms.each do |symbol, count|
30
+ @constituents[symbol] = {
31
+ element: Element.get_by_symbol(symbol),
32
+ atom_count: count
33
+ }
34
+ end
35
+ @constituents
36
+ end
37
+
38
+ def get_const_atoms(formula = @formula, r = {})
39
+ formula.scan(COMPOUND_REGEXP).each do |const|
40
+ multipler = get_multipler const
41
+ if const[0] != '(' && multipler.zero?
42
+ r[const] = r[const] ? r[const] + 1 : 1
43
+ else
44
+ (multipler.zero? ? 1 : multipler).times do
45
+ sub_const = const.match(/^\(?(.*?)\)?($|\d*$)/).to_a
46
+ get_const_atoms sub_const[const == sub_const.first ? 1 : 0], r
47
+ end
48
+ end
49
+ end
50
+ r
51
+ end
52
+
53
+ def get_multipler(const)
54
+ multipler = const.match(/\d*$/).to_a.first.to_i
55
+ multipler
56
+ end
57
+ end
58
+ end
59
+ end
@@ -1,265 +1,53 @@
1
- module Eulim
2
- module Chemistry
3
- # This class has functionality for elements
4
- # Ex: symbol, atomic mass, atomic number
5
- class Element
6
- VALID_SYMBOL_REGEXP = /[A-Z][a-z]{0,2}/
7
- VALID_NAME_REGEXP = /[A-Z][a-z]+/
8
-
9
- attr_accessor :atomic_mass, :symbol, :name, :atomic_number
10
-
11
- @@attrs = %w[symbol name atomic_number atomic_mass]
12
-
13
- def initialize(args)
14
- return nil unless args
15
- args.each do |k, v|
16
- instance_variable_set("@#{k}", v) unless v.nil?
17
- end
18
- end
19
-
20
- # Data taken from 'www.science.co.il/elements/'
21
- @@elements = [
22
- { symbol: 'H', name: 'Hydrogen',
23
- atomic_number: 1, atomic_mass: 1.0079 },
24
- { symbol: 'He', name: 'Helium',
25
- atomic_number: 2, atomic_mass: 4.002602 },
26
- { symbol: 'Li', name: 'Lithium',
27
- atomic_number: 3, atomic_mass: 6.941 },
28
- { symbol: 'Be', name: 'Beryllium',
29
- atomic_number: 4, atomic_mass: 9.012182 },
30
- { symbol: 'B', name: 'Boron',
31
- atomic_number: 5, atomic_mass: 10.811 },
32
- { symbol: 'C', name: 'Carbon',
33
- atomic_number: 6, atomic_mass: 12.0107 },
34
- { symbol: 'N', name: 'Nitrogen',
35
- atomic_number: 7, atomic_mass: 14.0067 },
36
- { symbol: 'O', name: 'Oxygen',
37
- atomic_number: 8, atomic_mass: 15.9996 },
38
- { symbol: 'F', name: 'Fluorine',
39
- atomic_number: 9, atomic_mass: 18.9984 },
40
- { symbol: 'Ne', name: 'Neon',
41
- atomic_number: 10, atomic_mass: 20.1797 },
42
- { symbol: 'Na', name: 'Sodium',
43
- atomic_number: 11, atomic_mass: 22.9897 },
44
- { symbol: 'Mg', name: 'Magnesium',
45
- atomic_number: 12, atomic_mass: 24.305 },
46
- { symbol: 'Al', name: 'Aluminium',
47
- atomic_number: 13, atomic_mass: 26.9815 },
48
- { symbol: 'Si', name: 'Silicon',
49
- atomic_number: 14, atomic_mass: 28.0855 },
50
- { symbol: 'P', name: 'Phosphorus',
51
- atomic_number: 15, atomic_mass: 30.9738 },
52
- { symbol: 'S', name: 'Sulfur',
53
- atomic_number: 16, atomic_mass: 32.065 },
54
- { symbol: 'Cl', name: 'Chlorine',
55
- atomic_number: 17, atomic_mass: 35.453 },
56
- { symbol: 'Ar', name: 'Argon',
57
- atomic_number: 18, atomic_mass: 39.948 },
58
- { symbol: 'K', name: 'Potassium',
59
- atomic_number: 19, atomic_mass: 39.0983 },
60
- { symbol: 'Ca', name: 'Calcium',
61
- atomic_number: 20, atomic_mass: 40.078 },
62
- { symbol: 'Sc', name: 'Scandium',
63
- atomic_number: 21, atomic_mass: 44.9559 },
64
- { symbol: 'Ti', name: 'Titanium',
65
- atomic_number: 22, atomic_mass: 47.867 },
66
- { symbol: 'V', name: 'Vanadium',
67
- atomic_number: 23, atomic_mass: 50.9415 },
68
- { symbol: 'Cr', name: 'Chromium',
69
- atomic_number: 24, atomic_mass: 51.9961 },
70
- { symbol: 'Mn', name: 'Manganese',
71
- atomic_number: 25, atomic_mass: 54.938 },
72
- { symbol: 'Fe', name: 'Iron',
73
- atomic_number: 26, atomic_mass: 55.845 },
74
- { symbol: 'Co', name: 'Cobalt',
75
- atomic_number: 27, atomic_mass: 58.9332 },
76
- { symbol: 'Ni', name: 'Nickel',
77
- atomic_number: 28, atomic_mass: 58.6934 },
78
- { symbol: 'Cu', name: 'Copper',
79
- atomic_number: 29, atomic_mass: 63.546 },
80
- { symbol: 'Zn', name: 'Zinc',
81
- atomic_number: 30, atomic_mass: 65.39 },
82
- { symbol: 'Ga', name: 'Gallium',
83
- atomic_number: 31, atomic_mass: 69.723 },
84
- { symbol: 'Ge', name: 'Germanium',
85
- atomic_number: 32, atomic_mass: 72.64 },
86
- { symbol: 'As', name: 'Arsenic',
87
- atomic_number: 33, atomic_mass: 74.9216 },
88
- { symbol: 'Se', name: 'Selenium',
89
- atomic_number: 34, atomic_mass: 78.96 },
90
- { symbol: 'Br', name: 'Bromine',
91
- atomic_number: 35, atomic_mass: 79.904 },
92
- { symbol: 'Kr', name: 'Krypton',
93
- atomic_number: 36, atomic_mass: 83.3 },
94
- { symbol: 'Rb', name: 'Rubidium',
95
- atomic_number: 37, atomic_mass: 85.4678 },
96
- { symbol: 'Sr', name: 'Strontium',
97
- atomic_number: 38, atomic_mass: 87.62 },
98
- { symbol: 'Y', name: 'Yttrium',
99
- atomic_number: 39, atomic_mass: 88.9059 },
100
- { symbol: 'Zr', name: 'Zirconium',
101
- atomic_number: 40, atomic_mass: 91.224 },
102
- { symbol: 'Nb', name: 'Niobium',
103
- atomic_number: 41, atomic_mass: 92.9064 },
104
- { symbol: 'Mo', name: 'Molybdenum',
105
- atomic_number: 42, atomic_mass: 95.94 },
106
- { symbol: 'Tc', name: 'Technitium',
107
- atomic_number: 43, atomic_mass: 98 },
108
- { symbol: 'Ru', name: 'Ruthenium',
109
- atomic_number: 44, atomic_mass: 101.07 },
110
- { symbol: 'Rh', name: 'Rhodium',
111
- atomic_number: 45, atomic_mass: 102.9055 },
112
- { symbol: 'Pd', name: 'Palladium',
113
- atomic_number: 46, atomic_mass: 106.42 },
114
- { symbol: 'Ag', name: 'Silver',
115
- atomic_number: 47, atomic_mass: 107.8682 },
116
- { symbol: 'Cd', name: 'Cadmiuim',
117
- atomic_number: 48, atomic_mass: 112.411 },
118
- { symbol: 'In', name: 'Indium',
119
- atomic_number: 49, atomic_mass: 114.818 },
120
- { symbol: 'Sn', name: 'Tin',
121
- atomic_number: 50, atomic_mass: 118.71 },
122
- { symbol: 'Sb', name: 'Antimony',
123
- atomic_number: 51, atomic_mass: 121.76 },
124
- { symbol: 'Te', name: 'Tellurium',
125
- atomic_number: 52, atomic_mass: 127.6 },
126
- { symbol: 'I', name: 'Iodine',
127
- atomic_number: 53, atomic_mass: 126.9045 },
128
- { symbol: 'Xe', name: 'Xenon',
129
- atomic_number: 54, atomic_mass: 131.293 },
130
- { symbol: 'Cs', name: 'Cesium',
131
- atomic_number: 55, atomic_mass: 132.9055 },
132
- { symbol: 'Ba', name: 'Barium',
133
- atomic_number: 56, atomic_mass: 137.327 },
134
- { symbol: 'La', name: 'Lanthanum',
135
- atomic_number: 57, atomic_mass: 138.9055 },
136
- { symbol: 'Ce', name: 'Cerium',
137
- atomic_number: 58, atomic_mass: 140.116 },
138
- { symbol: 'Pr', name: 'Praseodymium',
139
- atomic_number: 59, atomic_mass: 140.9077 },
140
- { symbol: 'Nd', name: 'Neodymium',
141
- atomic_number: 60, atomic_mass: 144.24 },
142
- { symbol: 'Pm', name: 'Promethium',
143
- atomic_number: 61, atomic_mass: 145 },
144
- { symbol: 'Sm', name: 'Samarium',
145
- atomic_number: 62, atomic_mass: 150.36 },
146
- { symbol: 'Eu', name: 'Europium',
147
- atomic_number: 63, atomic_mass: 151.964 },
148
- { symbol: 'Gd', name: 'Gadolium',
149
- atomic_number: 64, atomic_mass: 157.25 },
150
- { symbol: 'Tb', name: 'Terbium',
151
- atomic_number: 65, atomic_mass: 158.9253 },
152
- { symbol: 'Dy', name: 'Dysprosium',
153
- atomic_number: 66, atomic_mass: 162.5 },
154
- { symbol: 'Ho', name: 'Holmium',
155
- atomic_number: 67, atomic_mass: 164.9303 },
156
- { symbol: 'Er', name: 'Erbium',
157
- atomic_number: 68, atomic_mass: 167.259 },
158
- { symbol: 'Tm', name: 'Thulium',
159
- atomic_number: 69, atomic_mass: 168.9342 },
160
- { symbol: 'Yb', name: 'Ytterbium',
161
- atomic_number: 70, atomic_mass: 173.04 },
162
- { symbol: 'Lu', name: 'Lutetium',
163
- atomic_number: 71, atomic_mass: 174.967 },
164
- { symbol: 'Hf', name: 'Hafnium',
165
- atomic_number: 72, atomic_mass: 178.49 },
166
- { symbol: 'Ta', name: 'Tantalum',
167
- atomic_number: 73, atomic_mass: 180.9479 },
168
- { symbol: 'W', name: 'Tungsten',
169
- atomic_number: 74, atomic_mass: 183.84 },
170
- { symbol: 'Re', name: 'Rhenium',
171
- atomic_number: 75, atomic_mass: 186.207 },
172
- { symbol: 'Os', name: 'Osmium',
173
- atomic_number: 76, atomic_mass: 190.23 },
174
- { symbol: 'Ir', name: 'Iridium',
175
- atomic_number: 77, atomic_mass: 192.217 },
176
- { symbol: 'Pt', name: 'Platinum',
177
- atomic_number: 78, atomic_mass: 195.078 },
178
- { symbol: 'Au', name: 'Gold',
179
- atomic_number: 79, atomic_mass: 196.9665 },
180
- { symbol: 'Hg', name: 'Mercury',
181
- atomic_number: 80, atomic_mass: 200.59 },
182
- { symbol: 'Tl', name: 'Thallium',
183
- atomic_number: 81, atomic_mass: 204.3833 },
184
- { symbol: 'Pb', name: 'Lead',
185
- atomic_number: 82, atomic_mass: 207.2 },
186
- { symbol: 'Bi', name: 'Bismuth',
187
- atomic_number: 83, atomic_mass: 208.9804 },
188
- { symbol: 'Po', name: 'Polonium',
189
- atomic_number: 84, atomic_mass: 209 },
190
- { symbol: 'At', name: 'Astatine',
191
- atomic_number: 85, atomic_mass: 210 },
192
- { symbol: 'Rn', name: 'Radon',
193
- atomic_number: 86, atomic_mass: 222 },
194
- { symbol: 'Fr', name: 'Francium',
195
- atomic_number: 87, atomic_mass: 223 },
196
- { symbol: 'Ra', name: 'Radium',
197
- atomic_number: 88, atomic_mass: 226 },
198
- { symbol: 'Ac', name: 'Actinium',
199
- atomic_number: 89, atomic_mass: 227 },
200
- { symbol: 'Th', name: 'Thorium',
201
- atomic_number: 90, atomic_mass: 232.0381 },
202
- { symbol: 'Pa', name: 'Protactinium',
203
- atomic_number: 91, atomic_mass: 231.0359 },
204
- { symbol: 'U', name: 'Uranium',
205
- atomic_number: 92, atomic_mass: 238.0289 },
206
- { symbol: 'Np', name: 'Neptunum',
207
- atomic_number: 93, atomic_mass: 237 },
208
- { symbol: 'Pu', name: 'Plutonium',
209
- atomic_number: 94, atomic_mass: 244 },
210
- { symbol: 'Am', name: 'Americium',
211
- atomic_number: 95, atomic_mass: 243 },
212
- { symbol: 'Cm', name: 'Curium',
213
- atomic_number: 96, atomic_mass: 247 },
214
- { symbol: 'Bk', name: 'Berkilium',
215
- atomic_number: 97, atomic_mass: 247 },
216
- { symbol: 'Cf', name: 'Californium',
217
- atomic_number: 98, atomic_mass: 251 },
218
- { symbol: 'Es', name: 'Einsteinium',
219
- atomic_number: 99, atomic_mass: 252 },
220
- { symbol: 'Fm', name: 'Fermium',
221
- atomic_number: 100, atomic_mass: 257 },
222
- { symbol: 'Md', name: 'Mendelevium',
223
- atomic_number: 101, atomic_mass: 258 },
224
- { symbol: 'No', name: 'Nobelium',
225
- atomic_number: 102, atomic_mass: 259 },
226
- { symbol: 'Lr', name: 'Lawrencium',
227
- atomic_number: 103, atomic_mass: 262 },
228
- { symbol: 'Rf', name: 'Rutherfordium',
229
- atomic_number: 104, atomic_mass: 261 },
230
- { symbol: 'Db', name: 'Dubnium',
231
- atomic_number: 105, atomic_mass: 262 },
232
- { symbol: 'Sg', name: 'Seaborgium',
233
- atomic_number: 106, atomic_mass: 266 },
234
- { symbol: 'Bh', name: 'Bohrium',
235
- atomic_number: 107, atomic_mass: 264 },
236
- { symbol: 'Hs', name: 'Hassium',
237
- atomic_number: 108, atomic_mass: 277 },
238
- { symbol: 'Mt', name: 'Meitnerium',
239
- atomic_number: 109, atomic_mass: 268 }
240
- ]
241
-
242
- private_class_method def self.method_missing(m, *args)
243
- attribute = m.to_s.split('get_by_').last
244
- valid_method? m, attribute
245
- args[0] = attribute == 'name' ? args[0].capitalize : args[0]
246
- element_data = get_element_by_attribute attribute, args[0]
247
- raise 'Element not found' unless element_data
248
- new(element_data)
249
- end
250
-
251
- private_class_method def self.get_element_by_attribute(attribute, value)
252
- @@elements.select do |element|
253
- element[attribute.to_sym] == value
254
- end.first
255
- end
256
-
257
- private_class_method def self.valid_method?(m, attribute)
258
- super unless m.to_s.start_with?('get_by_')
259
- raise(NameError, 'Invalid attribute') unless @@attrs.include? attribute
260
- end
261
-
262
- private_class_method :new
263
- end
264
- end
265
- end
1
+ require 'csv'
2
+
3
+ module Eulim
4
+ module Chemistry
5
+ # This class has functionality for elements
6
+ # Ex: symbol, atomic mass, atomic number
7
+ class Element
8
+ attr_accessor :atomic_mass, :symbol, :name, :atomic_number
9
+
10
+ ATTRS = %w[symbol name atomic_number atomic_mass].freeze
11
+ # Data taken from 'www.science.co.il/elements/'
12
+ ELEMENTS = []
13
+
14
+ def initialize(arg)
15
+ @name = arg[1]
16
+ @symbol = arg[0]
17
+ @atomic_number = arg[2].to_i
18
+ @atomic_mass = arg[3].to_f
19
+ end
20
+
21
+ # elements of the csv have no spaces
22
+ CSV.foreach(File.join(File.dirname(__FILE__), 'elements.csv'), headers: true) do |row|
23
+ ELEMENTS << new(row)
24
+ end
25
+
26
+ private_class_method def self.method_missing(m, *args)
27
+ attribute = m.to_s.split('get_by_').last
28
+ valid_method? m, attribute
29
+ raise 'please give argument' if args.empty?
30
+ args[0] = attribute == 'name' ? args[0].capitalize : args[0]
31
+ element_data = get_element_by_attribute attribute, args[0]
32
+ raise 'Element not found' unless element_data
33
+ element_data
34
+ end
35
+
36
+ private_class_method def self.get_element_by_attribute(attribute, value)
37
+ ELEMENTS.each do |element|
38
+ # elements value of attribute == value give by user
39
+ return element if
40
+ element.instance_variable_get(('@' + attribute).intern) == value
41
+ end
42
+ raise(NameError, 'element not found')
43
+ end
44
+
45
+ private_class_method def self.valid_method?(m, attribute)
46
+ super unless m.to_s.start_with?('get_by_')
47
+ raise(NameError, 'Invalid attribute') unless ATTRS.include? attribute
48
+ end
49
+
50
+ private_class_method :new
51
+ end
52
+ end
53
+ end