Class: RailsDevTweaks::Configuration::GranularAutoloadConfiguration

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_dev_tweaks/configuration.rb

Instance Method Summary collapse

Constructor Details

#initializeGranularAutoloadConfiguration

Returns a new instance of GranularAutoloadConfiguration.



31
32
33
34
# File 'lib/rails_dev_tweaks/configuration.rb', line 31

def initialize
  # Each rule is a simple pair: [:skip, callable], [:keep, callable], etc.
  @rules = []
end

Instance Method Details

#append_rule(rule_type, *args, &block) ⇒ Object

Raises:

  • (TypeError)


44
45
46
47
48
49
50
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/rails_dev_tweaks/configuration.rb', line 44

def append_rule(rule_type, *args, &block)
  unless rule_type == :skip || rule_type == :keep
    raise TypeError, "Rule must be :skip or :keep.  Got: #{rule_type.inspect}"
  end

  # Simple matcher blocks
  if args.size == 0 && block.present?
    @rules.unshift [rule_type, block]
    return self
  end

  # String match shorthand
  args[0] = /^#{args[0]}/ if args.size == 1 && args[0].kind_of?(String)

  # Regex match shorthand
  args = [:path, args[0]] if args.size == 1 && args[0].kind_of?(Regexp)

  if args.size == 0 && block.blank?
    raise TypeError, 'Cannot process autoload rule as specified.  Expecting a named matcher (symbol), path prefix (string) or block'
  end

  # Named matcher
  matcher_class = "RailsDevTweaks::GranularAutoload::Matchers::#{args[0].to_s.classify}Matcher".constantize
  matcher       = matcher_class.new(*args[1..-1])
  raise TypeError, "Matchers must respond to :call. #{matcher.inspect} does not." unless matcher.respond_to? :call

  @rules.unshift [rule_type, matcher]

  self
end

#keep(*args, &block) ⇒ Object



36
37
38
# File 'lib/rails_dev_tweaks/configuration.rb', line 36

def keep(*args, &block)
  self.append_rule(:keep, *args, &block)
end

#should_reload?(request) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
82
83
# File 'lib/rails_dev_tweaks/configuration.rb', line 75

def should_reload?(request)
  @rules.each do |rule_type, callable|
    return rule_type == :keep if callable.call(request)
  end

  # We default to reloading to preserve behavior, but we should never get to this unless the configuration is
  # all sorts of horked.
  true
end

#skip(*args, &block) ⇒ Object



40
41
42
# File 'lib/rails_dev_tweaks/configuration.rb', line 40

def skip(*args, &block)
  self.append_rule(:skip, *args, &block)
end