Class: Peck::Rails

Inherits:
Object
  • Object
show all
Defined in:
lib/peck_on_rails.rb,
lib/peck_on_rails/model.rb,
lib/peck_on_rails/helper.rb,
lib/peck_on_rails/controller.rb,
lib/peck_on_rails/backtrace_cleaning.rb

Defined Under Namespace

Modules: BacktraceCleaning Classes: Controller, Helper, Model

Constant Summary collapse

HELPER_RE =
/Helper$/

Class Method Summary collapse

Class Method Details

.context_type(context, subject) ⇒ Object


57
58
59
60
61
62
63
# File 'lib/peck_on_rails.rb', line 57

def self.context_type(context, subject)
  context_type =
    context_type_for_description(context, subject) ||
    context_type_for_subject(context, subject)
  Peck.log("Using `#{context_type}' as context type for `#{subject.respond_to?(:name) ? subject.name : subject}'")
  context_type
end

.context_type_for_description(context, _) ⇒ Object


35
36
37
38
39
# File 'lib/peck_on_rails.rb', line 35

def self.context_type_for_description(context, _)
  context.description.find do |subject|
    subject.is_a?(Symbol)
  end
end

.context_type_for_subject(context, subject) ⇒ Object


43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/peck_on_rails.rb', line 43

def self.context_type_for_subject(context, subject)
  if subject.nil?
    :plain
  elsif defined?(ActionController) && subject < ActionController::Base
    :controller
  elsif defined?(ActiveRecord) && subject < ActiveRecord::Base
    :model
  elsif subject.name =~ HELPER_RE
    :helper
  else
    :plain
  end
end

.dev_nullObject


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

def self.dev_null
  @dev_null ||= File.open('/dev/null', 'w')
end

.initObject


65
66
67
68
69
70
71
72
73
74
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
# File 'lib/peck_on_rails.rb', line 65

def self.init
  if defined?(ActiveRecord)
    Peck.log("Migrate database if necessary")
    ActiveRecord::Migration.load_schema_if_pending!
  end
  Peck.log("Setting up Peck::Rails")
  proc do |context|
    subject      = Peck::Rails.subject(context)
    context_type = Peck::Rails.context_type(context, subject)
    [
      Peck::Rails::Helper,
      Peck::Rails::Model,
      Peck::Rails::Controller
    ].each do |klass|
      klass.send(:init, context, context_type, subject)
    end

    context.before do
      if respond_to?(:setup_fixtures)
        begin
          setup_fixtures
        rescue ActiveRecord::ConnectionNotEstablished
        end
      end
    end

    context.after do
      if respond_to?(:teardown_fixtures)
        begin
          teardown_fixtures
        rescue ActiveRecord::ConnectionNotEstablished
        end
      end
    end
  end
end

.silenceObject


21
22
23
24
25
26
27
28
29
# File 'lib/peck_on_rails.rb', line 21

def self.silence
  stdout, stderr = $stdout, $stderr
  $stdout, $stderr = dev_null, dev_null
  begin
    yield
  ensure
    $stdout, $stderr = stdout, stderr
  end
end

.subject(context) ⇒ Object


31
32
33
# File 'lib/peck_on_rails.rb', line 31

def self.subject(context)
  context.description.find { |a| a.is_a?(Module) }
end