Class: Rosette::Core::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/rosette/core/extractor/extractor.rb

Overview

Base class for extractors that extract phrases from source code, eg. Ruby, JavaScript, HAML, etc.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ Extractor

Creates a new extractor.

Parameters:

  • config (Configurator) (defaults to: nil)

    The Rosette config to use.



17
18
19
# File 'lib/rosette/core/extractor/extractor.rb', line 17

def initialize(config = nil)
  @config = config
end

Instance Attribute Details

#configConfigurator (readonly)

Returns the Rosette config to use.

Returns:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
# File 'lib/rosette/core/extractor/extractor.rb', line 11

class Extractor
  attr_reader :config

  # Creates a new extractor.
  #
  # @param [Configurator] config The Rosette config to use.
  def initialize(config = nil)
    @config = config
  end

  # Extracts each translatable phrase from the given source code.
  # Derived classes must implement the +#each_function_call+ method
  # for this to work.
  #
  # @param [String] source_code The source code to extract phrases
  #   from.
  # @return [void, Enumerator] If passed a block, this method yields
  #   each consecutive phrase found in +source_code+. If no block is
  #   passed, it returns an +Enumerator+.
  # @yield [phrase] a single extracted phrase.
  # @yieldparam phrase [Phrase]
  def extract_each_from(source_code)
    if block_given?
      each_function_call(source_code) do |node, line_number|
        if valid_name?(node) && valid_args?(node)
          yield make_phrase(get_key(node), get_meta_key(node)), line_number
        end
      end
    else
      to_enum(__method__, source_code)
    end
  end

  protected

  def each_function_call(source_code)
    raise NotImplementedError, "#{__method__} must be implemented by derived classes."
  end

  def valid_name?(node)
    raise NotImplementedError, "#{__method__} must be implemented by derived classes."
  end

  def valid_args?(node)
    raise NotImplementedError, "#{__method__} must be implemented by derived classes."
  end

  def get_key(node)
    raise NotImplementedError, "#{__method__} must be implemented by derived classes."
  end

  def get_meta_key(node)
    nil
  end

  def make_phrase(key, meta_key = nil, file = nil)
    Phrase.new(key, meta_key, file)
  end
end

Instance Method Details

#extract_each_from(source_code) {|phrase| ... } ⇒ void, Enumerator

Extracts each translatable phrase from the given source code. Derived classes must implement the #each_function_call method for this to work.

Parameters:

  • source_code (String)

    The source code to extract phrases from.

Yields:

  • (phrase)

    a single extracted phrase.

Yield Parameters:

Returns:

  • (void, Enumerator)

    If passed a block, this method yields each consecutive phrase found in source_code. If no block is passed, it returns an Enumerator.



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rosette/core/extractor/extractor.rb', line 32

def extract_each_from(source_code)
  if block_given?
    each_function_call(source_code) do |node, line_number|
      if valid_name?(node) && valid_args?(node)
        yield make_phrase(get_key(node), get_meta_key(node)), line_number
      end
    end
  else
    to_enum(__method__, source_code)
  end
end