chemistry_rails 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/chemistry_rails.rb +202 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4a79081d178f3e7b710614a8e73e1e4f62df6ad9
4
+ data.tar.gz: 101cd7f76d9508bb492d80efce22b8eb3cd84db6
5
+ SHA512:
6
+ metadata.gz: 363fabff21cd9fd3ba6a1497eb4f12dacbd7b2fc3f8e3cc7c8bcc61855f55c1daefb08d4de5e62f00812ca0d1e29f4d386c4049e03c19f389d46a32510cd1683
7
+ data.tar.gz: facee64d81a62ff78d10db5e788ad95ec00a56563255be4184055b314e4756e6cc711058cbe999ff92c32d306a900a4cf3286f4fd5a8b4e5e8f63d44514ae51e
@@ -0,0 +1,202 @@
1
+ module ChemistryRails
2
+ ELEMENT_CATEGORIES = ['Alkali Metal', 'Alkaline Earth', 'Transition Metal', 'Basic Metal', 'Semimetal', 'Nonmetal', 'Halogen', 'Noble gas', 'Lanthanide', 'Actinide']
3
+
4
+ ELEMENTS = [
5
+ nil,
6
+ { short: 'H', long: 'Hydrogen', mass: 1.0079, category: 5 },
7
+ { short: 'He', long: 'Helium', mass: 4.00260, category: 7 },
8
+ { short: 'Li', long: 'Lithium', mass: 6.941, category: 0 },
9
+ { short: 'Be', long: 'Beryllium', mass: 9.01218, category: 1 },
10
+ { short: 'B', long: 'Boron', mass: 10.811, category: 4 },
11
+ { short: 'C', long: 'Carbon', mass: 12.011, category: 5 },
12
+ { short: 'N', long: 'Nitrogen', mass: 14.00674, category: 5 },
13
+ { short: 'O', long: 'Oxygen', mass: 15.9994, category: 5 },
14
+ { short: 'F', long: 'Fluorine', mass: 18.998403, category: 6 },
15
+ { short: 'Ne', long: 'Neon', mass: 20.1797, category: 7 }, #10
16
+ { short: 'Na', long: 'Sodium', mass: 22.989768, category: 0 },
17
+ { short: 'Mg', long: 'Magnesium', mass: 24.305, category: 1 },
18
+ { short: 'Al', long: 'Aluminium', mass: 26.981539, category: 3 },
19
+ { short: 'Si', long: 'Silicon', mass: 28.0855, category: 4 },
20
+ { short: 'P', long: 'Phosphorus', mass: 30.973762, category: 5 },
21
+ { short: 'S', long: 'Sulfur', mass: 32.066, category: 5 },
22
+ { short: 'Cl', long: 'Chlorine', mass: 35.4527, category: 6 },
23
+ { short: 'Ar', long: 'Argon', mass: 39.948, category: 7 },
24
+ { short: 'K', long: 'Potassium', mass: 39.0983, category: 0 },
25
+ { short: 'Ca', long: 'Calcium', mass: 40.078, category: 1 }, #20
26
+ { short: 'Sc', long: 'Scandium', mass: 44.95591, category: 2 },
27
+ { short: 'Ti', long: 'Titanium', mass: 47.88, category: 2 },
28
+ { short: 'V', long: 'Venadium', mass: 50.9415, category: 2 },
29
+ { short: 'Cr', long: 'Chromium', mass: 51.9961, category: 2 },
30
+ { short: 'Mn', long: 'Manganese', mass: 54.938, category: 2 },
31
+ { short: 'Fe', long: 'Iron', mass: 54.847, category: 2 },
32
+ { short: 'Co', long: 'Cobalt', mass: 58.9332, category: 2 },
33
+ { short: 'Ni', long: 'Nickel', mass: 58.6934, category: 2 },
34
+ { short: 'Cu', long: 'Copper', mass: 63.546, category: 2 },
35
+ { short: 'Zn', long: 'Zinc', mass: 65.39, category: 2 }, #30
36
+ { short: 'Ga', long: 'Gallium', mass: 69.732, category: 3 },
37
+ { short: 'Ge', long: 'Germanium', mass: 72.64, category: 4 },
38
+ { short: 'As', long: 'Arsenic', mass: 74.92159, category: 4 },
39
+ { short: 'Se', long: 'Selenium', mass: 78.96, category: 5 },
40
+ { short: 'Br', long: 'Bromine', mass: 79.904, category: 6 },
41
+ { short: 'Kr', long: 'Krypton', mass: 83.80, category: 7 },
42
+ { short: 'Rb', long: 'Rubidium', mass: 85.4678, category: 0 },
43
+ { short: 'Sr', long: 'Strontium', mass: 87.62, category: 1 },
44
+ { short: 'Y', long: 'Yttrium', mass: 88.90585, category: 2 },
45
+ { short: 'Zr', long: 'Zirconium', mass: 91.224, category: 2 }, #40
46
+ { short: 'Nb', long: 'Niobium', mass: 92.90638, category: 2 },
47
+ { short: 'Mo', long: 'Molybdenum', mass: 95.94, category: 2 },
48
+ { short: 'Tc', long: 'Technetium', mass: 98.9072, category: 2 },
49
+ { short: 'Ru', long: 'Ruthenium', mass: 101.07, category: 2 },
50
+ { short: 'Rh', long: 'Rhodium', mass: 102.9055, category: 2 },
51
+ { short: 'Pd', long: 'Palladium', mass: 106.42, category: 2 },
52
+ { short: 'Ag', long: 'Silver', mass: 107.8682, category: 2 },
53
+ { short: 'Cd', long: 'Cadmium', mass: 112.411, category: 2 },
54
+ { short: 'In', long: 'Indium', mass: 114.818, category: 3 },
55
+ { short: 'Sn', long: 'Tin', mass: 118.71, category: 3 }, #50
56
+ { short: 'Sb', long: 'Antimony', mass: 121.760, category: 4 },
57
+ { short: 'Te', long: 'Tellurium', mass: 127.6, category: 4 },
58
+ { short: 'I', long: 'Iodine', mass: 126.90447, category: 6 },
59
+ { short: 'Xe', long: 'Xenon', mass: 131.29, category: 7 },
60
+ { short: 'Cs', long: 'Cesium', mass: 132.90543, category: 0 },
61
+ { short: 'Ba', long: 'Barium', mass: 137.327, category: 1 },
62
+
63
+ { short: 'La', long: 'Lanthanum', mass: 138.9055, category: 8 },
64
+ { short: 'Ce', long: 'Cerium', mass: 140.115, category: 8 },
65
+ { short: 'Pr', long: 'Praseodymium', mass: 140.90765, category: 8 },
66
+ { short: 'Nd', long: 'Neodymium', mass: 144.24, category: 8 }, #60
67
+ { short: 'Pm', long: 'Promethium', mass: 144.9127, category: 8 },
68
+ { short: 'Sm', long: 'Samarium', mass: 150.36, category: 8 },
69
+ { short: 'Eu', long: 'Europium', mass: 151.9655, category: 8 },
70
+ { short: 'Gd', long: 'Gadolinium', mass: 157.25, category: 8 },
71
+ { short: 'Tb', long: 'Terbium', mass: 158.92534, category: 8 },
72
+ { short: 'Dy', long: 'Dysprosium', mass: 162.50, category: 8 },
73
+ { short: 'Ho', long: 'Holmium', mass: 164.93032, category: 8 },
74
+ { short: 'Er', long: 'Erbium', mass: 167.26, category: 8 },
75
+ { short: 'Tm', long: 'Thulium', mass: 168.93421, category: 8 },
76
+ { short: 'Yb', long: 'Ytterbium', mass: 173.04, category: 8 }, #70
77
+ { short: 'Lu', long: 'Lutetium', mass: 174.967, category: 8 },
78
+
79
+ { short: 'Hf', long: 'Hafnium', mass: 178.49, category: 2 },
80
+ { short: 'Ta', long: 'Tantalum', mass: 180.9479, category: 2 },
81
+ { short: 'W', long: 'Tungsten', mass: 183.85, category: 2 },
82
+ { short: 'Re', long: 'Rhenium', mass: 186.207, category: 2 },
83
+ { short: 'Os', long: 'Osmium', mass: 190.23, category: 2 },
84
+ { short: 'Ir', long: 'Iridium', mass: 192.22, category: 2 },
85
+ { short: 'Pt', long: 'Platinum', mass: 195.08, category: 2 },
86
+ { short: 'Au', long: 'Gold', mass: 196.9665, category: 2 },
87
+ { short: 'Hg', long: 'Mercury', mass: 200.59, category: 2 }, #80
88
+ { short: 'Tl', long: 'Thallium', mass: 204.3833, category: 3 },
89
+ { short: 'Pb', long: 'Lead', mass: 204.3833, category: 3 },
90
+ { short: 'Bi', long: 'Bismuth', mass: 208.98037, category: 3 },
91
+ { short: 'Po', long: 'Polonium', mass: 208.9824, category: 4 },
92
+ { short: 'At', long: 'Astatine', mass: 209.9871, category: 6 },
93
+ { short: 'Rn', long: 'Radon', mass: 222.0176, category: 7 },
94
+ { short: 'Fr', long: 'Francium', mass: 223.0197, category: 0 },
95
+ { short: 'Ra', long: 'Radium', mass: 226.0254, category: 1 },
96
+
97
+ { short: 'Ac', long: 'Actinium', mass: 227.0278, category: 9 },
98
+ { short: 'Th', long: 'Thorium', mass: 232.0381, category: 9 }, #90
99
+ { short: 'Pa', long: 'Protactinium', mass: 231.03588, category: 9 },
100
+ { short: 'U', long: 'Uranium', mass: 238.0289, category: 9 },
101
+ { short: 'Np', long: 'Neptunium', mass: 237.0482, category: 9 },
102
+ { short: 'Pu', long: 'Plutonium', mass: 244.0642, category: 9 },
103
+ { short: 'Am', long: 'Americium', mass: 243.0614, category: 9 },
104
+ { short: 'Cm', long: 'Curium', mass: 247.0703, category: 9 },
105
+ { short: 'Bk', long: 'Berkelium', mass: 247.0703, category: 9 },
106
+ { short: 'Cf', long: 'Californium', mass: 251.0796, category: 9 },
107
+ { short: 'Es', long: 'Einsteinium', mass: 254, category: 9 },
108
+ { short: 'Fm', long: 'Fermium', mass: 257.0951, category: 9 }, #100
109
+ { short: 'Md', long: 'Mendelevium', mass: 258.1, category: 9 },
110
+ { short: 'No', long: 'Nobelium', mass: 259.1009, category: 9 },
111
+ { short: 'Lr', long: 'Lawrencium', mass: 262, category: 9 },
112
+
113
+ { short: 'Rf', long: 'Rutherfordium',mass: 261, category: 2 },
114
+ { short: 'Db', long: 'Dubnium', mass: 262, category: 2 },
115
+ { short: 'Sg', long: 'Seaborgium', mass: 266, category: 2 },
116
+ { short: 'Bh', long: 'Bohrium', mass: 264, category: 2 },
117
+ { short: 'Hs', long: 'Hassium', mass: 269, category: 2 },
118
+ { short: 'Mt', long: 'Meitnerium', mass: 268, category: 2 },
119
+ { short: 'Ds', long: 'Darmstadium', mass: 269, category: 2 }, #110
120
+ { short: 'Rg', long: 'Roentgenium', mass: 272, category: 2 },
121
+ { short: 'Cn', long: 'Copernicium', mass: 277, category: 2 },
122
+ { short: 'Uut',long: 'Ununtrium', mass: 286, category: 3 },
123
+ { short: 'Fl', long: 'Flerovium', mass: 289, category: 3 },
124
+ { short: 'Uup',long: 'Ununpentium', mass: 288, category: 3 },
125
+ { short: 'Lv', long: 'Livermorium', mass: 293, category: 3 },
126
+ { short: 'Uus',long: 'Ununseptium', mass: 294, category: 6 },
127
+ { short: 'Uuo',long: 'Ununoctium', mass: 294, category: 7 }, #118
128
+ ]
129
+
130
+ class << self
131
+
132
+ def elements(category = nil)
133
+ category ? ELEMENTS.select {|e| e[:category] == category} : ELEMENTS
134
+ end
135
+
136
+ def element(short)
137
+ ELEMENTS.select{|e| e && e[:short] == short}.first
138
+ end
139
+
140
+ def alkali_metals; elements(0); end
141
+ def alkaline_earth; elements(1); end
142
+ def transition_metals;elements(2); end
143
+ def basic_metals; elements(3); end
144
+ def semi_metals; elements(4); end
145
+ def non_metals; elements(5); end
146
+ def halogens; elements(6); end
147
+ def noble_gases; elements(7); end
148
+ def lanthanides; elements(8); end
149
+ def actinides; elements(9); end
150
+
151
+ end
152
+
153
+ end
154
+
155
+ module ChemistryRails
156
+ class Railtie < Rails::Railtie
157
+
158
+ initializer "chemistry_rails.active_record" do
159
+ ActiveSupport.on_load :active_record do
160
+ require 'chemistry_rails/orm/activerecord'
161
+ end
162
+ end
163
+
164
+ ##
165
+ # Loads the locale files before the Rails application locales
166
+ # letting the Rails application override the locale defaults
167
+ config.before_configuration do
168
+ I18n.load_path << File.join(File.dirname(__FILE__), "chemistry_rails", "locale", 'en.yml')
169
+ end
170
+ end
171
+ end
172
+
173
+ module ChemistryRails
174
+ class Formula
175
+
176
+ attr_accessor :formula, :elements
177
+
178
+ def initialize(formula, options={})
179
+ @formula = formula
180
+ @options = options
181
+ @elements = Hash[formula.scan(/([A-Z][a-z]{0,2})(\d*)/).map{|k,v| [k, v.blank? ? 1 : v.to_i]}]
182
+ end
183
+
184
+ def to_s; formula; end
185
+ def empty?; formula.blank?; end
186
+
187
+ def to_html
188
+ elements.map { |el, i| "#{el}<sub>#{i > 1 ? i : ''}</sub>" }.join('').html_safe
189
+ end
190
+
191
+ def elemental_analysis(include_oxygen = false)
192
+ mass = elements.map { |el, i| ChemistryRails.element(el)[:mass] * i }.sum
193
+
194
+ Hash[
195
+ elements.map { |el, i|
196
+ [ el, (ChemistryRails.element(el)[:mass] * i / mass * 100).round(2) ]
197
+ }
198
+ ]
199
+ end
200
+
201
+ end
202
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chemistry_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Radil Radenkov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A gem for validating chemical formula and calculating elemental analysis
14
+ email: radil@chem.io
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/chemistry_rails.rb
20
+ homepage: http://rubygems.org/gems/chemistry_rails
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Chemistry rails
44
+ test_files: []