Class: Maths::Brain

Inherits:
Object
  • Object
show all
Defined in:
lib/maths/brain.rb

Class Method Summary collapse

Class Method Details

.assign(variable, value) ⇒ Object

Public: Assigns data



5
6
7
8
9
10
11
# File 'lib/maths/brain.rb', line 5

def self.assign(variable, value)
  data = read
  data[variable] = value
  write(data)
  
  value
end

.brain_fileObject

Public: The file we’re using to store our memories



25
26
27
# File 'lib/maths/brain.rb', line 25

def self.brain_file
  File.join(ENV['HOME'], ".maths_brain")
end

.lookup(variable) ⇒ Object

Public: Looks up data



14
15
16
17
18
19
20
21
22
# File 'lib/maths/brain.rb', line 14

def self.lookup(variable)
  value = read[variable]
  
  if value.nil?
    nil
  else
    BigDecimal.new(read[variable])
  end
end

.readObject

Public: Reads the data out of the brain



30
31
32
33
34
35
36
# File 'lib/maths/brain.rb', line 30

def self.read
  if File.exist?(brain_file)
    JSON.parse(File.read(brain_file))
  else
    {}
  end
end

.write(data) ⇒ Object

Public: Reads the



39
40
41
42
43
# File 'lib/maths/brain.rb', line 39

def self.write(data)
  File.open(brain_file, "wb") do |file|
    file << JSON.generate(data)
  end
end