Module: Fixjour

Extended by:
Definitions
Includes:
Counter
Defined in:
lib/fixjour/errors.rb,
lib/fixjour/verify.rb,
lib/fixjour/counter.rb,
lib/fixjour/builders.rb,
lib/fixjour/generator.rb,
lib/fixjour/attributes.rb,
lib/fixjour/definitions.rb,
lib/fixjour/deprecation.rb,
lib/fixjour/merging_proxy.rb,
lib/fixjour/overrides_hash.rb,
lib/fixjour/redundant_check.rb

Defined Under Namespace

Modules: Counter, Definitions, Deprecation, RedundancyChecker Classes: Attributes, BuilderSavedRecord, DangerousBuilder, DeprecatedMergeAttempt, Generator, InvalidBuilder, MergingProxy, NonBlockBuilderReference, OverridesHash, RedundantBuilder, UnsavableBuilder, WrongBuilderType

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Definitions

define_create, define_new, define_valid_attributes

Methods included from Counter

#counter, counter, reset

Class Attribute Details

.allow_redundancyObject

Returns the value of attribute allow_redundancy.



6
7
8
# File 'lib/fixjour/builders.rb', line 6

def allow_redundancy
  @allow_redundancy
end

Class Method Details

.builder_defined?(builder) ⇒ Boolean

Checks to see whether or not a builder is defined. Duh.

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/fixjour/builders.rb', line 46

def builder_defined?(builder)
  case builder
  when Class          then builders.include?(builders)
  when String, Symbol then builders.map(&:name).include?(builder)
  end
end

.buildersObject

The list of classes that have builders defined.



9
10
11
# File 'lib/fixjour/builders.rb', line 9

def builders
  @builders ||= Set.new
end

.define_builder(klass, options = {}, &block) ⇒ Object

This method should always return a valid instance of a model object.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fixjour/builders.rb', line 15

def define_builder(klass, options={}, &block)
  add_builder(klass)

  if block_given?
    define_new(klass, &block)
  else
    define_new(klass) do |proxy, overrides|
      proxy.new(options.merge(overrides))
    end
  end

  name = name_for(klass)

  define_create(name)
  define_valid_attributes(name, options)
end

.evaluate(&block) ⇒ Object

Adds builders to Fixjour.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fixjour/builders.rb', line 33

def evaluate(&block)
  begin
    module_eval(&block)
  rescue NameError => e
    if e.name && evaluator.respond_to?(e.name)
      raise NonBlockBuilderReference.new("You must use a builder block in order to reference other Fixjour creation methods.")
    else
      raise e
    end
  end
end

.included(klass) ⇒ Object



2
3
4
# File 'lib/fixjour/redundant_check.rb', line 2

def self.included(klass)
  klass.extend(RedundancyChecker)
end

.new_record(klass) ⇒ Object



44
45
46
# File 'lib/fixjour/verify.rb', line 44

def new_record(klass)
  evaluator.send("new_#{name_for(klass)}")
end

.verify!Object

Checks each builder to ensure that they:

  • Return valid objects

  • The new_* methods return new records

  • The creation methods return objects of the proper type



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fixjour/verify.rb', line 8

def verify!
  builders.each do |klass|
    result = new_record(klass)

    unless result.valid?
      error(klass, InvalidBuilder, "returns an invalid object: #{result.errors.inspect}")
    end

    unless result.new_record?
      error(klass, BuilderSavedRecord, "must return a new record")
    end

    unless result.is_a?(klass)
      error(klass, WrongBuilderType, "must return an instance of #{klass}")
    end

    klass.transaction do
      begin
        result.save!
      rescue => e
        error(klass, UnsavableBuilder, "raises #{e.inspect} when saved to the database")
      end
    
      unless new_record(klass).valid?
        msg = ""
        msg << "returns invalid an invalid object after another object has been saved.\n"
        msg << "This could be caused by a validates_uniqueness_of validation in your model.\n"
        msg << "Use something like the faker gem to alleviate this issue."
        error(klass, DangerousBuilder, msg)
      end
      
      raise ActiveRecord::Rollback
    end
  end
end