Class: Haml::I18n::Extractor

Inherits:
Object
  • Object
show all
Defined in:
lib/haml-i18n-extractor/helpers.rb,
lib/haml-i18n-extractor/version.rb,
lib/haml-i18n-extractor/prompter.rb,
lib/haml-i18n-extractor/workflow.rb,
lib/haml-i18n-extractor/extractor.rb,
lib/haml-i18n-extractor/yaml_tool.rb,
lib/haml-i18n-extractor/haml_reader.rb,
lib/haml-i18n-extractor/haml_writer.rb,
lib/haml-i18n-extractor/text_finder.rb,
lib/haml-i18n-extractor/user_action.rb,
lib/haml-i18n-extractor/tagging_tool.rb,
lib/haml-i18n-extractor/text_replacer.rb,
lib/haml-i18n-extractor/cli.rb

Defined Under Namespace

Modules: Helpers Classes: AbortFile, CLI, HamlReader, HamlWriter, InvalidSyntax, NotADirectory, NotDefinedLineType, Prompter, TaggingTool, TextFinder, TextReplacer, UserAction, Workflow, YamlTool

Constant Summary collapse

VERSION =
"0.3.2"
LINE_TYPES_ALL =
[:text, :not_text, :loud, :silent, :element]
LINE_TYPES_ADD_EVAL =
[:text, :element]
DEFAULT_LINE_LOCALE_HASH =
{ :modified_line => nil,:keyname => nil,:replaced_text => nil, :path => nil }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(haml_path, opts = {}) ⇒ Extractor

Returns a new instance of Extractor.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/haml-i18n-extractor/extractor.rb', line 22

def initialize(haml_path, opts = {})
  @options = opts
  @type = @options[:type]
  @interactive = @options[:interactive]
  @prompter = Haml::I18n::Extractor::Prompter.new
  @haml_reader = Haml::I18n::Extractor::HamlReader.new(haml_path)
  validate_haml(@haml_reader.body)
  @haml_writer = Haml::I18n::Extractor::HamlWriter.new(haml_path, {:type => @type})
  @yaml_tool = Haml::I18n::Extractor::YamlTool.new(@options[:i18n_scope], @options[:yaml_file])
  @tagging_tool ||= Haml::I18n::Extractor::TaggingTool.new
  # hold all the processed lines
  @body = []
  # holds a line_no => {info_about_line_replacemnts_or_not}
  @locale_hash = {}
end

Instance Attribute Details

#haml_readerObject (readonly)

Returns the value of attribute haml_reader.



17
18
19
# File 'lib/haml-i18n-extractor/extractor.rb', line 17

def haml_reader
  @haml_reader
end

#haml_writerObject (readonly)

Returns the value of attribute haml_writer.



17
18
19
# File 'lib/haml-i18n-extractor/extractor.rb', line 17

def haml_writer
  @haml_writer
end

#locale_hashObject (readonly)

Returns the value of attribute locale_hash.



18
19
20
# File 'lib/haml-i18n-extractor/extractor.rb', line 18

def locale_hash
  @locale_hash
end

#typeObject (readonly)

Returns the value of attribute type.



18
19
20
# File 'lib/haml-i18n-extractor/extractor.rb', line 18

def type
  @type
end

#yaml_toolObject (readonly)

Returns the value of attribute yaml_tool.



18
19
20
# File 'lib/haml-i18n-extractor/extractor.rb', line 18

def yaml_tool
  @yaml_tool
end

Class Method Details

.debug?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/haml-i18n-extractor/extractor.rb', line 5

def self.debug?
  ENV['DEBUG']
end

Instance Method Details

#assign_new_bodyObject



45
46
47
# File 'lib/haml-i18n-extractor/extractor.rb', line 45

def assign_new_body
  @haml_writer.body = new_body
end

#assign_replacementsObject



53
54
55
56
# File 'lib/haml-i18n-extractor/extractor.rb', line 53

def assign_replacements
  assign_new_body
  assign_yaml
end

#assign_yamlObject



49
50
51
# File 'lib/haml-i18n-extractor/extractor.rb', line 49

def assign_yaml
  @yaml_tool.locale_hash = @locale_hash
end

#interactive?Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/haml-i18n-extractor/extractor.rb', line 104

def interactive?
  !!@interactive
end

#new_bodyObject



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/haml-i18n-extractor/extractor.rb', line 58

def new_body
  begin
    @haml_reader.lines.each_with_index do |orig_line, line_no|
      @current_line_no = line_no
      process_line(orig_line,line_no)
    end
  rescue AbortFile
    @prompter.moving_to_next_file
    add_rest_of_file_to_body(@current_line_no)
  end
  @body.join("\n")
end

#process_line(orig_line, line_no) ⇒ Object

this is the bulk of it: where we end up setting body info and locale_hash. not write, just set that info in memory in correspoding locations. refactor more?



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/haml-i18n-extractor/extractor.rb', line 75

def process_line(orig_line, line_no)
  orig_line.chomp!
  orig_line, whitespace = handle_line_whitespace(orig_line)
  line_type, line_match = handle_line_finding(orig_line)
  should_be_replaced, text_to_replace, line_locale_hash = gather_replacement_info(orig_line, line_match, line_type, line_no)

  user_action = Haml::I18n::Extractor::UserAction.new('y') # default if no prompting: just do it.
  if should_be_replaced
    if interactive?
      user_action = @prompter.ask_user(orig_line,text_to_replace)
    end
  end

  if user_action.tag?
    @tagging_tool.write(line_locale_hash[:path], line_no)
    add_to_body("#{whitespace}#{orig_line}")
  elsif user_action.next?
    raise AbortFile, "stopping to process the rest of the file"
 elsif user_action.replace_line?
    append_to_locale_hash(line_no, line_locale_hash)
    add_to_body("#{whitespace}#{text_to_replace}")
  elsif user_action.no_replace?
    append_to_locale_hash(line_no, DEFAULT_LINE_LOCALE_HASH)
    add_to_body("#{whitespace}#{orig_line}")
  end

  return should_be_replaced
end

#runObject



38
39
40
41
42
43
# File 'lib/haml-i18n-extractor/extractor.rb', line 38

def run
  assign_replacements
  validate_haml(@haml_writer.body)
  @haml_writer.write_file
  @yaml_tool.write_file
end