eulim 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a138013833764d873ab40e70b3c4b340bb8a828
4
- data.tar.gz: 882d5ef4120fdc98084134a15292bfc8dc89acb0
3
+ metadata.gz: 9ae4a069d424238d03ee05cdf5261aecc11c4c64
4
+ data.tar.gz: 5ec6e25efaab183bd4d7cb2a9622d6f691b9004c
5
5
  SHA512:
6
- metadata.gz: 9fea25b3c5147273c8d9b66b9ccd2edbebeae7a2a39c08d2c3712229e96c5bf6aef58082a306094eb87d148ed32a304025017b628a42c3969c0b83d45b8583c2
7
- data.tar.gz: ebb53f057e21a6f359e7954b56bce2d7a706481097d4fd3e38108210fc8e7d406d57406219287182a6e0223b5ba9bdb51430ea6d2d62c49e1c3a2f2e504b9156
6
+ metadata.gz: 09d19cc17e2be7ab42ae9e29924f241dc286caae6496bc7b09a71db65a7ba8d05915677f60603c6bbfab5b9822363ef23a80cdf0e43dfba2038ecfe896d4d843
7
+ data.tar.gz: 7c655642b401042d157b99e4a6ea904eada04f2469bfd7a236f4642ff405bbeb37b7f29e2253719825013d93f66d7215674e2a3a4ebabd7ebb36e0186ff83f38
data/README.md CHANGED
@@ -32,28 +32,27 @@ Or install it yourself as:
32
32
  #### Element
33
33
 
34
34
  * H, He, Li, Be, B, C, N, O, F, Ne, Na, Mg, Al, Si, P, S, Cl, Ar, K , Ca, Sc, Ti, V, Cr, Mn, Fe, Co, Ni
35
- * symbol, name, atomic_number, atomic_mass
35
+ * attributes: symbol, name, atomic_number, atomic_mass
36
+ * methods: get_by_* (* can be any of the attributes)
36
37
 
37
38
  #### Compound
38
39
 
39
- * molecular_mass
40
+ * attributes: formula, molecular_mass, constituents
41
+ * methods: new(compound formula)
40
42
 
41
43
  ## Usage
42
44
 
43
- $ Eulim::Chemistry::Element.get_by_symbol("H")
44
- => #<Eulim::Chemistry::Element: @symbol="H", @name="Hydrogen", @atomic_number=1, @atomic_mass=1.008>
45
+ $ Eulim::Chemistry::Element.get_by_symbol "H"
46
+ => #<Eulim::Chemistry::Element: @symbol="H", @name="Hydrogen", @atomic_number=1, @atomic_mass=1.0079>
45
47
 
46
- $ Eulim::Chemistry::Element.get_by_atomic_number(12)
48
+ $ Eulim::Chemistry::Element.get_by_atomic_number 12
47
49
  => #<Eulim::Chemistry::Element: @symbol="Mg", @name="Magnesium", @atomic_number=12, @atomic_mass=24.305>
48
50
 
