Class: Rosette::Core::ExtractorConfig

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

Overview

Configuration for an extractor.

Examples:

ex = ExtractorConfig.new(JsonExtractor::KeyValueExtractor)
  .set_encoding(Encoding::UTF_8)
  .set_conditions do |root|
    root.match_file_extension('.json').and(
      root.match_path('config/locales')
    )
  end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(extractor_id, extractor_class) ⇒ ExtractorConfig

Creates a new extractor configuration.

Parameters:

  • extractor_id (String)

    The extractor id of extractor_class.

  • extractor_class (Class)

    The extractor to use.


34
35
36
37
38
39
# File 'lib/rosette/core/extractor/extractor_config.rb', line 34

def initialize(extractor_id, extractor_class)
  @extractor_id = extractor_id
  @extractor = extractor_class.new(self)
  @root = PathMatcherFactory.create_root
  @encoding = Rosette::Core::DEFAULT_ENCODING
end

Instance Attribute Details

#encodingString, Encoding (readonly)

Returns the encoding to expect the contents of source files to be in.

Returns:

  • (String, Encoding)

    the encoding to expect the contents of source files to be in.


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
70
71
72
# File 'lib/rosette/core/extractor/extractor_config.rb', line 27

class ExtractorConfig
  attr_reader :extractor_id, :extractor, :encoding, :root

  # Creates a new extractor configuration.
  #
  # @param [String] extractor_id The extractor id of +extractor_class+.
  # @param [Class] extractor_class The extractor to use.
  def initialize(extractor_id, extractor_class)
    @extractor_id = extractor_id
    @extractor = extractor_class.new(self)
    @root = PathMatcherFactory.create_root
    @encoding = Rosette::Core::DEFAULT_ENCODING
  end

  # Sets the encoding to expect the contents of source files to be in.
  #
  # @param [String, Encoding] new_encoding the encoding to use.
  # @return [self]
  def set_encoding(new_encoding)
    @encoding = new_encoding
    self
  end

  # Determines if the given path matches all the conditions in the
  # conditions tree.
  #
  # @param [String] path The path to match.
  # @return [Boolean] true if the path matches, false otherwise.
  def matches?(path)
    root.matches?(path)
  end

  # Creates and yields a node that represents the root of a conditions
  # tree. Callers should use the yielded root to build up a set of
  # conditions that will be used to match paths in the repository.
  # Matching paths will be processed by the extractor (i.e. their
  # translatable phrases will be identified and stored).
  #
  # @return [self]
  # @yield [root] the root of the conditions tree
  # @yieldparam root [PathMatcherFactory::Node]
  def set_conditions
    @root = yield PathMatcherFactory.create_root
    self
  end
end

#extractorExtractor (readonly)

Returns the extractor instance that will be used to extract phrases.

Returns:

  • (Extractor)

    the extractor instance that will be used to extract phrases.


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
70
71
72
# File 'lib/rosette/core/extractor/extractor_config.rb', line 27

class ExtractorConfig
  attr_reader :extractor_id, :extractor, :encoding, :root

  # Creates a new extractor configuration.
  #
  # @param [String] extractor_id The extractor id of +extractor_class+.
  # @param [Class] extractor_class The extractor to use.
  def initialize(extractor_id, extractor_class)
    @extractor_id = extractor_id
    @extractor = extractor_class.new(self)
    @root = PathMatcherFactory.create_root
    @encoding = Rosette::Core::DEFAULT_ENCODING
  end

  # Sets the encoding to expect the contents of source files to be in.
  #
  # @param [String, Encoding] new_encoding the encoding to use.
  # @return [self]
  def set_encoding(new_encoding)
    @encoding = new_encoding
    self
  end

  # Determines if the given path matches all the conditions in the
  # conditions tree.
  #
  # @param [String] path The path to match.
  # @return [Boolean] true if the path matches, false otherwise.
  def matches?(path)
    root.matches?(path)
  end

  # Creates and yields a node that represents the root of a conditions
  # tree. Callers should use the yielded root to build up a set of
  # conditions that will be used to match paths in the repository.
  # Matching paths will be processed by the extractor (i.e. their
  # translatable phrases will be identified and stored).
  #
  # @return [self]
  # @yield [root] the root of the conditions tree
  # @yieldparam root [PathMatcherFactory::Node]
  def set_conditions
    @root = yield PathMatcherFactory.create_root
    self
  end
