Class: Mdg::Command
- Inherits:
-
Object
- Object
- Mdg::Command
- Defined in:
- lib/mdg/command.rb,
lib/mdg/command/options.rb
Defined Under Namespace
Modules: Options
Class Method Summary collapse
Instance Method Summary collapse
- #create_deed(content, hour) ⇒ Object
- #delete_deed(id) ⇒ Object
- #execute ⇒ Object
-
#initialize(argv) ⇒ Command
constructor
A new instance of Command.
- #list_deed ⇒ Object
- #update_deed(id, attributes) ⇒ Object
Constructor Details
#initialize(argv) ⇒ Command
Returns a new instance of Command.
10 11 12 |
# File 'lib/mdg/command.rb', line 10 def initialize(argv) @argv = argv end |
Class Method Details
.run(argv) ⇒ Object
6 7 8 |
# File 'lib/mdg/command.rb', line 6 def self.run(argv) new(argv).execute end |
Instance Method Details
#create_deed(content, hour) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/mdg/command.rb', line 48 def create_deed(content, hour) ActiveRecord::Base.transaction do deed = Deed.create!(content: content, hour: hour) Timestamp.create!(deed_id: deed.id, totaltime: deed.hour) deed end end |
#delete_deed(id) ⇒ Object
56 57 58 59 |
# File 'lib/mdg/command.rb', line 56 def delete_deed(id) deed = Deed.find(id) deed.destroy end |
#execute ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mdg/command.rb', line 14 def execute = Options.parse!(@argv) sub_command = .delete(:command) if sub_command == 'server' puts 'Start server process...' port_option = [:port].nil? ? '' : "-p #{[:port]}" config = File.(File.join(File.dirname(__FILE__), '..', '..', 'config.ru')) exec "cd #{File.dirname(config)} && rackup -E production #{port_option} #{config}" end DB.prepare deeds = case sub_command when 'create' create_deed([:content], [:hour]) when 'delete' delete_deed([:id]) when 'update' update_deed([:id], ) when 'list' list_deed end display_deeds deeds rescue => e abort "Error: #{e.}" end |
#list_deed ⇒ Object
44 45 46 |
# File 'lib/mdg/command.rb', line 44 def list_deed Deed.order('created_at DESC') end |
#update_deed(id, attributes) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mdg/command.rb', line 61 def update_deed(id, attributes) deed = Deed.find(id) attributes[:hour] = deed.hour + attributes[:hour].to_f today_range = Date.today.beginning_of_day..Date.today.end_of_day ActiveRecord::Base.transaction do deed.update_attributes! attributes Timestamp.where(deed_id: deed.id).where(created_at: today_range).delete_all Timestamp.create!(deed_id: deed.id, totaltime: attributes[:hour]) end deed end |