hatchery 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/hatchery.rb +132 -0
- metadata +67 -0
data/lib/hatchery.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# THIS GEM (including all code) is copyrighted (C) 2012 TOM CORELIS
|
2
|
+
# SEE LICENSE.txt for the license agreement
|
3
|
+
|
4
|
+
module Hatchery
|
5
|
+
module HatcheryMethods
|
6
|
+
|
7
|
+
# Declare this in the class you want to use
|
8
|
+
# Accepts an array of parameters of the types Symbol or Hash:
|
9
|
+
# spawn_with :name, :xp => { hash of options }, :money, :color => { hash of options... }
|
10
|
+
def hatch_with(*params)
|
11
|
+
|
12
|
+
@params = []
|
13
|
+
@keys = []
|
14
|
+
|
15
|
+
params.each do |p|
|
16
|
+
|
17
|
+
if p.is_a?(::Symbol)
|
18
|
+
|
19
|
+
@params.push( {p => nil} )
|
20
|
+
@keys.push(p)
|
21
|
+
|
22
|
+
elsif p.is_a?(::Hash)
|
23
|
+
|
24
|
+
if p[:global_options]
|
25
|
+
@global_options = p
|
26
|
+
else
|
27
|
+
@params.push(p)
|
28
|
+
@keys.push(p.keys[0])
|
29
|
+
end
|
30
|
+
|
31
|
+
else
|
32
|
+
raise ArgumentError, "Each parameter must be either a symbol or a hash"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
# Run me or hatch() in an IRB prompt!
|
40
|
+
def new_interactive!()
|
41
|
+
|
42
|
+
unless @params || !@params.empty?
|
43
|
+
puts("Use `hatch_with` to declare parameters before you can use me! Aborting...")
|
44
|
+
ap @params
|
45
|
+
return nil
|
46
|
+
end
|
47
|
+
|
48
|
+
zerg = self.new()
|
49
|
+
puts "Let's make a new #{zerg.class.name}!"
|
50
|
+
|
51
|
+
while (true == true)
|
52
|
+
|
53
|
+
@params.each do |p|
|
54
|
+
zerg[p.keys[0].to_sym] = prompt_for_value(p.keys[0])
|
55
|
+
end
|
56
|
+
|
57
|
+
y = ''
|
58
|
+
committed = false
|
59
|
+
|
60
|
+
while (committed == false)
|
61
|
+
|
62
|
+
ap zerg
|
63
|
+
|
64
|
+
puts "Verify the new #{zerg.class.name} shown above."
|
65
|
+
puts " * Answer with the name of an attribute to edit it, or"
|
66
|
+
puts " * Answer with:"
|
67
|
+
puts " Y to return your new #{zerg.class.name}, or "
|
68
|
+
puts " N to restart from scratch, or"
|
69
|
+
puts " Q to abort.\n"
|
70
|
+
print "------> WHAT SAY YOU? (y/n/q/attribute): "
|
71
|
+
y = gets.chomp
|
72
|
+
|
73
|
+
if y =~ /^(y|n|q)$/i
|
74
|
+
|
75
|
+
case $1.downcase
|
76
|
+
when 'y'
|
77
|
+
# if x.save
|
78
|
+
# puts "saved!"
|
79
|
+
# committed == true
|
80
|
+
# return true
|
81
|
+
# else
|
82
|
+
# puts "Validation failed. Why?\n"
|
83
|
+
# puts x.errors.each { |msg| puts " - #{msg}" }
|
84
|
+
# end
|
85
|
+
committed = true
|
86
|
+
return zerg
|
87
|
+
when 'n'
|
88
|
+
puts "Tabula rasa..."
|
89
|
+
committed = true
|
90
|
+
# will restart at the beginning of this while block
|
91
|
+
when 'q'
|
92
|
+
committed = true
|
93
|
+
return false
|
94
|
+
end
|
95
|
+
|
96
|
+
elsif @params
|
97
|
+
zerg[y.to_sym] = prompt_for_value(y)
|
98
|
+
else
|
99
|
+
puts "I don't understand that input. Try again!"
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
# alias for new_interactive
|
109
|
+
def hatch!()
|
110
|
+
new_interactive!()
|
111
|
+
end
|
112
|
+
|
113
|
+
###########################################################################################
|
114
|
+
private
|
115
|
+
|
116
|
+
def prompt_for_value(title)
|
117
|
+
print "#{title}: "
|
118
|
+
gets.chomp
|
119
|
+
end
|
120
|
+
|
121
|
+
@params;
|
122
|
+
@keys;
|
123
|
+
@global_opts = {}
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
def self.included(base)
|
128
|
+
base.extend(HatcheryMethods)
|
129
|
+
end
|
130
|
+
# extend HatcheryMethods
|
131
|
+
|
132
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hatchery
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tom Corelis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-03 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: awesome_print
|
16
|
+
requirement: &2156933900 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2156933900
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activemodel
|
27
|
+
requirement: &2156933280 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2156933280
|
36
|
+
description: A tool for quick, interactive creation of ActiveModel objects via IRB
|
37
|
+
email: tom@tomcorelis.com
|
38
|
+
executables: []
|
39
|
+
extensions: []
|
40
|
+
extra_rdoc_files: []
|
41
|
+
files:
|
42
|
+
- lib/hatchery.rb
|
43
|
+
homepage: http://www.tomcorelis.com
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.10
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Interactive object spawner
|
67
|
+
test_files: []
|