Class: FactoryGirl::Upgrader::Transformer

Inherits:
RubyTransform::Transformer
  • Object
show all
Defined in:
lib/factory_girl/upgrader/transformer.rb

Instance Method Summary collapse

Instance Method Details

#matches_define_factory?(e) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/factory_girl/upgrader/transformer.rb', line 16

def matches_define_factory?(e)
  e.is_a?(Sexp) && e.kind == :iter &&  # We're in a block
    e.body.first.is_a?(Sexp) && e.body.first.kind == :call && # What's calling
      e.body.first.body.first == s(:const, :Factory) && e.body.first.body.second == :define &&  # Factory.define
      e.body.second.try(:kind) == :lasgn && # and we're passing a block variable for the factory
      e.body.third.try(:kind) == :block # and we have a block
end

#matches_factory_association?(e, local_variable) ⇒ Boolean

Factory Associations:

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/factory_girl/upgrader/transformer.rb', line 76

def matches_factory_association?(e, local_variable)
  e.is_a?(Sexp) && e.kind == :call && e.body.first.kind == :lvar && e.body.first.body.first == local_variable.to_sym &&
    e.body.second == :association
end

#matches_factory_attribute?(e, local_variable) ⇒ Boolean

Factory Attributes:

Returns:

  • (Boolean)


54
55
56
# File 'lib/factory_girl/upgrader/transformer.rb', line 54

def matches_factory_attribute?(e, local_variable)
  e.is_a?(Sexp) && e.kind == :call && e.body.first.kind == :lvar && e.body.first.body.first == local_variable.to_sym
end

#matches_factory_sequence?(e, local_variable) ⇒ Boolean

Factory Sequences:

Returns:

  • (Boolean)


65
66
67
# File 'lib/factory_girl/upgrader/transformer.rb', line 65

def matches_factory_sequence?(e, local_variable)
  e.is_a?(Sexp) && e.kind == :iter && matches_factory_attribute?(e.body.first, local_variable)
end

#transform(e) ⇒ Object



4
5
6
# File 'lib/factory_girl/upgrader/transformer.rb', line 4

def transform(e)
  super(transform_factories(e))
end

#transform_factories(e) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/factory_girl/upgrader/transformer.rb', line 8

def transform_factories(e)
  if matches_define_factory?(e)
    transform_factory_definition(e)
  else
    e
  end
end

#transform_factory_association(e) ⇒ Object



81
82
83
84
85
# File 'lib/factory_girl/upgrader/transformer.rb', line 81

def transform_factory_association(e)
  association_name = e.body.third.body.first.body.first
  
  s(:call, nil, association_name, s(:arglist))
end

#transform_factory_attribute(e) ⇒ Object



58
59
60
61
62
# File 'lib/factory_girl/upgrader/transformer.rb', line 58

def transform_factory_attribute(e)
  e.dup.tap do |sexp|
    sexp[1] = nil
  end
end

#transform_factory_definition(e) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/factory_girl/upgrader/transformer.rb', line 24

def transform_factory_definition(e)
  factory_name = e.body.first.body.third.body.first.body.first  # Yeah... we really need an OO AST model.
  local_variable = e.body.second.body.first
  block = e.body.third
  
  s(:iter,
    s(:call, s(:const, :FactoryGirl), :define, s(:arglist)), nil,
    s(:iter,
      s(:call, nil, :factory, s(:arglist, s(:lit, factory_name.to_sym))), nil,
      transform_factory_definition_block(block, local_variable)
    )
  )
end

#transform_factory_definition_block(block, local_variable) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/factory_girl/upgrader/transformer.rb', line 38

def transform_factory_definition_block(block, local_variable)
  Sexp.new(*([:block] + block.body.map {|e|
    case
    when matches_factory_association?(e, local_variable)
      transform_factory_association(e)
    when matches_factory_sequence?(e, local_variable)
      transform_factory_sequence(e)
    when matches_factory_attribute?(e, local_variable)
      transform_factory_attribute(e)
    else
      e
    end
  }))
end

#transform_factory_sequence(e) ⇒ Object



69
70
71
72
73
# File 'lib/factory_girl/upgrader/transformer.rb', line 69

def transform_factory_sequence(e)
  e.dup.tap do |sexp|
    sexp[1] = transform_factory_attribute(sexp[1])
  end
end