Class: Maxiskirt

Inherits:
Struct
  • Object
show all
Defined in:
lib/maxiskirt.rb,
lib/maxiskirt.rb,
lib/maxiskirt/version.rb
more...

Overview

Factory girl, relaxed.

Factory.define :user do |f|
  f. 'johndoe%d'                          # Sequence.
  f.email '%{login}@example.com'               # Interpolate.
  f.password f.password_confirmation('foobar') # Chain.
end

Factory.define :post do |f|
  f.user { Factory :user }                     # Blocks, if you must.
end

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, value = nil, &block) ⇒ Object

Capture method calls, and save it to factory attributes

[View source]

116
117
118
119
# File 'lib/maxiskirt.rb', line 116

def method_missing(name, value = nil, &block)
  __params__.merge!(name => block || value)
  value # Return value to be able to use chaining like: f.password f.password_confirmation("something")
end

Instance Attribute Details

#__klass__Object

Returns the value of attribute __klass__

Returns:

  • (Object)

    the current value of __klass__


17
18
19
# File 'lib/maxiskirt.rb', line 17

def __klass__
  @__klass__
end

#__name__Object

Returns the value of attribute __name__

Returns:

  • (Object)

    the current value of __name__


17
18
19
# File 'lib/maxiskirt.rb', line 17

def __name__
  @__name__
end

#__params__Object

Returns the value of attribute __params__

Returns:

  • (Object)

    the current value of __params__


17
18
19
# File 'lib/maxiskirt.rb', line 17

def __params__
  @__params__
end

#__parent__Object

Returns the value of attribute __parent__

Returns:

  • (Object)

    the current value of __parent__


17
18
19
# File 'lib/maxiskirt.rb', line 17

def __parent__
  @__parent__
end

Class Method Details

.attributes_for(name, params_for_replace = {}) ⇒ Object

[View source]

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/maxiskirt.rb', line 75

def attributes_for(name, params_for_replace = {})
  params_for_replace = params_for_replace.symbolize_keys

  factory = @factories[name.to_s]

  klass  = factory.__klass__
  parent = factory.__parent__
  params = factory.__params__

  parent_params = if parent
    @factories[parent.to_s].__params__
  else
    {}
  end

  attributes = {}

  merged_params = parent_params.merge(params).merge(params_for_replace)
  merged_params.each do |name, value|
    attributes[name] = case value
    when String
      value.sub(/%\d*d/) { |d|
        d % (@sequence[klass] += 1)
      } % attributes
    when Proc
      value.call
    else
      value.duplicable? ? value.dup : value
    end
  end

  return attributes
end

.build(name, params = {}) ⇒ Object

Initialize and setup class from factory.

You can override default factory settings, by passing them as second argument.

[View source]

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/maxiskirt.rb', line 51

def build(name, params = {})
  factory    = @factories[name.to_s]

  klass      = factory.__klass__
  parent     = factory.__parent__
  attributes = attributes_for(name, params)

  # If parent set, then merge parent template with current template
  if parent
    klass = @factories[parent.to_s].__klass__
  end

  # Convert klass to real Class
  klass = klass.is_a?(Class) ? klass : klass.to_s.classify.constantize

  object = klass.new

  attributes.each do |name, value|
    object.send(:"#{name}=", value)
  end

  return object
end

.create(name, params = {}) ⇒ Object

Create and save new factory product

[View source]

110
111
112
# File 'lib/maxiskirt.rb', line 110

def create(name, params = {})
  build(name, params).tap { |record| record.save! }
end

.define(name, options = {}) {|| ... } ⇒ Object

Define new factory with given name. New instance of Maxiskirt will be passed as argument to given block.

Options are:

  • class - name of class to be instantiated. By default is same as name

  • parent - name of parent factory

Yields:

  • ()
[View source]

35
36
37
38
39
40
41
42
43
44
45
# File 'lib/maxiskirt.rb', line 35

def define(name, options = {})
  name = name.to_s

  # Get class name from options or use name
  klass = options.delete(:class) || name
  parent = options.delete(:parent)

  @factories[name] = new(name, klass, parent, {})

  yield(@factories[name])
end