Sha256: e03512ef68e472d93894f1cf64198bc38e1d19b05ca2435b6a461d1f5b95b0a2

Contents?: true

Size: 1.53 KB

Versions: 128

Compression:

Stored size: 1.53 KB

Contents

#!/usr/bin/env ruby

require 'tins/xt'
$:.unshift 'examples'
require 'recipe_common'

class Cup < Unit; end
class Teaspoon < Unit; end
class Tablespoon < Unit; end

class Flour < Ingredient; end
class Bakingpowder < Ingredient; end
class Salt < Ingredient; end
class Egg < Ingredient; end
class Milk < Ingredient; end
class Butter < Ingredient; end

class Recipe < Tins::BlankSlate.with(:respond_to?, :instance_exec, :inspect, /^deflect/)
  include Tins::Deflect

  def initialize(&block)
    @ingredients = []
    deflector = Deflector.new do |number, id, name|
      if unit = Unit.unit(name, number)
        unit
      else
        ingredient = Ingredient.ingredient(name, number)
        @ingredients << ingredient
      end
    end
    deflect(Numeric, :method_missing, deflector) do
      instance_exec(&block)
    end
  end

  attr_reader :ingredients

  def to_a
    @ingredients
  end
  alias to_ary to_a

  def to_s
    to_a * "\n"
  end
  alias inspect to_s

  def method_missing(name, *args)
    name = name.to_s.gsub(/_/, '').capitalize
    @ingredients << Object.const_get(name).new(*args)
  end
end

class RecipeInterpreter
  def recipe(&block)
    Recipe.new(&block)
  end
end

recipe_source = <<EOT
pancakes = recipe do
  flour         2.cups
  baking_powder 2.5.teaspoons
  salt          0.5.teaspoon
  egg           1
  milk          1.5.cups
  butter        2.tablespoons
end
EOT

pancakes = RecipeInterpreter.new.interpret(recipe_source)
puts pancakes

puts

def recipe(&block)
  Recipe.new(&block)
end

pancakes = eval recipe_source
puts pancakes

Version data entries

128 entries across 114 versions & 8 rubygems

Version Path
tins-1.38.0 examples/recipe.rb
tins-1.37.1 examples/recipe.rb
tins-1.37.0 examples/recipe.rb
tins-1.36.1 examples/recipe.rb
tins-1.36.0 examples/recipe.rb
tins-1.35.0 examples/recipe.rb
tins-1.34.0 examples/recipe.rb
tins-1.33.0 examples/recipe.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/examples/recipe.rb
tins-1.32.1 examples/recipe.rb
tins-1.32.0 examples/recipe.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/examples/recipe.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/examples/recipe.rb
tins-1.31.1 examples/recipe.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/examples/recipe.rb
tins-1.31.0 examples/recipe.rb
tins-1.30.0 examples/recipe.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/examples/recipe.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/examples/recipe.rb
tdiary-5.1.7 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/examples/recipe.rb