class ResponseDecorator
Public Instance Methods
decorate_table(response, options = {})
click to toggle source
# File lib/response_decorator.rb, line 4 def decorate_table response, options = {} @response = response @options = options return '# The list is empty' if response.empty? return '# ' + response[:errors] if response.include? :errors Terminal::Table.new headings: get_header, rows: get_rows end
Private Instance Methods
default_rows()
click to toggle source
# File lib/response_decorator.rb, line 14 def default_rows @response.map do |r| ["#{ r[:id] }", "#{ r[:name] }", "#{ icon_state(r[:state]) }", "#{ r[:category] }"] end end
get_header()
click to toggle source
# File lib/response_decorator.rb, line 23 def get_header head = ['Id', 'Name', 'State', 'Category'] head << 'Date' if @options[:date] head << 'Description' if @options[:description] head end
get_rows()
click to toggle source
# File lib/response_decorator.rb, line 30 def get_rows rows = default_rows return rows if @options.empty? new_rows = options_rows (0...rows.count).map { |i| (rows[i] << new_rows[i]).flatten } end
icon_state(state)
click to toggle source
# File lib/response_decorator.rb, line 48 def icon_state state return '❏' if state == 'todo' '✔' end
option_response(option)
click to toggle source
# File lib/response_decorator.rb, line 44 def option_response option @response.map { |r| "#{ r[option] }" } end
options_rows()
click to toggle source
# File lib/response_decorator.rb, line 37 def options_rows date = option_response :created_at if @options[:date] description = option_response :description if @options[:description] return ([date] + [description]).compact.first if @options.count == 1 [date, description].transpose end