Class: Guard::I18nJson

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/i18n_json.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ I18nJson

Returns a new instance of I18nJson.



14
15
16
17
18
# File 'lib/guard/i18n_json.rb', line 14

def initialize(options = {})
  @config_file = options.delete(:config_file)
  @require_file = options.delete(:require_file)
  super
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



12
13
14
# File 'lib/guard/i18n_json.rb', line 12

def config_file
  @config_file
end

#current_threadObject (readonly)

Returns the value of attribute current_thread.



12
13
14
# File 'lib/guard/i18n_json.rb', line 12

def current_thread
  @current_thread
end

#require_fileObject (readonly)

Returns the value of attribute require_file.



12
13
14
# File 'lib/guard/i18n_json.rb', line 12

def require_file
  @require_file
end

Instance Method Details

#error(message) ⇒ Object



71
72
73
# File 'lib/guard/i18n_json.rb', line 71

def error(message)
  ::Guard::UI.error "[guard-i18n-json] #{message}"
end

#export_files(changes = nil) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/guard/i18n_json.rb', line 48

def export_files(changes = nil)
  return unless validate_file(:config_file, config_file)
  return unless validate_file(:require_file, require_file)

  current_thread&.exit

  info("Changes detected: #{changes.join(', ')}") if changes

  @current_thread = Thread.new do
    require @require_file
    ::I18nJSON.call(config_file: @config_file)
  end

  current_thread.join
end

#info(message) ⇒ Object



75
76
77
# File 'lib/guard/i18n_json.rb', line 75

def info(message)
  ::Guard::UI.info "[guard-i18n-json] #{message}"
end

#reloadObject



28
29
30
# File 'lib/guard/i18n_json.rb', line 28

def reload
  export_files
end

#run_allObject



32
33
34
# File 'lib/guard/i18n_json.rb', line 32

def run_all
  export_files
end

#run_on_additions(paths) ⇒ Object



36
37
38
# File 'lib/guard/i18n_json.rb', line 36

def run_on_additions(paths)
  export_files(paths)
end

#run_on_modifications(paths) ⇒ Object



40
41
42
# File 'lib/guard/i18n_json.rb', line 40

def run_on_modifications(paths)
  export_files(paths)
end

#run_on_removals(paths) ⇒ Object



44
45
46
# File 'lib/guard/i18n_json.rb', line 44

def run_on_removals(paths)
  export_files(paths)
end

#startObject



20
21
22
# File 'lib/guard/i18n_json.rb', line 20

def start
  export_files
end

#stopObject



24
25
26
# File 'lib/guard/i18n_json.rb', line 24

def stop
  current_thread&.exit
end

#validate_file(key, file) ⇒ Object



64
65
66
67
68
69
# File 'lib/guard/i18n_json.rb', line 64

def validate_file(key, file)
  return true if file && File.file?(file)

  error("#{key.inspect} must be a file")
  false
end