49
- $ Eulim::Chemistry::Compound.new("CaCO3")
50
- => #<Eulim::Chemistry::Compound: @formula="CaCO3">
51
-
52
- $ Eulim::Chemistry::Compound.new("CaCO3").elements
53
- => [#<Eulim::Chemistry::Element: @symbol="Ca", @name="Calcium", @atomic_number=20, @atomic_mass=40.078>, #<Eulim::Chemistry::Element: @symbol="C", @name="Carbon", @atomic_number=6, @atomic_mass=12.0107>, #<Eulim::Chemistry::Element: @symbol="O", @name="Oxygen", @atomic_number=8, @atomic_mass=15.9996>]
51
+ $ Eulim::Chemistry::Element.get_by_name "helium" # or "Helium"
52
+ => #<Eulim::Chemistry::Element: @symbol="He", @name="Helium", @atomic_number=2, @atomic_mass=4.002602>
54
53
 
55
- $ Eulim::Chemistry::Compound.new("Be3Al2(SiO3)6").molecular_mass
56
- => 537.505346
54
+ $ Eulim::Chemistry::Compound.new("CaCO3")
55
+ => #<Eulim::Chemistry::Compound: @formula="CaCO3", @constituents=[{:element=>#<Eulim::Chemistry::Element: @symbol="Ca", @name="Calcium", @atomic_number=20, @atomic_mass=40.078>, :atom_count=>1}, {:element=>#<Eulim::Chemistry::Element: @symbol="C", @name="Carbon", @atomic_number=6, @atomic_mass=12.0107>, :atom_count=>1}, {:element=>#<Eulim::Chemistry::Element: @symbol="O", @name="Oxygen", @atomic_number=8, @atomic_mass=15.9996>, :atom_count=>3}], @molecular_mass=100.0875>
57
56
 
58
57
  ## Development
59
58
 
@@ -2,63 +2,51 @@ module Eulim::Chemistry
2
2
  class Compound
3
3
 
4
4
  VALID_COMPOUND_REGEXP = /[A-Z][a-z]{0,2}\d*|\((?:[^()]*(?:\(.*\))?[^()]*)+\)\d*/
5
- VAILD_ELEMENT_REGEXP = /[A-Z][a-z]{0,2}\d*/
6
5
 
6
+ attr_accessor :molecular_mass, :constituents, :formula
7
+
7
8
  def initialize(arg)
8
9
  @formula = arg
10
+ @constituents = get_constituents
11
+ @molecular_mass = get_molecular_mass
9
12
  end
10
13
 
11
- def molecular_mass
12
- mass = 0
13
- element_atom_count = get_element_atom_count
14
- element_atom_count.each do |symbol, count|
15
- mass += Element.get_by_symbol(symbol).atomic_mass * count
14
+ private
15
+ def get_molecular_mass
16
+ mass = 0
17
+ @constituents.each do |constituent|
18
+ mass += constituent[:element].atomic_mass * constituent[:atom_count]
19
+ end
20
+ mass
16
21
  end
17
- mass
18
- end
19
22
 
20
- def elements
21
- elements = []
22
- element_atom_count = get_element_atom_count
23
- element_atom_count.each do |symbol, count|
24
- elements << Element.get_by_symbol(symbol)
23
+ def get_constituents
24
+ constituents = []
25
+ get_constituent_atoms.each do |symbol, count|
26
+ constituents << {element: Element.get_by_symbol(symbol), atom_count: count}
27
+ end
28
+ constituents
25
29
  end
26
- elements
27
- end
28
30
 
29
- private
30
- def get_element_atom_count formula = @formula, group_multiplier = 1, result = {}, groups = []
31
- formula
32
- elements_n_groups = formula.scan VALID_COMPOUND_REGEXP
33
- loop do
34
- break if !elements_n_groups.last
35
- elements = elements_n_groups.last.scan VAILD_ELEMENT_REGEXP
36
- if elements.first === elements_n_groups.last
37
- element_multipler = elements.first.match(/\d*$/).to_a.first.to_i
38
- element_multipler = element_multipler > 0 ? element_multipler : 1
39
- element_symbol = elements.first.match(/[A-Z][a-z]{0,2}/).to_a.first
40
- if result[element_symbol]
41
- result[element_symbol] += (element_multipler * group_multiplier)
42
- else
43
- result[element_symbol] = (element_multipler * group_multiplier)
44
- end
31
+ def get_constituent_atoms formula=@formula, result={}
32
+ constituents = formula.scan VALID_COMPOUND_REGEXP
33
+ constituents.each do |constituent|
34
+ multipler = get_multipler constituent
35
+ if constituent[0] != '(' && multipler == 0
36
+ result[constituent] = result[constituent] ? result[constituent] + 1 : 1
45
37
  else
46
- groups << elements_n_groups.last
47
- end
48
- elements_n_groups.pop
49
- end
50
- loop do
51
- break if !groups.last
52
- group = groups.pop
53
- grml = get_group_multiplier(group)
54
- get_element_atom_count group[1..-(grml.to_s.length+1)], grml*group_multiplier, result, groups
38
+ (multipler == 0 ? 1 : multipler).times do
39
+ sub_constituents = constituent.match(/^\(?(.*?)\)?($|\d*$)/).to_a
40
+ idx = constituent == sub_constituents.first ? 1 : 0
41
+ get_constituent_atoms sub_constituents[idx], result
42
+ end
43
+ end
55
44
  end
56
45
  result
57
46
  end
58
47
 
59
- def get_group_multiplier formula
60
- group_multiplier = formula.scan(VALID_COMPOUND_REGEXP).first.match(/\d*$/).to_a.first.to_i
61
- group_multiplier > 0 ? group_multiplier : 1
48
+ def get_multipler constituent
49
+ multipler = constituent.match(/\d*$/).to_a.first.to_i
62
50
  end
63
51
 
64
52
  end
@@ -4,60 +4,72 @@ module Eulim::Chemistry
4
4
  VALID_SYMBOL_REGEXP = /[A-Z][a-z]{0,2}/
5
5
  VALID_NAME_REGEXP = /[A-Z][a-z]+/
6
6
 
7
- attr_accessor :atomic_mass
7
+ attr_accessor :atomic_mass, :symbol, :name, :atomic_number
8
8
 
9
+ @@attributes = ['symbol', 'name', 'atomic_number', 'atomic_mass']
9
10
  @@elements = [
10
- { symbol:'H', name: 'Hydrogen', atomic_number: 1, atomic_mass: 1.0079 },
11
- { symbol:'He', name: 'Helium', atomic_number: 2, atomic_mass: 4.002602 },
12
- { symbol:'Li', name: 'Lithium', atomic_number: 3, atomic_mass: 6.941 },
13
- { symbol:'Be', name: 'Beryllium', atomic_number: 4, atomic_mass: 9.012182 },
14
- { symbol:'B', name: 'Boron', atomic_number: 5, atomic_mass: 10.811 },
15
- { symbol:'C', name: 'Carbon', atomic_number: 6, atomic_mass: 12.0107 },
16
- { symbol:'N', name: 'Nitrogen', atomic_number: 7, atomic_mass: 14.0067 },
17
- { symbol:'O', name: 'Oxygen', atomic_number: 8, atomic_mass: 15.9996 },
18
- { symbol:'F', name: 'Fluorine', atomic_number: 9, atomic_mass: 18.9984 },
19
- { symbol:'Ne', name: 'Neon', atomic_number: 10, atomic_mass: 20.1797 },
11
+ { symbol:'H', name: 'Hydrogen', atomic_number: 1, atomic_mass: 1.0079 },
12
+ { symbol:'He', name: 'Helium', atomic_number: 2, atomic_mass: 4.002602 },
13
+ { symbol:'Li', name: 'Lithium', atomic_number: 3, atomic_mass: 6.941 },
14
+ { symbol:'Be', name: 'Beryllium', atomic_number: 4, atomic_mass: 9.012182 },
15
+ { symbol:'B', name: 'Boron', atomic_number: 5, atomic_mass: 10.811 },
16
+ { symbol:'C', name: 'Carbon', atomic_number: 6, atomic_mass: 12.0107 },
17
+ { symbol:'N', name: 'Nitrogen', atomic_number: 7, atomic_mass: 14.0067 },
18
+ { symbol:'O', name: 'Oxygen', atomic_number: 8, atomic_mass: 15.9996 },
19
+ { symbol:'F', name: 'Fluorine', atomic_number: 9, atomic_mass: 18.9984 },
20
+ { symbol:'Ne', name: 'Neon', atomic_number: 10, atomic_mass: 20.1797 },
20
21
  { symbol:'Na', name: 'Sodium', atomic_number: 11, atomic_mass: 22.9897 },
21
- { symbol:'Mg', name: 'Magnesium', atomic_number: 12, atomic_mass: 24.305 },
22
- { symbol:'Al', name: 'Aluminium', atomic_number: 13, atomic_mass: 26.9815 },
22
+ { symbol:'Mg', name: 'Magnesium', atomic_number: 12, atomic_mass: 24.305 },
23
+ { symbol:'Al', name: 'Aluminium', atomic_number: 13, atomic_mass: 26.9815 },
23
24
  { symbol:'Si', name: 'Silicon', atomic_number: 14, atomic_mass: 28.0855 },
24
- { symbol:'P', name: 'Phosphorus', atomic_number: 15, atomic_mass: 30.9738 },
25
- { symbol:'S', name: 'Sulfur', atomic_number: 16, atomic_mass: 32.065 },
25
+ { symbol:'P', name: 'Phosphorus', atomic_number: 15, atomic_mass: 30.9738 },
26
+ { symbol:'S', name: 'Sulfur', atomic_number: 16, atomic_mass: 32.065 },
26
27
  { symbol:'Cl', name: 'Chlorine', atomic_number: 17, atomic_mass: 35.453 },
27
28
  { symbol:'Ar', name: 'Argon', atomic_number: 18, atomic_mass: 39.948 },
28
- { symbol:'K', name: 'Potassium', atomic_number: 19, atomic_mass: 39.0983 },
29
+ { symbol:'K', name: 'Potassium', atomic_number: 19, atomic_mass: 39.0983 },
29
30
  { symbol:'Ca', name: 'Calcium', atomic_number: 20, atomic_mass: 40.078 },
30
31
  { symbol:'Sc', name: 'Scandium', atomic_number: 21, atomic_mass: 44.9559 },
31
32
  { symbol:'Ti', name: 'Titanium', atomic_number: 22, atomic_mass: 47.867 },
32
- { symbol:'V', name: 'Vanadium', atomic_number: 23, atomic_mass: 50.9415 },
33
+ { symbol:'V', name: 'Vanadium', atomic_number: 23, atomic_mass: 50.9415 },
33
34
  { symbol:'Cr', name: 'Chromium', atomic_number: 24, atomic_mass: 51.9961 },
34
- { symbol:'Mn', name: 'Manganese', atomic_number: 25, atomic_mass: 54.938 },
35
- { symbol:'Fe', name: 'Iron', atomic_number: 26, atomic_mass: 55.845 },
35
+ { symbol:'Mn', name: 'Manganese', atomic_number: 25, atomic_mass: 54.938 },
36
+ { symbol:'Fe', name: 'Iron', atomic_number: 26, atomic_mass: 55.845 },
36
37
  { symbol:'Co', name: 'Cobalt', atomic_number: 27, atomic_mass: 58.9332 },
37
38
  { symbol:'Ni', name: 'Nickel', atomic_number: 28, atomic_mass: 58.6934 }
38
39
  ]
39
40
 
40
- def initialize args
41
- if args
42
- args.each do |k,v|
43
- instance_variable_set("@#{k}", v) unless v.nil?
41
+ private
42
+ def self.method_missing m, *args
43
+ if m.to_s.start_with?("get_by_")
44
+ attribute = m.to_s.split("get_by_").last
45
+ if @@attributes.include? attribute
46
+ args[0] = attribute == 'name' ? args[0].capitalize : args[0]
47
+ element_data = self.all.select { |element| element[attribute.to_sym] === args[0]}.first
48
+ if element_data
49
+ new(element_data)
50
+ else
51
+ raise "Element not found"
52
+ end
53
+ else
54
+ raise NameError, "Element class does not have that attribute or method"
55
+ end
56
+ else
57
+ super
44
58
  end
45
- else
46
- nil
47
59
  end
48
- end
49
60
 
50
- def self.all
51
- @@elements
52
- end
53
-
54
- def self.get_by_symbol(symbol)
55
- new(self.all.select { |element| element[:symbol] === symbol }.first)
56
- end
57
-
58
- def self.get_by_atomic_number(z)
59
- new(self.all.select { |element| element[:atomic_number] === z }.first)
60
- end
61
+ def self.all
62
+ @@elements
63
+ end
61
64
 
65
+ def initialize args
66
+ if args
67
+ args.each do |k,v|
68
+ instance_variable_set("@#{k}", v) unless v.nil?
69
+ end
70
+ else
71
+ nil
72
+ end
73
+ end
62
74
  end
63
75
  end
data/lib/eulim/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Eulim
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eulim
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Syed Fazil Basheer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-22 00:00:00.000000000 Z
11
+ date: 2017-05-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler