Class: David::Command
- Inherits:
-
Object
- Object
- David::Command
- Defined in:
- lib/david/command.rb
Class Method Summary collapse
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(config, cmd_str) ⇒ Command
constructor
A new instance of Command.
- #parse_args(args) ⇒ Object
Constructor Details
#initialize(config, cmd_str) ⇒ Command
Returns a new instance of Command.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/david/command.rb', line 8 def initialize(config, cmd_str) @config = config @child_name = @config.child_name @cmd_str = cmd_str unless @cmd_str.nil? analyze end @db = @config.db end |
Class Method Details
.interact(config) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/david/command.rb', line 51 def self.interact(config) while true puts "What's news about #{config.child_name}?" input = gets.strip if input.empty? next elsif ['quit', 'q', 'bye'].include? input puts 'bye.' break end cmd = Command.new(config, input) cmd.execute end end |
Instance Method Details
#execute ⇒ Object
46 47 48 49 |
# File 'lib/david/command.rb', line 46 def execute @db.execute("INSERT INTO stories (action, thing, happend_at, created_at) VALUES (?, ?, ?, ?)", [@predicate, @object_clause, @happend_at.to_s, @created_at.to_s]) puts "#{@child_name} #{@predicate} #{@object_clause} on #{@happend_at.to_date}." end |
#parse_args(args) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/david/command.rb', line 19 def parse_args(args) if args[0].start_with? '-' args.delete_at 0 else end @predicate = args[0] @happend_at = Time.zone.now @created_at = Time.zone.now on_index = args.rindex 'on' if (on_index.nil?) @object_clause = args[1..].join(' ') else datetime = args[(on_index+1)..].join(' ') @happend_at = datetime&.to_datetime_safe if @happend_at.nil? @object_clause = args[1..].join(' ') else @object_clause = args[1..(on_index-1)].join(' ') end end self end |