Skip to content

Instantly share code, notes, and snippets.

@lunks
Last active August 1, 2020 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lunks/3455b3ad64c357e467d5ad580211ff59 to your computer and use it in GitHub Desktop.
Save lunks/3455b3ad64c357e467d5ad580211ff59 to your computer and use it in GitHub Desktop.
u-case examples/proposals
class User::Create < Micro::Case
attributes :name, :age
def call!
return invalid_user unless valid?
if User.create(name: name, age: age)
Success
else
Failure
end
end
private
def invalid_user
return Failure(:name_cant_be_blank) if !name_present?
return Failure(:under_18) if !over_18?
end
def valid?
name.present? && over_18?
end
def name_present?
name.present?
end
def over_18?
age > 18
end
end
class User::Create < Micro::Case
attributes :name, :age
def call!
validate
create
end
private
def validate
Failure(:name_cant_be_blank) if !name_present?
Failure(:under_18) if !over_18?
end
def create
user = User.create!(name: name, age: age)
Success user: user
rescue
Failure user_not_created: user.errors
end
def name_present?
name.present?
end
def over_18?
age >= 18
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment