Class: Elementy::Element

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/elementy/element.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#electronsObject

Returns the value of attribute electrons.



4
5
6
# File 'lib/elementy/element.rb', line 4

def electrons
  @electrons
end

#groupObject

Returns the value of attribute group.



4
5
6
# File 'lib/elementy/element.rb', line 4

def group
  @group
end

#molarObject

Returns the value of attribute molar.



4
5
6
# File 'lib/elementy/element.rb', line 4

def molar
  @molar
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/elementy/element.rb', line 4

def name
  @name
end

#numberObject

Returns the value of attribute number.



4
5
6
# File 'lib/elementy/element.rb', line 4

def number
  @number
end

#positionObject

Returns the value of attribute position.



4
5
6
# File 'lib/elementy/element.rb', line 4

def position
  @position
end

#symbolObject

Returns the value of attribute symbol.



4
5
6
# File 'lib/elementy/element.rb', line 4

def symbol
  @symbol
end

Class Method Details

.allObject



8
9
10
# File 'lib/elementy/element.rb', line 8

def all
  @all ||= defaults.map { |d| new d }
end

.defaultsObject



16
17
18
# File 'lib/elementy/element.rb', line 16

def defaults
  YAML.load(File.open "#{Elementy.root}/data/elements.yaml")
end

.eachObject



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_consoleObject



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_hashObject



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