Module: HelperMethods

Defined in:
lib/blindfold/helpers/db.rb,
lib/blindfold/helpers/date.rb,
lib/blindfold/helpers/text.rb,
lib/blindfold/helpers/machinist.rb

Instance Method Summary collapse

Instance Method Details

#random_datetime(args = {:dayrange => 7, :force_future => false}) ⇒ Object

Inspired by random_data’s date method, github.com/tomharris/random_data



3
4
5
6
# File 'lib/blindfold/helpers/date.rb', line 3

def random_datetime(args={:dayrange => 7, :force_future => false})
  offset = args[:force_future] ? rand(args[:dayrange]) : (rand(args[:dayrange]*2)-args[:dayrange])
  Time.now + (offset * 24 * 60 * 60)
end

#random_string(length = 12) ⇒ Object



2
3
4
# File 'lib/blindfold/helpers/text.rb', line 2

def random_string(length=12)
  Forgery::Basic.text(:at_least => length, :at_most => length)
end

#spawn(name, args = {}) ⇒ Object

Spawn n blueprint driven instances of a model NOTE: Machinist2 makes this easy enough to do with ‘make’ itself



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/blindfold/helpers/machinist.rb', line 4

def spawn(name, args={})
  
  # Reload Machinist blueprints despite them having been loaded earlier.
  # For some reason, Rack::Test's use of ActionController::Dispatcher.call(env)
  # causes the blueprints to fall out of scope for the next Connection instance.
  #
  # TODO: determine root cause, raise ticket if necessary with Rack::Test
  #
  blueprints_dir = File.join(Blindfold.config_dir, Launcher.blueprints_dir)
  if Dir.exists?(blueprints_dir)
    Dir[File.join(blueprints_dir,'**','*_blueprint.rb')].each {|f| load f}
  end
  
  klass = name.to_s.singularize.camelize.constantize
  # klass.make(args)
  OpenStruct.new(klass.make(args).attributes)
end

#wipe(name, ids = []) ⇒ Object

Wipe a database table completely or based on an array of ids



3
4
5
6
7
# File 'lib/blindfold/helpers/db.rb', line 3

def wipe(name, ids=[])
  ids = Array(ids) # allow a single id to be passed in
  klass = name.to_s.singularize.camelize.constantize
  ids.empty? ? klass.delete_all : klass.delete_all(['id IN (?)', ids])
end