Class: Elementy::Element
- Inherits:
-
Object
- Object
- Elementy::Element
- Extended by:
- Enumerable
- Defined in:
- lib/elementy/element.rb
Instance Attribute Summary collapse
-
#electrons ⇒ Object
Returns the value of attribute electrons.
-
#group ⇒ Object
Returns the value of attribute group.
-
#molar ⇒ Object
Returns the value of attribute molar.
-
#name ⇒ Object
Returns the value of attribute name.
-
#number ⇒ Object
Returns the value of attribute number.
-
#position ⇒ Object
Returns the value of attribute position.
-
#symbol ⇒ Object
Returns the value of attribute symbol.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes) ⇒ Element
constructor
A new instance of Element.
- #to_console ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(attributes) ⇒ Element
Returns a new instance of Element.
29 30 31 32 33 |
# File 'lib/elementy/element.rb', line 29 def initialize(attributes) attributes.each do |k,v| send "#{k}=", v end end |
Instance Attribute Details
#electrons ⇒ Object
Returns the value of attribute electrons.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def electrons @electrons end |
#group ⇒ Object
Returns the value of attribute group.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def group @group end |
#molar ⇒ Object
Returns the value of attribute molar.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def molar @molar end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def name @name end |
#number ⇒ Object
Returns the value of attribute number.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def number @number end |
#position ⇒ Object
Returns the value of attribute position.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def position @position end |
#symbol ⇒ Object
Returns the value of attribute symbol.
4 5 6 |
# File 'lib/elementy/element.rb', line 4 def symbol @symbol end |
Class Method Details
.all ⇒ Object
8 9 10 |
# File 'lib/elementy/element.rb', line 8 def all @all ||= defaults.map { |d| new d } end |
.defaults ⇒ Object
16 17 18 |
# File 'lib/elementy/element.rb', line 16 def defaults YAML.load(File.open "#{Elementy.root}/data/elements.yaml") end |
.each ⇒ Object
12 13 14 |
# File 'lib/elementy/element.rb', line 12 def each all.each { |e| yield e } end |
.search(term) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/elementy/element.rb', line 20 def search(term) if term.is_a?(Numeric) || term =~ /\A\d*\Z/ find { |e| e.number == term.to_i} else find { |e| [e.symbol, e.name].any? { |a| a.upcase == term.to_s.upcase} } end end |
Instance Method Details
#to_console ⇒ Object
40 41 42 43 44 |
# File 'lib/elementy/element.rb', line 40 def to_console to_hash.map do |k,v| " #{k}:#{' ' * (12-k.length)}#{v}" end.join("\n") end |
#to_hash ⇒ Object
35 36 37 38 |
# File 'lib/elementy/element.rb', line 35 def to_hash { symbol: symbol, name: name, number: number, molar: molar, position: position, group: group, electrons: electrons } end |