Skip to content

Instantly share code, notes, and snippets.

@McPolemic
Created April 13, 2020 18:56
Show Gist options
  • Save McPolemic/d671baf0ccbe74c4ff52d79bc80042eb to your computer and use it in GitHub Desktop.
Save McPolemic/d671baf0ccbe74c4ff52d79bc80042eb to your computer and use it in GitHub Desktop.
class Errorable
attr_accessor :state, :errors
def initialize(state)
@state = state
@errors = []
end
def method_missing(symbol, *args)
raise NoMethodError, "undefined method `#{symbol}' for #{self}" unless self.respond_to?(symbol, true)
return false unless ok?
self.send(symbol, *args)
end
def ok?
@errors.empty?
end
private
def validate_params(params)
if params[:greeting] == "Hello"
true
else
@errors << "greeting must be 'Hello'"
false
end
end
def get_lesson_type
if state[:lesson_type_id]
lesson_type = "This lesson here"
state[:lesson_type] = lesson_type
return lesson_type
else
@errors << "Lesson type ID not found"
return nil
end
end
end
errorable = Errorable.new({lesson_type_id: nil})
errorable.validate_params({greeting: "Hello"})
lesson_type = errorable.get_lesson_type
puts "errorable.ok => #{errorable.ok?.inspect}"
puts "errorable.state => #{errorable.state.inspect}"
puts "errorable.errors => #{errorable.errors.inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment