Class: Hbtrack::ListCommand
- Defined in:
- lib/hbtrack/command/list_command.rb
Instance Attribute Summary collapse
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#printer ⇒ Object
readonly
Returns the value of attribute printer.
Instance Method Summary collapse
- #create_option_parser ⇒ Object
- #execute ⇒ Object
- #get_entry_from_db(store, id) ⇒ Object
- #get_habits_from_db(store) ⇒ Object
-
#initialize(store_path, options) ⇒ ListCommand
constructor
A new instance of ListCommand.
- #list_from_db(store, names) ⇒ Object
Methods inherited from Command
Constructor Details
#initialize(store_path, options) ⇒ ListCommand
Returns a new instance of ListCommand.
11 12 13 14 |
# File 'lib/hbtrack/command/list_command.rb', line 11 def initialize(store_path, ) @month = Date.today.strftime("%Y,%-m").to_sym super(store_path, ) end |
Instance Attribute Details
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
9 10 11 |
# File 'lib/hbtrack/command/list_command.rb', line 9 def formatter @formatter end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
9 10 11 |
# File 'lib/hbtrack/command/list_command.rb', line 9 def month @month end |
#printer ⇒ Object (readonly)
Returns the value of attribute printer.
9 10 11 |
# File 'lib/hbtrack/command/list_command.rb', line 9 def printer @printer end |
Instance Method Details
#create_option_parser ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/hbtrack/command/list_command.rb', line 21 def create_option_parser OptionParser.new do |opts| opts. = 'Usage: hbtrack list [<habit_name>] [options]' opts.separator '' opts.separator 'Options:' # TODO: Renamed to better describe the functionality # as in this case user are required toe enter # the input in the form of <year>,<month> opts.on('-m', '--month MONTH', 'List habit(s) according to month provided') do |month| @month = month.to_sym @year, @mon = month.split(',') end opts.on_tail('-h', '--help', 'Prints this help') do puts opts exit end end end |
#execute ⇒ Object
16 17 18 19 |
# File 'lib/hbtrack/command/list_command.rb', line 16 def execute return list_from_db(local_store, @names) super end |
#get_entry_from_db(store, id) ⇒ Object
58 59 60 61 62 |
# File 'lib/hbtrack/command/list_command.rb', line 58 def get_entry_from_db(store, id) month = @mon.to_i >= 1 ? @mon.to_i : Date.today.month year = @year.to_i >= 1 ? @year.to_i : Date.today.year store.get_entries_of_month(id, month, year) end |
#get_habits_from_db(store) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/hbtrack/command/list_command.rb', line 48 def get_habits_from_db(store) habits = [] entries = {} habits = store.get_all_habits habits.each do |habit| entries[habit[:title]] = get_entry_from_db(store, habit[:id]) end [habits, entries] end |
#list_from_db(store, names) ⇒ Object
42 43 44 45 46 |
# File 'lib/hbtrack/command/list_command.rb', line 42 def list_from_db(store, names) habits = [] habits, entries = get_habits_from_db(store) Hbtrack::CLI::View.list_all_habits(habits, entries, @month) end |