Module: ViewSpec::Utils

Defined in:
lib/view_spec/utils.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_class(name, root = Object, base_class: nil, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/view_spec/utils.rb', line 21

def create_class(name, root = Object, base_class: nil, &block)
  namespace_parts = name.camelize.split("::")
  class_name = namespace_parts.pop
  namespace = namespace_parts.join("::")

  scope = create_namespace(namespace, root)
  scope.const_set(class_name, Class.new(base_class))

  klass = scope.const_get(class_name)
  klass.instance_exec(&block) if block
  klass
end

.create_namespace(ns, root = Object) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/view_spec/utils.rb', line 7

def create_namespace(ns, root = Object)
  module_names = ns.split("::")
  current_context = root
  module_names.each do |module_name|
    unless module_name == "ViewSpecs"
      current_context.const_set(module_name, Module.new)
    end
  rescue
  ensure
    current_context = current_context.const_get(module_name)
  end
  current_context
end

.heredoc_tag(source_line) ⇒ Object



34
35
36
37
# File 'lib/view_spec/utils.rb', line 34

def heredoc_tag(source_line)
  matches = source_line.match(/<<~?(?<tag>[A-Z_\d]+)/)
  matches[:tag] if matches
end

.normalize_paths(paths) ⇒ Object



3
4
5
# File 'lib/view_spec/utils.rb', line 3

def normalize_paths(paths)
  paths.map { File.absolute_path(_1, Rails.root) }
end

Instance Method Details

#lang_from_source_line(source_line, fallback = nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/view_spec/utils.rb', line 39

def lang_from_source_line(source_line, fallback = nil)
  if source_line.present?
    tag = Utils.heredoc_tag(source_line)
    tag.present? ? tag.downcase.to_sym : fallback
  else
    fallback
  end
end