Module: Ngannotate::ProcessorCommon

Included in:
Processor
Defined in:
lib/ngannotate/processor_common.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
# File 'lib/ngannotate/processor_common.rb', line 3

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#configObject



44
45
46
# File 'lib/ngannotate/processor_common.rb', line 44

def config
  ::Rails.configuration.ng_annotate
end

#exec_contextObject



40
41
42
# File 'lib/ngannotate/processor_common.rb', line 40

def exec_context
  @exec_context ||= self.class.shared_exec_context
end

#ignore_file?(input) ⇒ Boolean

Returns true if current file is ignored.

Returns:

  • (Boolean)

    true if current file is ignored



71
72
73
# File 'lib/ngannotate/processor_common.rb', line 71

def ignore_file?(input)
  match_input(config.ignore_paths, input)
end

#loggerObject



22
23
24
# File 'lib/ngannotate/processor_common.rb', line 22

def logger
  ::Rails.logger
end

#match_input(paths, input) ⇒ Object

Returns truthy value if input matches any of paths.

Returns:

  • truthy value if input matches any of paths



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ngannotate/processor_common.rb', line 78

def match_input(paths, input)
  file = input[:filename]
  paths.any? do |p|
    if p.is_a? Proc
      p.call(file)
    elsif p.is_a? Regexp
      p.match(file)
    else
      file.index(p)
    end
  end
end

#need_process?(input) ⇒ Boolean

Is processing done for current file. This is determined by 4 checks

  • config.paths

  • config.ignore_paths

  • config.process

  • NG_FORCE=true env option

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/ngannotate/processor_common.rb', line 56

def need_process?(input)
  force_process = ENV['NG_FORCE'] == 'true'
  process_file?(input) && (force_process || config.process)
end

#parse_ngannotate_optionsObject

Parse extra options for ngannotate



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ngannotate/processor_common.rb', line 94

def parse_ngannotate_options
  opt = config.options.clone

  if ENV['NG_OPT']
    opt_str = ENV['NG_OPT']
    if opt_str
      opt = Hash[opt_str.split(',').map { |e| e.split('=') }]
      opt.symbolize_keys!
    end
  end

  regexp = ENV['NG_REGEXP']
  if regexp
    opt[:regexp] = regexp
  end

  opt
end

#process_data(input) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ngannotate/processor_common.rb', line 26

def process_data(input)
  data = input[:data]
  file = input[:filename]

  handle = need_process?(input)
  state = handle ? :process : (ignore_file?(input) ? :ignore : :skip)
  logger.info "ng-#{state}: #{file}"
  return data unless handle

  opt = { add: true }.merge!(parse_ngannotate_options)
  r = exec_context.call 'window.annotate', data, opt
  r['src']
end

#process_file?(input) ⇒ Boolean

Returns true if current file should be processed.

Returns:

  • (Boolean)

    true if current file should be processed



64
65
66
# File 'lib/ngannotate/processor_common.rb', line 64

def process_file?(input)
  !ignore_file?(input) && match_input(config.paths, input)
end