Skip to content

Commit

Permalink
greatly simplified the matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
jnicklas committed Dec 19, 2007
1 parent a3ac822 commit 5c73ba0
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 242 deletions.
22 changes: 11 additions & 11 deletions lib/matchers/english.rb
@@ -1,18 +1,18 @@
Rorem::Filler.match(%w(id type), :nothing)
Rorem::Filler.match(/_type$/, :nothing)
Rorem::Filler.match(/_id$/, :nothing)
Rorem.add_matcher(%w(id type), :nothing)
Rorem.add_matcher(/_type$/, :nothing)
Rorem.add_matcher(/_id$/, :nothing)

Rorem::Filler.match('name', :name, :table => %w(users people contacts admins employees players))
Rorem::Filler.match(%w(first_name firstname given_name givenname), :first_name)
Rorem::Filler.match(%w(last_name lastname family_name familyname sur_name surname), :last_name)
Rorem.add_matcher('name', :name, :table => %w(users people contacts admins employees players))
Rorem.add_matcher(%w(first_name firstname given_name givenname), :first_name)
Rorem.add_matcher(%w(last_name lastname family_name familyname sur_name surname), :last_name)

Rorem::Filler.match('email') do |rorem|
Rorem.add_matcher('email') do |rorem|
tlds = %w(se com de org net co.uk co.jp)
rorem.first_name.downcase << '.' << rorem.last_name.downcase << '@' << rorem.word(6..12) << '.' << tlds.random
end

Rorem::Filler.match( %w(birthdate date_of_birth dateofbirth) ) {|r| r.date 45.years.ago..18.years.ago }
Rorem.add_matcher( %w(birthdate date_of_birth dateofbirth) ) {|r| r.date 45.years.ago..18.years.ago }

Rorem::Filler.match(/^computerized_/, :word )
Rorem::Filler.match(/^crypted_/, :digest)
Rorem::Filler.match(:salt, :digest)
Rorem.add_matcher(/^computerized_/, :word )
Rorem.add_matcher(/^crypted_/, :digest)
Rorem.add_matcher(:salt, :digest)
47 changes: 24 additions & 23 deletions lib/rorem.rb
@@ -1,33 +1,34 @@
module Rorem

class Initializer

def self.matchers
@matchers ||= []
end

def self.init
%w(array_extension randomizer analytics generator record matcher setter filler).each do |path|
require File.join(File.dirname(__FILE__), 'rorem', path)
end

require File.join(File.dirname(__FILE__), 'matchers', 'english')

ActiveRecord::Base.send(:include, Rorem::Record)

self.rails_init if defined?(RAILS_ROOT)

nil # otherwise this returns AR::Base which looks weird :P
def self.init(language = 'english')
%w(array_extension randomizer analytics generator active_record_extension matcher filler).each do |path|
require File.join(File.dirname(__FILE__), 'rorem', path)
end

def self.rails_init
ActionView::Base.send(:include, Rorem::Helper)
ActionController::Base.send(:include, Rorem::Helper)
require File.join(File.dirname(__FILE__), 'matchers', language)

rorem_file = File.join(RAILS_ROOT, *%w(config rorem.rb))
ActiveRecord::Base.extend Rorem::ActiveRecordExtension

self.rails_init if defined?(RAILS_ROOT)

require rorem_file if File.exists?(rorem_file)
end
nil # otherwise this returns AR::Base which looks weird :P
end

end
def self.rails_init
ActionView::Base.send(:include, Rorem::Helper)
ActionController::Base.send(:include, Rorem::Helper)

rorem_file = File.join(RAILS_ROOT, *%w(config rorem.rb))

def Rorem.init
Rorem::Initializer.init
require rorem_file if File.exists?(rorem_file)
end

def self.add_matcher(condition, *args, &block)
self.matchers.push(Rorem::Matcher.new(condition, *args, &block))
end

end
71 changes: 71 additions & 0 deletions lib/rorem/active_record_extension.rb
@@ -0,0 +1,71 @@
module Rorem

module ActiveRecordExtension

def make_fillable(options={})
self.send(:include, ActiveRecordMethods)
self.rorem_attributes = self.column_names - [ self.primary_key ]
end

end


module ActiveRecordMethods

def self.append_features(base) #:nodoc:
super
base.extend(ClassMethods)
end

def fill
self.class.rorem_attributes.each do |attribute|

# loop through all existing matchers and check if this attribute matches anything
self.class.all_rorem_matchers do |matcher|

# if the matcher matches it will return a parameter, which might be an Array if the
# matcher's condition was a Regex with capturing groups.
if match_parameters = matcher.match?(attribute, self.class.table_name)
if match_parameters.is_a?(Array)
matcher.to_proc.call(Rorem::Generator, *match_parameters)
else
matcher.to_proc.call(Rorem::Generator)
end

break # abort the looping of the matchers
end
end

end
end

module ClassMethods

attr_accessor :rorem_attributes

def rorem_matchers
@rorem_matchers ||= []
end

def all_rorem_matchers
self.rorem_matchers + Rorem.matchers
end

def add_rorem_matcher(condition, *args, &block)
self.rorem_matchers.push(Rorem::Matcher.new(condition, *args, &block))
end

def fill(count)
self.transaction do
Rorem::Randomizer.random_length(count).times do
record = self.new
record.fill
record.save!
end
end
end

end

end
end
43 changes: 0 additions & 43 deletions lib/rorem/record.rb

This file was deleted.

45 changes: 0 additions & 45 deletions lib/rorem/setter.rb

This file was deleted.

120 changes: 0 additions & 120 deletions spec/setter_spec.rb

This file was deleted.

0 comments on commit 5c73ba0

Please sign in to comment.