can_has_fixtures 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/HISTORY CHANGED
@@ -1,4 +1,8 @@
1
1
  can_has_fixtures Release History
2
+ Version 0.1.1 (18 March 2008)
3
+ * Overhaul of Random
4
+ * Added Random#url and Random#boolean
5
+ * Added Array#random and Range#random
2
6
 
3
7
  Version 0.1.0 (16 March 2008)
4
8
  * Initial release.
data/Rakefile CHANGED
@@ -45,14 +45,14 @@ namespace :doc do
45
45
  rdoc.main = 'README'
46
46
  rdoc.title = 'I CAN HAS "CAN HAS FIXTURES?" DOCS? '
47
47
  rdoc.template = 'tools/allison-2.0.3/lib/allison'
48
- rdoc.rdoc_dir = 'doc/can_has_fixtures/rdoc'
48
+ rdoc.rdoc_dir = "doc/#{NAME}/rdoc"
49
49
  rdoc.options << '--line-numbers' << '--inline-source'
50
50
  end
51
51
 
52
52
  desc "rdoc to rubyforge"
53
53
  task :rubyforge => :doc do
54
54
  sh %{chmod -R 755 doc}
55
- sh %{/usr/bin/scp -r -p doc/can_has_fixtures/rdoc/* benburkert@rubyforge.org:/var/www/gforge-projects/canhasgems/can_has_fixtures}
55
+ sh %{/usr/bin/scp -r -p doc/can_has_fixtures/rdoc/* benburkert@rubyforge.org:/var/www/gforge-projects/canhasgems/#{NAME}}
56
56
  end
57
57
  end
58
58
 
@@ -69,8 +69,8 @@ end
69
69
  # release
70
70
  ##############################################################################
71
71
  task :release => [:specs, "doc:rubyforge", :package] do
72
- sh %{rubyforge add_release canhasgems can_has_fixtures "#{CanHasFixtures::VERSION}" pkg/#{NAME}-#{CanHasFixtures::VERSION}.gem}
72
+ sh %{rubyforge add_release canhasgems #{NAME} "#{CanHasFixtures::VERSION}" pkg/#{NAME}-#{CanHasFixtures::VERSION}.gem}
73
73
  %w[zip tgz].each do |ext|
74
- sh %{rubyforge add_file canhasgems can_has_fixtures #{CanHasFixtures::VERSION} pkg/can_has_fixtures-#{CanHasFixtures::VERSION}.#{ext}}
74
+ sh %{rubyforge add_file canhasgems #{NAME} #{CanHasFixtures::VERSION} pkg/#{NAME}-#{CanHasFixtures::VERSION}.#{ext}}
75
75
  end
76
76
  end
@@ -0,0 +1,5 @@
1
+ class Array
2
+ def random
3
+ self[rand(size)]
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ class Range
2
+ def random
3
+ entries.random
4
+ end
5
+ end
@@ -1,2 +1,3 @@
1
- require "can_has_fixtures/core_ext/class"
2
- require "can_has_fixtures/core_ext/fixnum"
1
+ require "can_has_fixtures/core_ext/array"
2
+ require "can_has_fixtures/core_ext/fixnum"
3
+ require "can_has_fixtures/core_ext/range"
@@ -59,7 +59,7 @@ module FixturizeMixin
59
59
 
60
60
  raise "#{identifier} fixture was not found" if attr_procs.nil? || attr_procs.empty?
61
61
 
62
- proc = attr_procs[rand(attr_procs.size)]
62
+ proc = attr_procs.random
63
63
 
64
64
  values = proc.call(*args)
65
65
 
@@ -1,13 +1,39 @@
1
1
  class Random
2
+
2
3
  WORDS_PER_SENTENCE = 3..20
3
4
  SENTENCES_PER_PARAGRAPH = 3..8
5
+ TLDS = %w[com net org biz info gov]
4
6
 
5
7
  attr_accessor :count
6
8
 
7
9
  def self.chain(target)
8
10
  Random.new(target)
9
11
  end
10
-
12
+
13
+ def self.method_missing_with_instance_method_check(meth, *args, &block)
14
+ if instance_methods(false).include?(meth.to_s)
15
+ Random.new.send(meth, *args, &block)
16
+ else
17
+ method_missing_without_instance_method_check(meth, *args, &block)
18
+ end
19
+ end
20
+
21
+ class << self
22
+ alias_method :method_missing_without_instance_method_check, :method_missing
23
+ alias_method :method_missing, :method_missing_with_instance_method_check
24
+ end
25
+
26
+ def method_missing_with_singular_check(meth, *args, &block)
27
+ if Random.instance_methods(false).include?(method = English::Inflect.singular(meth.to_s))
28
+ (1..count).collect { send(method, *args, &block) }
29
+ else
30
+ method_missing_without_singular_check(meth, *args, &block)
31
+ end
32
+ end
33
+
34
+ alias_method :method_missing_without_singular_check, :method_missing
35
+ alias_method :method_missing, :method_missing_with_singular_check
36
+
11
37
  def initialize(count = 1)
12
38
  @count = count
13
39
  end
@@ -16,36 +42,53 @@ class Random
16
42
  @@dictionary ||= File.read("/usr/share/dict/words").split
17
43
  end
18
44
 
19
- def word
20
- dictionary[rand(dictionary.size)]
45
+ def boolean
46
+ [true, false].random
21
47
  end
22
48
 
23
- def words
24
- (1..count).collect { word }
49
+ def word
50
+ dictionary.random
25
51
  end
26
52
 
27
53
  def sentence(punctuate = true)
28
- (1..WORDS_PER_SENTENCE.to_a[rand(WORDS_PER_SENTENCE.to_a.size)]).collect{ word }.join(' ') + (punctuate ? '.' : '')
29
- end
30
-
31
-
32
- def sentences(punctuate = true)
33
- (1..count).collect { sentence(punctuate) }
54
+ WORDS_PER_SENTENCE.random.random.words.join(' ') << (punctuate && '.')
34
55
  end
35
56
 
36
57
  def paragraph(end_with_newline = true)
37
- (1..SENTENCES_PER_PARAGRAPH.to_a[rand(SENTENCES_PER_PARAGRAPH.to_a.size)]).collect{ sentence }.join(' ') + (end_with_newline ? '\n' : '')
38
- end
39
-
40
- def paragraphs(end_with_newline = true)
41
- (1..count).collect { paragraph(end_with_newline) }
58
+ SENTENCES_PER_PARAGRAPH.random.random.sentences.join(' ') << (end_with_newline && '\n')
42
59
  end
43
60
 
44
61
  def email
45
- "#{word}@#{word}.#{%w[com net org biz info gov][rand(6)]}"
62
+ "#{word}@#{word}.#{TLDS.random}"
46
63
  end
47
64
 
48
- def emails
49
- (1..count).collect { email }
65
+ def url
66
+ str = "#{%w[http https ftp].random}://"
67
+ if boolean
68
+ str << word
69
+ if boolean
70
+ str << ":#{word}"
71
+ end
72
+ str << "@"
73
+ end
74
+ if boolean
75
+ str << "www."
76
+ else
77
+ str << "#{word}."
78
+ end
79
+ str << "#{word}.#{TLDS.random}"
80
+ if boolean
81
+ str << "/#{word}"
82
+ if boolean
83
+ str << "/#{word}"
84
+ end
85
+ if boolean
86
+ str << "/#{word}.#{word}"
87
+ end
88
+ if boolean
89
+ str << "?#{word}=#{word}"
90
+ end
91
+ end
92
+ str
50
93
  end
51
94
  end
@@ -1,3 +1,12 @@
1
+ # Copied without permission or regard from merb-core:
2
+ # http://github.com/wycats/merb-core/tree/master/lib/merb-core/core_ext/class.rb
3
+
4
+ # Allows attributes to be shared within an inheritance hierarchy, but where
5
+ # each descendant gets a copy of their parents' attributes, instead of just a
6
+ # pointer to the same. This means that the child can add elements to, for
7
+ # example, an array without those additions being shared with either their
8
+ # parent, siblings, or children, which is unlike the regular class-level
9
+ # attributes that are shared across the entire hierarchy.
1
10
  class Class # :nodoc:
2
11
  include FixturizeMixin
3
12
 
@@ -0,0 +1,197 @@
1
+ module English
2
+
3
+ # = English Nouns Number Inflection.
4
+ #
5
+ # This module provides english singular <-> plural noun inflections.
6
+ module Inflect
7
+
8
+ @singular_of = {}
9
+ @plural_of = {}
10
+
11
+ @singular_rules = []
12
+ @plural_rules = []
13
+
14
+ class << self
15
+ # Define a general exception.
16
+ def word(singular, plural=nil)
17
+ plural = singular unless plural
18
+ singular_word(singular, plural)
19
+ plural_word(singular, plural)
20
+ end
21
+
22
+ # Define a singularization exception.
23
+ def singular_word(singular, plural)
24
+ @singular_of[plural] = singular
25
+ end
26
+
27
+ # Define a pluralization exception.
28
+ def plural_word(singular, plural)
29
+ @plural_of[singular] = plural
30
+ end
31
+
32
+ # Define a general rule.
33
+ def rule(singular, plural)
34
+ singular_rule(singular, plural)
35
+ plural_rule(singular, plural)
36
+ end
37
+
38
+ # Define a singularization rule.
39
+ def singular_rule(singular, plural)
40
+ @singular_rules << [singular, plural]
41
+ end
42
+
43
+ # Define a plurualization rule.
44
+ def plural_rule(singular, plural)
45
+ @plural_rules << [singular, plural]
46
+ end
47
+
48
+ # Read prepared singularization rules.
49
+ def singularization_rules
50
+ return @singularization_rules if @singularization_rules
51
+ sorted = @singular_rules.sort_by{ |s, p| "#{p}".size }.reverse
52
+ @singularization_rules = sorted.collect do |s, p|
53
+ [ /#{p}$/, "#{s}" ]
54
+ end
55
+ end
56
+
57
+ # Read prepared pluralization rules.
58
+ def pluralization_rules
59
+ return @pluralization_rules if @pluralization_rules
60
+ sorted = @plural_rules.sort_by{ |s, p| "#{s}".size }.reverse
61
+ @pluralization_rules = sorted.collect do |s, p|
62
+ [ /#{s}$/, "#{p}" ]
63
+ end
64
+ end
65
+
66
+ #
67
+ def plural_of
68
+ @plural_of
69
+ end
70
+
71
+ #
72
+ def singular_of
73
+ @singular_of
74
+ end
75
+
76
+ # Convert an English word from plurel to singular.
77
+ #
78
+ # "boys".singular #=> boy
79
+ # "tomatoes".singular #=> tomato
80
+ #
81
+ def singular(word)
82
+ if result = singular_of[word]
83
+ return result.dup
84
+ end
85
+ result = word.dup
86
+ singularization_rules.each do |(match, replacement)|
87
+ break if result.gsub!(match, replacement)
88
+ end
89
+ return result
90
+ end
91
+
92
+ # Alias for #singular (a Railism).
93
+ #
94
+ alias_method(:singularize, :singular)
95
+
96
+ # Convert an English word from singular to plurel.
97
+ #
98
+ # "boy".plural #=> boys
99
+ # "tomato".plural #=> tomatoes
100
+ #
101
+ def plural(word)
102
+ if result = plural_of[word]
103
+ return result.dup
104
+ end
105
+ #return self.dup if /s$/ =~ self # ???
106
+ result = word.dup
107
+ pluralization_rules.each do |(match, replacement)|
108
+ break if result.gsub!(match, replacement)
109
+ end
110
+ return result
111
+ end
112
+
113
+ # Alias for #plural (a Railism).
114
+ alias_method(:pluralize, :plural)
115
+ end
116
+
117
+ # One argument means singular and plural are the same.
118
+
119
+ word 'equipment'
120
+ word 'information'
121
+ word 'money'
122
+ word 'species'
123
+ word 'series'
124
+ word 'fish'
125
+ word 'sheep'
126
+ word 'moose'
127
+ word 'hovercraft'
128
+
129
+ # Two arguments defines a singular and plural exception.
130
+
131
+ word 'Swiss' , 'Swiss'
132
+ word 'life' , 'lives'
133
+ word 'wife' , 'wives'
134
+ word 'goose' , 'geese'
135
+ word 'criterion' , 'criteria'
136
+ word 'alias' , 'aliases'
137
+ word 'status' , 'statuses'
138
+ word 'axis' , 'axes'
139
+ word 'crisis' , 'crises'
140
+ word 'testis' , 'testes'
141
+ word 'child' , 'children'
142
+ word 'person' , 'people'
143
+ word 'potato' , 'potatoes'
144
+ word 'tomato' , 'tomatoes'
145
+ word 'buffalo' , 'buffaloes'
146
+ word 'torpedo' , 'torpedoes'
147
+ word 'quiz' , 'quizes'
148
+ word 'matrix' , 'matrices'
149
+ word 'vertex' , 'vetices'
150
+ word 'index' , 'indices'
151
+ word 'ox' , 'oxen'
152
+ word 'mouse' , 'mice'
153
+ word 'louse' , 'lice'
154
+ word 'thesis' , 'theses'
155
+ word 'thief' , 'thieves'
156
+ word 'analysis' , 'analyses'
157
+
158
+ # One-way singularization exception (convert plural to singular).
159
+
160
+ singular_word 'cactus', 'cacti'
161
+
162
+ # General rules.
163
+
164
+ rule 'hive' , 'hives'
165
+ rule 'rf' , 'rves'
166
+ rule 'af' , 'aves'
167
+ rule 'ero' , 'eroes'
168
+ rule 'man' , 'men'
169
+ rule 'ch' , 'ches'
170
+ rule 'sh' , 'shes'
171
+ rule 'ss' , 'sses'
172
+ rule 'ta' , 'tum'
173
+ rule 'ia' , 'ium'
174
+ rule 'ra' , 'rum'
175
+ rule 'ay' , 'ays'
176
+ rule 'ey' , 'eys'
177
+ rule 'oy' , 'oys'
178
+ rule 'uy' , 'uys'
179
+ rule 'y' , 'ies'
180
+ rule 'x' , 'xes'
181
+ rule 'lf' , 'lves'
182
+ rule 'us' , 'uses'
183
+ rule '' , 's'
184
+
185
+ # One-way singular rules.
186
+
187
+ singular_rule 'of' , 'ofs' # proof
188
+ singular_rule 'o' , 'oes' # hero, heroes
189
+ singular_rule 'f' , 'ves'
190
+
191
+ # One-way plural rules.
192
+
193
+ plural_rule 'fe' , 'ves' # safe, wife
194
+ plural_rule 's' , 'ses'
195
+
196
+ end
197
+ end
@@ -0,0 +1,2 @@
1
+ require "can_has_fixtures/vendor/class_ext"
2
+ require "can_has_fixtures/vendor/inflect"
@@ -1,3 +1,3 @@
1
1
  module CanHasFixtures
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,6 +1,7 @@
1
1
  require "can_has_fixtures/util"
2
2
  require "can_has_fixtures/fixturize_mixin"
3
3
  require "can_has_fixtures/core_ext"
4
+ require "can_has_fixtures/vendor"
4
5
 
5
6
  require "can_has_fixtures/random"
6
7
 
@@ -30,12 +31,4 @@ module CanHasFixtures
30
31
  method_missing_without_fixture_check(meth, *args, &block)
31
32
  end
32
33
  end
33
-
34
- def fixture(*args, &block)
35
- p "here"
36
- end
37
-
38
- def fixturize_description
39
- p description_args
40
- end
41
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: can_has_fixtures
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-03-17 00:00:00 -05:00
12
+ date: 2008-03-18 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -28,12 +28,17 @@ files:
28
28
  - README
29
29
  - lib/can_has_fixtures
30
30
  - lib/can_has_fixtures/core_ext
31
- - lib/can_has_fixtures/core_ext/class.rb
31
+ - lib/can_has_fixtures/core_ext/array.rb
32
32
  - lib/can_has_fixtures/core_ext/fixnum.rb
33
+ - lib/can_has_fixtures/core_ext/range.rb
33
34
  - lib/can_has_fixtures/core_ext.rb
34
35
  - lib/can_has_fixtures/fixturize_mixin.rb
35
36
  - lib/can_has_fixtures/random.rb
36
37
  - lib/can_has_fixtures/util.rb
38
+ - lib/can_has_fixtures/vendor
39
+ - lib/can_has_fixtures/vendor/class_ext.rb
40
+ - lib/can_has_fixtures/vendor/inflect.rb
41
+ - lib/can_has_fixtures/vendor.rb
37
42
  - lib/can_has_fixtures/version.rb
38
43
  - lib/can_has_fixtures.rb
39
44
  has_rdoc: false