Class: HAL9000::Manager
- Inherits:
-
Object
- Object
- HAL9000::Manager
- Defined in:
- lib/hal9000/manager.rb
Constant Summary collapse
- HAL_FILE =
"#{ENV['HOME']}/.hal"
Instance Attribute Summary collapse
-
#actionstore ⇒ Object
Returns the value of attribute actionstore.
Instance Method Summary collapse
- #create_action(thought, action) ⇒ Object
- #delete_action(thought) ⇒ Object
- #find_action(thought) ⇒ Object
-
#hal_file ⇒ Object
Persistence ==============================================================.
-
#initialize ⇒ Manager
constructor
A new instance of Manager.
- #list_actions ⇒ Object
- #persist ⇒ Object
- #populate ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize ⇒ Manager
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
#actionstore ⇒ Object
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_file ⇒ Object
Persistence ==============================================================
36 37 38 |
# File 'lib/hal9000/manager.rb', line 36 def hal_file ENV['HAL_FILE'] || HAL_FILE end |
#list_actions ⇒ Object
15 16 17 |
# File 'lib/hal9000/manager.rb', line 15 def list_actions actionstore.list end |
#persist ⇒ Object
49 50 51 |
# File 'lib/hal9000/manager.rb', line 49 def persist File.open(hal_file, 'w') { |f| f.write(to_json) } end |
#populate ⇒ Object
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_json ⇒ Object
53 54 55 |
# File 'lib/hal9000/manager.rb', line 53 def to_json Oj.dump(actionstore, { :mode => :compat, :indent => 2 }) end |