end

#extractor_idObject (readonly)

Returns the value of attribute extractor_id


28
29
30
# File 'lib/rosette/core/extractor/extractor_config.rb', line 28

def extractor_id
  @extractor_id
end

#rootPathMatcherFactory::Node (readonly)

Returns the root of the conditions tree. Only files that are matched by the conditions in this tree will have their phrases extracted.

Returns:

  • (PathMatcherFactory::Node)

    the root of the conditions tree. Only files that are matched by the conditions in this tree will have their phrases extracted.


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
70
71
72
# File 'lib/rosette/core/extractor/extractor_config.rb', line 27

class ExtractorConfig
  attr_reader :extractor_id, :extractor, :encoding, :root

  # Creates a new extractor configuration.
  #
  # @param [String] extractor_id The extractor id of +extractor_class+.
  # @param [Class] extractor_class The extractor to use.
  def initialize(extractor_id, extractor_class)
    @extractor_id = extractor_id
    @extractor = extractor_class.new(self)
    @root = PathMatcherFactory.create_root
    @encoding = Rosette::Core::DEFAULT_ENCODING
  end

  # Sets the encoding to expect the contents of source files to be in.
  #
  # @param [String, Encoding] new_encoding the encoding to use.
  # @return [self]
  def set_encoding(new_encoding)
    @encoding = new_encoding
    self
  end

  # Determines if the given path matches all the conditions in the
  # conditions tree.
  #
  # @param [String] path The path to match.
  # @return [Boolean] true if the path matches, false otherwise.
  def matches?(path)
    root.matches?(path)
  end

  # Creates and yields a node that represents the root of a conditions
  # tree. Callers should use the yielded root to build up a set of
  # conditions that will be used to match paths in the repository.
  # Matching paths will be processed by the extractor (i.e. their
  # translatable phrases will be identified and stored).
  #
  # @return [self]
  # @yield [root] the root of the conditions tree
  # @yieldparam root [PathMatcherFactory::Node]
  def set_conditions
    @root = yield PathMatcherFactory.create_root
    self
  end
end

Instance Method Details

#matches?(path) ⇒ Boolean

Determines if the given path matches all the conditions in the conditions tree.

Parameters:

  • path (String)

    The path to match.

Returns:

  • (Boolean)

    true if the path matches, false otherwise.


55
56
57
# File 'lib/rosette/core/extractor/extractor_config.rb', line 55

def matches?(path)
  root.matches?(path)
end

#set_conditions {|root| ... } ⇒ self

Creates and yields a node that represents the root of a conditions tree. Callers should use the yielded root to build up a set of conditions that will be used to match paths in the repository. Matching paths will be processed by the extractor (i.e. their translatable phrases will be identified and stored).

Yields:

  • (root)

    the root of the conditions tree

Yield Parameters:

Returns:

  • (self)

68
69
70
71
# File 'lib/rosette/core/extractor/extractor_config.rb', line 68

def set_conditions
  @root = yield PathMatcherFactory.create_root
  self
end

#set_encoding(new_encoding) ⇒ self

Sets the encoding to expect the contents of source files to be in.

Parameters:

  • new_encoding (String, Encoding)

    the encoding to use.

Returns:

  • (self)

45
46
47
48
# File 'lib/rosette/core/extractor/extractor_config.rb', line 45

def set_encoding(new_encoding)
  @encoding = new_encoding
  self
end