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 NumberFormatter | |
def format(_number) | |
raise NotImplementedError, "subclass should specify its formatting details" | |
end | |
end | |
class NonPositiveFormatter < NumberFormatter | |
def format(_number) | |
"\n" # nothing to print | |
end |
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
defmodule Dictionary do | |
@name __MODULE__ | |
## API | |
def start_link, | |
do: Agent.start_link(fn -> %{} end, name: @name) | |
def add_words(words), | |
do: Agent.update(@name, &do_add_words(&1, words)) |
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
def required_attributes(obj) | |
target = obj.class == Class ? obj : obj.class | |
target.validators.select do |amv| | |
amv.class == ActiveRecord::Validations::PresenceValidator | |
end.map(&:attributes).flatten | |
end |