Skip to content

Instantly share code, notes, and snippets.

@repomaa
Created August 31, 2018 09:46
Show Gist options
  • Save repomaa/f232632925a7d21126e295330396f9ae to your computer and use it in GitHub Desktop.
Save repomaa/f232632925a7d21126e295330396f9ae to your computer and use it in GitHub Desktop.
OPERATIONS_ORDER = %i[crop scale grayscale]
alias CropTuple = NamedTuple(x_coord: Int32, y_coord: Int32, width: Int32, height: Int32)
abstract struct Operation
end
struct Crop < Operation
getter x_coord : Int32, y_coord : Int32, width : Int32, height : Int32
def initialize(**args)
@x_coord = args[:x_coord]
@y_coord = args[:y_coord]
@width = args[:width]
@height = args[:height]
end
end
struct Grayscale < Operation
end
params = {crop: {x_coord: 2, y_coord: 1, width: 40, height: 100}.as(CropTuple?), grayscale: nil.as(Bool?), id: "foo"}
def operations(params)
operations = OPERATIONS_ORDER.map do |op|
op_params = params[op]?
puts op_params
case op_params
when NamedTuple
case op
when :crop then Crop.new(**op_params)
end
when Bool
case op
when :grayscale then Grayscale.new if op_params
end
end
end
operations.compact
end
operations(params)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment