Class: HAL9000::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/hal9000/manager.rb

Constant Summary collapse

HAL_FILE =
"#{ENV['HOME']}/.hal"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



10
11
12
13
# File 'lib/hal9000/manager.rb', line 10

def initialize
  @actionstore = ActionStore.new
  populate
end

Instance Attribute Details

#actionstoreObject

Returns the value of attribute actionstore.



8
9
10
# File 'lib/hal9000/manager.rb', line 8

def actionstore
  @actionstore
end

Instance Method Details

#create_action(thought, action) ⇒ Object



19
20
21
22
23
# File 'lib/hal9000/manager.rb', line 19

def create_action(thought, action)
  action = Action.new(thought, action)
  return unless actionstore.add_action(action)
  persist
end

#delete_action(thought) ⇒ Object



25
26
27
28
# File 'lib/hal9000/manager.rb', line 25

def delete_action(thought)
  return unless actionstore.delete_action(thought)
  persist
end

#find_action(thought) ⇒ Object



30
31
32
# File 'lib/hal9000/manager.rb', line 30

def find_action(thought)
  actionstore.find_action(thought)
end

#hal_fileObject

Persistence ==============================================================



36
37
38
# File 'lib/hal9000/manager.rb', line 36

def hal_file
  ENV['HAL_FILE'] || HAL_FILE
end

#list_actionsObject



15
16
17
# File 'lib/hal9000/manager.rb', line 15

def list_actions
  actionstore.list
end

#persistObject



49
50
51
# File 'lib/hal9000/manager.rb', line 49

def persist
  File.open(hal_file, 'w') { |f| f.write(to_json) }
end

#populateObject



40
41
42
43
44
45
46
47
# File 'lib/hal9000/manager.rb', line 40

def populate
  unless !File.exist?(hal_file)
    storage = Oj.load(IO.read(hal_file), { :mode => :compat, :indent => 2 })
    storage['actions'].each do |action|
      actionstore.add_action(Action.new(action['thought'], action['action']))
    end
  end
end

#to_jsonObject



53
54
55
# File 'lib/hal9000/manager.rb', line 53

def to_json
  Oj.dump(actionstore, { :mode => :compat, :indent => 2 })
end