Module: Hatchery::HatcheryMethods

Defined in:
lib/hatchery.rb

Instance Method Summary collapse

Instance Method Details

#hatch!Object

alias for new_interactive



110
111
112
# File 'lib/hatchery.rb', line 110

def hatch!()
  new_interactive!()
end

#hatch_with(*params) ⇒ Object

Declare this in the class you want to use Accepts an array of parameters of the types Symbol or Hash:

spawn_with :name, :xp => { hash of options }, :money, :color => { hash of options... }
NOTE: options code is tentative. do not use options yet...


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
# File 'lib/hatchery.rb', line 11

def hatch_with(*params)
  
  @params = []
  @keys = []
  
  params.each do |p|
    
    if p.is_a?(::Symbol)
      
      @params.push( {p => nil} )
      @keys.push(p)
    
    elsif p.is_a?(::Hash)
    
      if p[:global_options]
        @global_options = p
      else
        @params.push(p)
        @keys.push(p.keys[0])
      end
    
    else
      raise ArgumentError, "Each parameter must be either a symbol or a hash"
    end
    
  end
  
end

#new_interactive!Object

Run me or hatch!() in an IRB prompt!



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/hatchery.rb', line 41

def new_interactive!()
  
  unless @params || !@params.empty?
    puts("Use `hatch_with` to declare parameters before you can use me! Aborting...")
    ap @params
    return nil
  end
  
  zerg = self.new()
  puts "Let's make a new #{zerg.class.name}!"
  
  while (true == true)

    @params.each do |p|
      zerg[p.keys[0].to_sym] = prompt_for_value(p.keys[0])
    end
  
  y = ''
  committed = false
  
  while (committed == false)
  
    ap zerg

      puts "Verify the new #{zerg.class.name} shown above."
      puts " * Answer with the name of an attribute to edit it, or"
      puts " * Answer with:"
      puts "     Y to return your new #{zerg.class.name}, or "
      puts "     N to restart from scratch, or"
      puts "     Q to abort.\n"
    print "------> WHAT SAY YOU? (y/n/q/attribute): "
    y = gets.chomp

    if y =~ /^(y|n|q)$/i

      case $1.downcase
          when 'y'
#           if x.save
#               puts "saved!"
#             committed == true
#              return true
#           else
#             puts "Validation failed. Why?\n"
#             puts x.errors.each { |msg| puts " - #{msg}" }
#           end
            committed = true
            return zerg
          when 'n'
            puts "Tabula rasa..."
            committed = true
            # will restart at the beginning of this while block
          when 'q'
            committed = true
            return false
        end

      elsif @keys.include?(y.to_sym)
        zerg[y.to_sym] = prompt_for_value(y)
      else
        puts "I don't understand that input. Try again!"
      end
  
  end
      
end

end