class Ovaltine::StoryboardFormatter

Constants

CELL_REUSE_SECTION_TITLE
SEGUE_SECTION_TITLE
VIEW_CONTROLLER_SECTION_TITLE

Attributes

classname[R]
storyboard[R]

Public Class Methods

new(storyboard, prefix, copyright, output_path) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 10
def initialize storyboard, prefix, copyright, output_path
  scrubbed_name = storyboard.name.gsub(/\W/,'_')
  @storyboard = storyboard
  @prefix = prefix
  @classname = "#{@prefix}#{scrubbed_name}Storyboard"
  @copyright = copyright
  @header_path = File.join(File.expand_path(output_path), "#{classname}.h")
  @impl_path = File.join(File.expand_path(output_path), "#{classname}.m")
end

Public Instance Methods

output_paths() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 20
def output_paths
  [@header_path, @impl_path]
end
write() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 24
def write
  File.open(@header_path, 'w') do |file|
    file.puts generate_header
  end
  File.open(@impl_path, 'w') do |file|
    file.puts generate_implementation
  end
end

Private Instance Methods

format(identifier, suffix_matcher, suffix, capitalize=false) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 134
def format identifier, suffix_matcher, suffix, capitalize=false
  formatted = identifier.gsub(/\W/,'_')
  unless formatted =~ /#{suffix_matcher}$/i
    formatted = "#{formatted}#{suffix}"
  end
  capitalize ? formatted.gsub(/\b\w/){ $&.upcase } : formatted
end
format_reuse_identifier(identifier) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 126
def format_reuse_identifier identifier
  format(identifier, 'identifier', 'ReuseIdentifier')
end
format_segue_identifier(identifier) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 130
def format_segue_identifier identifier
  format(identifier, 'segue|(segue)?identifier', 'SegueIdentifier')
end
format_view_controller(identifier) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 122
def format_view_controller identifier
  format(identifier, 'controller', 'ViewController', true)
end
generate_header() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 43
def generate_header
  StoryboardTemplates::HEADER_TEMPLATE\
    .gsub('{FILENAME}', "#{classname}.h")\
    .gsub('{CLASS_NAME}', classname)\
    .gsub('{REUSE_IDENTIFIERS}', reuse_identifier_definitions)\
    .gsub('{SEGUE_IDENTIFIERS}', segue_identifier_definitions)\
    .gsub('{VIEW_CONTROLLERS}', view_controller_definitions)\
    .gsub('{COPYRIGHT}', copyright_text)
end
generate_implementation() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 53
def generate_implementation
  StoryboardTemplates::IMPLEMENTATION_TEMPLATE\
    .gsub('{FILENAME}', "#{classname}.m")\
    .gsub('{CLASS_NAME}', classname)\
    .gsub('{STATIC_VARIABLES}', static_variables)\
    .gsub('{REUSE_IDENTIFIERS}', reuse_identifier_implementations)\
    .gsub('{SEGUE_IDENTIFIERS}', segue_identifier_implementations)\
    .gsub('{VIEW_CONTROLLERS}', view_controller_implementations)\
    .gsub('{COPYRIGHT}', copyright_text)\
    .gsub('{STORYBOARD}', StoryboardTemplates::STORYBOARD_IMPLEMENTATION_TEMPLATE.gsub('{IDENTIFIER_CONSTANT_NAME}', variable_name(storyboard.name)))
end
prepend_title(title, text) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 142
def prepend_title(title, text)
  if text.length > 0
    title = StoryboardTemplates::STORYBOARD_SECTION_TITLE_TEMPLATE.gsub('{TITLE}', title)
    "#{title}\n" + text
  else
    text
  end
end
reuse_identifier_definitions() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 76
def reuse_identifier_definitions
  prepend_title(CELL_REUSE_SECTION_TITLE, storyboard.cell_reuse_identifiers.each_with_index.map do |identifier, index|
    StoryboardTemplates::REUSE_DEFINITION_TEMPLATE.gsub('{IDENTIFIER}', format_reuse_identifier(identifier))
  end.join("\n"))
end
reuse_identifier_implementations() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 82
def reuse_identifier_implementations
  prepend_title(CELL_REUSE_SECTION_TITLE, storyboard.cell_reuse_identifiers.sort.each_with_index.map do |identifier, index|
    StoryboardTemplates::REUSE_IMPLEMENTATION_TEMPLATE\
      .gsub('{IDENTIFIER}', format_reuse_identifier(identifier))\
      .gsub('{IDENTIFIER_CONSTANT_NAME}', variable_name(identifier))
  end.join("\n"))
end
segue_identifier_definitions() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 90
def segue_identifier_definitions
  prepend_title(SEGUE_SECTION_TITLE, storyboard.segue_identifiers.sort.each_with_index.map do |identifier, index|
    StoryboardTemplates::SEGUE_DEFINITION_TEMPLATE.gsub('{IDENTIFIER}', format_segue_identifier(identifier))
  end.join("\n"))
end
segue_identifier_implementations() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 96
def segue_identifier_implementations
  prepend_title(SEGUE_SECTION_TITLE, storyboard.segue_identifiers.sort.each_with_index.map do |identifier, index|
    StoryboardTemplates::SEGUE_IMPLEMENTATION_TEMPLATE\
      .gsub('{IDENTIFIER}', format_segue_identifier(identifier))\
      .gsub('{IDENTIFIER_CONSTANT_NAME}', variable_name(identifier))
  end.join("\n"))
end
static_variables() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 65
def static_variables
  identifiers = (storyboard.cell_reuse_identifiers +
    storyboard.view_controller_identifiers +
    storyboard.segue_identifiers + [storyboard.name]).sort.uniq
  identifiers.map do |identifier|
    StoryboardTemplates::STATIC_IDENTIFIER_TEMPLATE\
      .gsub('{IDENTIFIER}', identifier)\
      .gsub('{IDENTIFIER_CONSTANT_NAME}', variable_name(identifier))
  end.join("\n")
end
variable_name(identifier) click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 118
def variable_name identifier
  "_#{@prefix}#{identifier.gsub(/\W/,'').gsub(/\b\w/){ $&.downcase }}"
end
view_controller_definitions() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 104
def view_controller_definitions
  prepend_title(VIEW_CONTROLLER_SECTION_TITLE, storyboard.view_controller_identifiers.sort.each_with_index.map do |identifier, index|
    StoryboardTemplates::VIEW_CONTROLLER_DEFINITION_TEMPLATE.gsub('{IDENTIFIER}', format_view_controller(identifier))
  end.join("\n"))
end
view_controller_implementations() click to toggle source
# File lib/ovaltine/objc/storyboard_formatter.rb, line 110
def view_controller_implementations
  prepend_title(VIEW_CONTROLLER_SECTION_TITLE, storyboard.view_controller_identifiers.sort.each_with_index.map do |identifier, index|
    StoryboardTemplates::VIEW_CONTROLLER_IMPLEMENTATION_TEMPLATE\
      .gsub('{IDENTIFIER}', variable_name(identifier))\
      .gsub('{CAPITALIZED_IDENTIFIER}', format_view_controller(identifier))
  end.join("\n"))
end