File: recipe_common.rb

package info (click to toggle)
ruby-tins 1.32.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,248 kB
  • sloc: ruby: 6,659; makefile: 3
file content (65 lines) | stat: -rw-r--r-- 1,035 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Ingredient
  class << self
    def inherited(klass)
      ingredients << klass
    end

    attr_accessor :ingredients

    def ingredient(name, amount)
      name = name.to_s.gsub(/_/, '').capitalize
      if klass = ingredients.find { |n| n.to_s == name }
        klass.new(amount)
      else
        raise "unknown ingredient #{name}"
      end
    end
  end
  self.ingredients = []

  def initialize(amount = 1)
    @amount = amount
  end

  def name
    self.class.name.downcase
  end

  attr_reader :amount

  def to_s
    "#@amount #{name}"
  end
end

class Unit
  class << self
    def inherited(klass)
      units << klass
    end

    attr_accessor :units

    def unit(name, amount)
      name = name.to_s.gsub(/s$/, '').capitalize
      if klass = units.find { |n| n.to_s == name }
        klass.new(amount)
      end
    end
  end
  self.units = []

  def initialize(n = 1)
    @n = n
  end

  def name
    self.class.name.downcase
  end

  attr_reader :n

  def to_s
    "#@n #{name}#{@n > 1 ? 's' : ''}"
  end
end