Module: ModelGrinder::ClassMethods

Included in:
ModelGrinder
Defined in:
lib/model_grinder/class_methods.rb

Overview

Please see the documentation for the module ModelGrinder for full documentation.

Instance Method Summary collapse

Instance Method Details

#build(*args) ⇒ Class

Generates a new hash from a template on a class, creates a new object, and persists it to the datastore. Class must respond to new, accept a hash as the first and only argument, and respond to save.

Parameters:

  • klass (Class)

    The class that’s attached to the template (optional)

  • name (Symbol)

    The name of the template (optional, defaults to :default)

  • override_attrs (Hash)

    Values to assign to the hash irrespective of the template

Returns:

  • (Class)


85
86
87
88
89
# File 'lib/model_grinder/class_methods.rb', line 85

def build(*args)
  model = generate(*args)
  model.save
  model
end

#gen_hash(*args) ⇒ Object Also known as: genh

Generates a new hash from a template found by the Class and name passed. A bit of magic is used to make the first argument optional on all methods.

Parameters:

  • klass (Class)

    The class that’s attached to the template (optional)

  • name (Symbol)

    The name of the template (optional, defaults to :default)

  • override_attrs (Hash)

    Values to assign to the hash irrespective of the template

Raises:

  • (ArgumentError)

    if template does not exist.



36
37
38
39
40
41
42
# File 'lib/model_grinder/class_methods.rb', line 36

def gen_hash(*args)
  klass, name, override_attrs = parse_args(args)
  raise ArgumentError.new("The template `#{name}` for `#{klass}` does not exist.") unless _mg_templates[klass] && _mg_templates[klass][name]
  attrs = _mg_templates[klass][name].call
  attrs.merge!(override_attrs) if override_attrs
  attrs
end

#generate(*args) ⇒ Class Also known as: gen, make

Generates a new hash from a template on a class and creates a new object. Class must respond to new and accept a hash as the first and only argument.

Parameters:

  • klass (Class)

    The class that’s attached to the template (optional)

  • name (Symbol)

    The name of the template (optional, defaults to :default)

  • override_attrs (Hash)

    Values to assign to the hash irrespective of the template

Returns:

  • (Class)


52
53
54
55
56
57
58
59
60
# File 'lib/model_grinder/class_methods.rb', line 52

def generate(*args)
  klass, name, attrs = parse_args(args)
  # This returns the model instance
  _mg_store_generated(
      klass,
      name,
      klass.new(gen_hash(*args) || {})
  )
end

#pick(*args) ⇒ Array

Picks a specified number of already generated models

Parameters:

  • klass (Class)

    The class that’s attached to the template (optional)

  • name (Symbol)

    The name of the template (optional, defaults to :default)

  • options (Hash)

    Currently the only option is number of models returned (e.g. number: 3)

Returns:

  • (Array)

    An array of the number of generated models, up to the number actually generated.



72
73
74
75
76
77
# File 'lib/model_grinder/class_methods.rb', line 72

def pick(*args)
  klass, name, hash = parse_args(args)
  hash[:number] ||= 1
  return [] unless _mg_generated[klass].is_a?(Hash) && _mg_generated[klass][name].is_a?(Array)
  _mg_generated[klass][name].sample hash[:number]
end

#template(*args, &blk) ⇒ Proc Also known as: fix, fixture, t

Sets up a template that’s a block which contains a hash (for pseudo-random generation later). A bit of magic is used to make the first argument optional on all methods.

Parameters:

  • klass (Class)

    The class that’s attached to the template (optional)

  • name (Symbol)

    The name of the template (optional, defaults to :default)

Returns:

  • (Proc)

    The passed block

Raises:

  • (ArgumentError)

    on no block passed



15
16
17
18
19
20
21
# File 'lib/model_grinder/class_methods.rb', line 15

def template(*args, &blk)
  #TODO: check to make sure block returns hash
  raise ArgumentError.new('A block that contains a hash must be passed to template.') unless block_given?
  klass, name = parse_args(args)
  _mg_templates[klass] ||= {}
  _mg_templates[klass][name] = blk
end