Last active
June 27, 2017 09:00
-
-
Save kke/eb64b994e07c0283276f12fb2ca2f31b to your computer and use it in GitHub Desktop.
This file contains 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
module Recipe | |
Version = Class.new(Gem::Version) | |
class Definition | |
attr_reader :options | |
def initialize(recipe_name, options = {}) | |
@options = options | |
recipe recipe_name | |
end | |
def errors | |
@errors ||= {} | |
end | |
def recipe(name = nil) | |
return @recipe if name.nil? | |
@recipe = name | |
end | |
def version(version_number = nil) | |
return @version if version_number.nil? | |
@version = Version.new(version_number) | |
rescue => ex | |
add_error(:version, :format, ex.message) | |
end | |
private | |
def add_error(field, type, message) | |
errors[field] ||= {} | |
errors[field][type] ||= [] | |
errors[field][type] << message | |
end | |
end | |
def self.define(name = nil, options = {}, &block) | |
recipe = Definition.new(name, options) | |
recipe.instance_exec(&block) if block_given? | |
loaded_definitions << recipe | |
recipe | |
end | |
def self.loaded_definitions | |
@loaded_definitions ||= [] | |
end | |
end | |
recipe = Recipe.define "moms spaghetti" do | |
version "1.3.4" | |
ingredients do | |
ingredient "spaghetti" do | |
grams 100 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment