Appearance
Types
Custom types are the best way to share input-related logic between generators.
Built-in Ruby Types
Loci can automatically cast inputs to common Ruby types. These types require no extra setup and behave exactly as you expect.
ruby
arg :start_date, type: Dateruby
params.start_date
# => #<Date ...>
params.start_date.class
# => DateCustom Types
Any class that implements initialize(value) can be used as a type.
Here is a simplified custom type that represents a Rails resource.
ruby
# loci/types/rails/resource.rb
module Rails
class Resource
def initialize(raw)
@raw = raw
end
def controller_path
File.join("app", "controllers", "#{@raw.pluralize}_controller.rb")
end
def model_path
File.join("app", "models", "#{@raw.singularize}.rb")
end
end
endUsing a Type in a Generator
ruby
arg :resource, type: Rails::ResourceNow the generator receives a rich object instead of a string.
ruby
params.resource.controller_path
# => "app/controllers/posts_controller.rb"
params.resource.model_path
# => "app/models/post.rb"