Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.