class Evva::SwiftGenerator

Constants

EXTENSION_HEADER
NATIVE_TYPES

Public Instance Methods

event_case(event_data) click to toggle source
# File lib/evva/swift_generator.rb, line 26
def event_case(event_data)
  function_name = camelize(event_data.event_name)
  if event_data.properties.empty?
    "\t\tcase #{function_name}"
  else
    trimmed_properties = event_data.properties.map { |k, v| k.to_s + ': ' + native_type(v) }.join(", ")
    "\t\tcase #{function_name}(#{trimmed_properties})"
  end
end
event_data(event_data) click to toggle source
# File lib/evva/swift_generator.rb, line 36
def event_data(event_data)
  function_name = camelize(event_data.event_name)
  if event_data.properties.empty?
    function_body = "\t\t\tcase .#{function_name}:\n" \
                    "\t\t\t\treturn EventData(name: \"#{event_data.event_name}\")"
  else
    function_header = prepend_let(event_data.properties)
    function_arguments = dictionary_pairs(event_data.properties)
    function_body = "\t\t\tcase .#{function_name}(#{function_header}):\n"\
                    "\t\t\t\treturn EventData(name: \"#{event_data.event_name}\", properties: [\n"\
                    "\t\t\t\t\t#{function_arguments.join(",\n\t\t\t\t\t")} ]\n"\
                    "\t\t\t\t)"
  end
  function_body
end
event_enum(enum, file_name) click to toggle source
# File lib/evva/swift_generator.rb, line 52
def event_enum(enum, file_name)
  # empty
end
events(bundle, file_name) click to toggle source
# File lib/evva/swift_generator.rb, line 12
    def events(bundle, file_name)
      header_footer_wrapper do
"""\tenum Event {
#{bundle.map { |e| event_case(e) }.join("\n")}

\t\tvar data: EventData {
\t\t\tswitch self {
#{bundle.map { |e| event_data(e) }.join("\n\n")}
\t\t\t}
\t\t}
\t}"""
      end
    end
people_properties(people_bundle, file_name) click to toggle source
# File lib/evva/swift_generator.rb, line 56
def people_properties(people_bundle, file_name)
  header_footer_wrapper do
    props = "\tenum Property: String {\n"
    people_bundle.each do |prop|
      props << "\t\tcase #{camelize(prop)} = \"#{prop}\"\n"
    end
    props << "\t}"
  end
end
special_property_enums(enums) click to toggle source
# File lib/evva/swift_generator.rb, line 66
def special_property_enums(enums)
  header_footer_wrapper do
    enums.map do |enum|
      body = "\tenum #{enum.enum_name}: String {\n"
      enum.values.each do |value|
        body << "\t\tcase #{camelize(value)} = \"#{value}\"\n"
      end
      body << "\t}"
    end.join("\n\n")
  end
end

Private Instance Methods

camelize(term) click to toggle source
# File lib/evva/swift_generator.rb, line 121
def camelize(term)
  string = term.to_s.tr(' ', '_').downcase
  string = string.sub(/^(?:#{@acronym_regex}(?=\b|[A-Z_])|\w)/) { |match| match.downcase }
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{$1}#{$2.capitalize}" }
  string.gsub!("/".freeze, "::".freeze)
  string
end
dictionary_pairs(props) click to toggle source
# File lib/evva/swift_generator.rb, line 92
def dictionary_pairs(props)
  props.map do |name, type|
    pair = "\"#{name}\": #{name}"
    if is_raw_representable_property?(type)
      if is_optional_property?(type)
        pair += "?"
      end
      pair += ".rawValue"
    end
    pair += " as Any"
  end
end
is_optional_property?(type) click to toggle source
# File lib/evva/swift_generator.rb, line 109
def is_optional_property?(type)
  type.end_with?('?')
end
is_raw_representable_property?(type) click to toggle source
# File lib/evva/swift_generator.rb, line 105
def is_raw_representable_property?(type)
  !NATIVE_TYPES.include?(native_type(type).chomp('?'))
end
native_type(type) click to toggle source
# File lib/evva/swift_generator.rb, line 113
def native_type(type)
  type.gsub('Boolean','Bool').gsub('Long', 'Int')
end
prepend_let(props) click to toggle source
# File lib/evva/swift_generator.rb, line 117
def prepend_let(props)
  props.map { |k, v| "let #{k}" }.join(', ')
end