class Terrestrial::Cli::Editor::Storyboard

Constants

QUERIES

Public Class Methods

add_import(file) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 20
def self.add_import(file)
  # Not needed
  # Override parent class implementation
end
find_and_edit_line(approved_string) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 15
def self.find_and_edit_line(approved_string)
  insert_runtime_attribute(approved_string)
end
insert_runtime_attribute(entry) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 37
def self.insert_runtime_attribute(entry)
  editor = self.new(entry)
  editor.insert_attribute
  editor.save_document
end
new(entry) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 25
def initialize(entry)
  @path          = entry.file
  @document      = REXML::Document.new(File.new(@path))

  @type          = entry.type
  @string        = entry.string
  @storyboard_id = entry.metadata["storyboard_element_id"]
  @identifier    = entry.identifier

  set_document_to_double_quotes
end

Public Instance Methods

find_node(id, type) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 60
def find_node(id, type)
  REXML::XPath.first(@document, query_for(type, storyboard_id: id))
end
format_document() click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 71
def format_document
  printer = CustomPrinter.new(4)
  printer.width = 1000
  printer.compact = true
  printer.write(@document.root, "")
end
insert_attribute() click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 43
def insert_attribute
  node = find_node(@storyboard_id, @type)

  # TODO, There was a case when "node" was nil in this point, after
  # trying to find it by type + ID.
  #
  # Keep an eye out for it to see if reproducible

  if node.nil?
    puts "Warning: Was not able to find #{@type} with string '#{@string}' and ID '#{@storyboard_id}' in #{@path}."
    puts "It will not be added to your Localizable.strings file automatically."
  else
    node.add(create_element)
  end
  refresh_document(node)
end
save_document() click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 64
def save_document
  File.open(@path, "w") do |f|
    f.write "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
    f.write format_document
  end 
end

Private Instance Methods

create_element() click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 103
def create_element
  REXML::Element.new("userDefinedRuntimeAttributes")
        .add_element("userDefinedRuntimeAttribute", 
             {"type" => "boolean", 
              "keyPath" => "Terrestrial", 
              "value" => "YES" }).parent
        .add_element("userDefinedRuntimeAttribute", 
             {"type" => "string", 
              "keyPath" => "Identifier", 
              "value" => @identifier }).parent
end
query_for(type, storyboard_id: "") click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 88
def query_for(type, storyboard_id: "")
  # Find element of said type, with the ID, that does not
  # have a userDefinedRuntimeAttribute as a child

  QUERIES[type] + "[@id=\"#{storyboard_id}\" and not(userDefinedRuntimeAttributes[@Terrestrial])]"
end
refresh_document(node) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 80
def refresh_document(node)
  # This is a bit ridiculous, but necessary because
  # &*£(*$)£@*$!£ REXML
  
  @document = REXML::Document.new(node.document.to_s)
  set_document_to_double_quotes
end
set_document_to_double_quotes() click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 99
def set_document_to_double_quotes
  @document.context[:attribute_quote] = :quote
end
text_attribute(type) click to toggle source
# File lib/terrestrial/cli/editor/storyboard.rb, line 95
def text_attribute(type)
  Parser::Storyboard::Engine::TEXT_ATTRIBUTE[type]
end