Last active
January 22, 2023 22:22
-
-
Save ch1ago/b4525587e537906c3f6be182e10a8b7f to your computer and use it in GitHub Desktop.
tres exemplos de controllers Command para a landing page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalculatorCommand < AppCommand | |
class Error < StandardError; end | |
def self.sum(a, b)= a + b | |
def self.sub(a, b)= a - b | |
def self.mul(a, b)= a * b | |
def self.div(a, b)= a / b | |
def self.call (args) | |
log "Called #{self}.#{__method__} with args #{args}" | |
new.call(args) | |
end | |
def call(args) | |
log "Called #{self}.#{__method__} with args #{args}" | |
return help if args.empty? | |
@args = args | |
handle_args | |
validate_args | |
calculate_result | |
rescue StandardError => error | |
@error = error | |
handle_error | |
end | |
def handle_args | |
log "Called #{self}.#{__method__}" | |
raise Error, "liza calculator 0 + 0" if @args.size != 3 | |
@a = @args[0].to_f | |
@op = @args[1].to_sym | |
@b = @args[2].to_f | |
log "@a = #{@a}" | |
log "@op = #{@op}" | |
log "@b = #{@b}" | |
end | |
def validate_args | |
log "Called #{self}.#{__method__}" | |
raise Error, "#{@op} is not a valid operator" unless [:+, :-, :*, :/].include? @op | |
end | |
def calculate_result | |
log "Called #{self}.#{__method__}" | |
@result = \ | |
case @op | |
when :+ | |
self.class.sum(@a, @b) | |
when :- | |
self.class.sub(@a, @b) | |
when :* | |
self.class.mul(@a, @b) | |
when :/ | |
self.class.div(@a, @b) | |
end | |
log "RESULT: #{@result}" | |
log render "success.txt" | |
end | |
def handle_error | |
log "Called #{self}.#{__method__}" | |
log "an error occured: #{@error}" | |
puts | |
puts render "error.txt" | |
puts | |
help | |
end | |
def help | |
log "Called #{self}.#{__method__}" | |
puts | |
puts render "help.txt" | |
puts | |
end | |
end | |
__END__ | |
# success.txt.erb | |
RESULT: | |
<%= @result %> | |
# error.txt.erb | |
ERROR: | |
<%= @error %> | |
# help.txt.erb | |
NAME: | |
liza calculator | |
DESCRIPTION: | |
Calculates the result of an arithmetic operation. | |
USAGE: | |
liza calculator <a> <op> <b> | |
where <a> and <b> are numbers and <op> is one of the following operators: | |
+ addition | |
- subtraction | |
* multiplication | |
/ division | |
EXAMPLES: | |
liza calculator 1 + 2 | |
liza calculator 3 - 4 | |
liza calculator 5 * 6 | |
liza calculator 7 / 8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalendarCommand < AppCommand | |
class Error < StandardError; end | |
def self.wday(year, month, day) | |
Date.new(year, month, day).strftime("%A") | |
end | |
def self.call(args) | |
log "Called #{self}.#{__method__} with args #{args}" | |
new.call(args) | |
end | |
def call(args) | |
log "Called #{self}.#{__method__} with args #{args}" | |
return help if args.empty? | |
@args = args | |
handle_args | |
validate_args | |
calculate_result | |
rescue StandardError => error | |
@error = error | |
handle_error | |
end | |
def handle_args | |
log "Called #{self}.#{__method__}" | |
raise Error, "liza calendar 2020 10 1" if @args.size != 3 | |
@year = @args[0].to_i | |
@month = @args[1].to_i | |
@day = @args[2].to_i | |
log "@year = #{@year}" | |
log "@month = #{@month}" | |
log "@day = #{@day}" | |
end | |
def validate_args | |
log "Called #{self}.#{__method__}" | |
raise Error, "#{@year} is not a valid year" unless (1900..2100).include? @year | |
raise Error, "#{@month} is not a valid month" unless (1..12).include? @month | |
raise Error, "#{@day} is not a valid day" unless (1..31).include? @day | |
end | |
def calculate_result | |
log "Called #{self}.#{__method__}" | |
@result = self.class.wday(@year, @month, @day) | |
log "RESULT: #{@result}" | |
log render "success.txt" | |
end | |
def handle_error | |
log "Called #{self}.#{__method__}" | |
log render "error.txt" | |
end | |
def help | |
log "Called #{self}.#{__method__}" | |
log render "help.txt" | |
end | |
end | |
__END__ | |
# success.txt.erb | |
RESULT: | |
<%= @result %> | |
# error.txt.erb | |
ERROR: | |
<%= @error %> | |
# help.txt.erb | |
NAME: | |
liza calendar | |
DESCRIPTION: | |
Displays the week day of a given year, month and day. | |
USAGE: | |
liza calendar <year> <month> <day> | |
EXAMPLES: | |
liza calendar 2020 10 1 | |
liza calendar 2020 11 1 | |
liza calendar 2020 12 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class TemperatureCommand < AppCommand | |
class Error < StandardError; end | |
def self.c_to_f(c)= (c * 9.0 / 5.0) + 32 | |
def self.f_to_c(f)= (f - 32) * 5.0 / 9.0 | |
def self.call (args) | |
log "Called #{self}.#{__method__} with args #{args}" | |
new.call(args) | |
end | |
def call(args) | |
log "Called #{self}.#{__method__} with args #{args}" | |
return help if args.empty? | |
@args = args | |
handle_args | |
validate_args | |
calculate_result | |
rescue StandardError => error | |
@error = error | |
handle_error | |
end | |
def handle_args | |
log "Called #{self}.#{__method__}" | |
raise Error, "liza temperature 0 c to f" if @args.size != 4 | |
raise Error, "liza temperature 0 c to f" if @args[-2] != "to" | |
@value = @args[0].to_f | |
@from = @args[1].to_sym | |
@to = @args[3].to_sym | |
log "value = #{@value}" | |
log "from = #{@from}" | |
log "to = #{@to}" | |
end | |
def validate_args | |
log "Called #{self}.#{__method__}" | |
raise Error, "#{@from} is not a valid temperature unit" unless [:c, :f].include? @from | |
raise Error, "#{@to} is not a valid temperature unit" unless [:c, :f].include? @to | |
log "valid" | |
end | |
def calculate_result | |
log "Called #{self}.#{__method__}" | |
return @value if @from == @to | |
@result = \ | |
case @from | |
when :c | |
self.class.c_to_f(@value) | |
when :f | |
self.class.f_to_c(@value) | |
else | |
raise Error, "can't convert from #{@from} to #{@to}" | |
end | |
log "@result = #{@result}" | |
log render "success.txt" | |
end | |
def handle_error | |
log "Called #{self}.#{__method__}" | |
log "an error occured: #{@error}" | |
log render "error.txt" | |
end | |
def help | |
log "Called #{self}.#{__method__}" | |
log render "help.txt" | |
end | |
end | |
__END__ | |
# success.txt.erb | |
SUCCESS: | |
<%= @args.join(" ").upcase %> is <%= @result %> | |
# error.txt.erb | |
ERROR: | |
<%= @error %> | |
# help.txt.erb | |
NAME: | |
liza temperature | |
DESCRIPTION: | |
Convert temperature from one unit to another | |
USAGE: | |
liza temperature <temp> <from> to <to> | |
where <temp> is a number | |
and <from> is a temperature unit (c or f) | |
and <to> is a temperature unit (c or f) | |
EXAMPLES: | |
liza temperature 0 c to f | |
liza temperature 32 f to c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment