Module: StrictIvars

Defined in:
lib/strict_ivars.rb,
lib/strict_ivars/version.rb

Defined Under Namespace

Classes: BaseProcessor, Configuration, NameError, Processor

Constant Summary collapse

EMPTY_ARRAY =
[].freeze
EVERYTHING =
["**/*"].freeze
METHOD_METHOD =
Module.instance_method(:method)
CONFIG =
Configuration.new
VERSION =
"1.0.2"

Class Method Summary collapse

Class Method Details

.__eval_block_from_forwarding__(&block) ⇒ Object

: () { () -> void } -> Proc



92
93
94
# File 'lib/strict_ivars.rb', line 92

def self.__eval_block_from_forwarding__(*, &block)
  block
end

.__process_eval_args__(receiver, method_name, *args) ⇒ Object

For internal use only. This method pre-processes arguments to an eval method. : (Object, Symbol, *untyped)



53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/strict_ivars.rb', line 53

def self.__process_eval_args__(receiver, method_name, *args)
  method = METHOD_METHOD.bind_call(receiver, method_name)
  owner = method.owner

  source, file = nil

  case method_name
  when :class_eval, :module_eval
    if Module == owner
      source, file = args
    end
  when :instance_eval
    if BasicObject == owner
      source, file = args
    end
  when :eval
    if Kernel == owner
      source, binding, file = args
    elsif Binding == owner
      source, file = args
    end
  end

  if String === source
    file ||= caller_locations(1, 1).first.path

    if CONFIG.match?(file)
      args[0] = Processor.call(source)
    else
      args[0] = BaseProcessor.call(source)
    end
  end

  args
rescue ::NameError
  args
end

.init(include: EMPTY_ARRAY, exclude: EMPTY_ARRAY) ⇒ Object

Initializes StrictIvars so that code loaded after this point will be guarded against undefined instance variable reads. You can pass an array of globs to ‘include:` and `exclude:`.

“‘ruby StrictIvars.init(

include: ["#{Dir.pwd}/**/*"],
exclude: ["#{Dir.pwd}/vendor/**/*"]

) “‘ : (include: Array, exclude: Array) -> void



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/strict_ivars.rb', line 33

def self.init(include: EMPTY_ARRAY, exclude: EMPTY_ARRAY)
  CONFIG.include(*include)
  CONFIG.exclude(*exclude)

  RequireHooks.source_transform(
    patterns: EVERYTHING,
    exclude_patterns: EMPTY_ARRAY
  ) do |path, source|
    source ||= File.read(path)

    if CONFIG.match?(path)
      Processor.call(source)
    else
      BaseProcessor.call(source)
    end
  end
end