Class: Evva::SwiftGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/evva/swift_generator.rb

Constant Summary collapse

BASE_TEMPLATE =
File.expand_path("./templates/swift/base.swift", __dir__)
EVENTS_TEMPLATE =
File.expand_path("./templates/swift/events.swift", __dir__)
PEOPLE_PROPERTIES_TEMPLATE =
File.expand_path("./templates/swift/people_properties.swift", __dir__)
SPECIAL_PROPERTY_ENUMS_TEMPLATE =
File.expand_path("./templates/swift/special_property_enums.swift", __dir__)
DESTINATIONS_TEMPLATE =
File.expand_path("./templates/swift/destinations.swift", __dir__)
TAB_SIZE =

t -> 4 spaces

"    "
NATIVE_TYPES =
%w[Int String Double Float Bool Date].freeze
SWIFT_KEYWORDS =
%w[default].freeze

Instance Method Summary collapse

Constructor Details

#initialize(swift_public: false) ⇒ SwiftGenerator

Returns a new instance of SwiftGenerator.



16
17
18
# File 'lib/evva/swift_generator.rb', line 16

def initialize(swift_public: false)
  @swift_public_modifier = swift_public ? "public " : ""
end

Instance Method Details

#destinations(destinations_bundle, _file_name) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/evva/swift_generator.rb', line 101

def destinations(destinations_bundle, _file_name)
  header_footer_wrapper do
    destinations = destinations_bundle.map { |p| camelize(p) }

    template_from(DESTINATIONS_TEMPLATE).result(binding)
  end
end

#event_enumObject



55
56
57
# File 'lib/evva/swift_generator.rb', line 55

def event_enum
  # empty
end

#events(bundle, _file_name, _enums_file_name, _destinations_file_name) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/evva/swift_generator.rb', line 20

def events(bundle, _file_name, _enums_file_name, _destinations_file_name)
  header_footer_wrapper do
    events = bundle.map do |event|
      properties = event.properties.map { |k, v|
        type = native_type(v)

        value_fetcher = k.to_s

        if special_property?(type)
          if type.end_with?("?")
            # optional value, we need ? to access a parameter
            value_fetcher += "?"
          end
          value_fetcher += ".rawValue"
        end

        {
          name: k.to_s,
          type: type,
          value: value_fetcher,
        }
      }

      {
        case_name: camelize(event.event_name),
        event_name: event.event_name,
        properties: properties,
        destinations: event.destinations.map { |p| camelize(p) },
      }
    end

    template_from(EVENTS_TEMPLATE).result(binding)
  end
end

#people_properties(people_bundle, _file_name, _enums_file_name, _destinations_file_name) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/evva/swift_generator.rb', line 59

def people_properties(people_bundle, _file_name, _enums_file_name, _destinations_file_name)
  header_footer_wrapper do
    properties = people_bundle.map do |p|
      type = native_type(p.type)
      {
        case_name: camelize(p.property_name),
        property_name: p.property_name,
        type: type,
        is_special_property: special_property?(type),
        is_optional: type.end_with?("?"),
        destinations: p.destinations.map { |p| camelize(p) },
      }
    end

    template_from(PEOPLE_PROPERTIES_TEMPLATE).result(binding)
  end
end

#people_properties_enumObject



77
78
79
# File 'lib/evva/swift_generator.rb', line 77

def people_properties_enum
  # empty
end

#special_property_enums(enums_bundle) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/evva/swift_generator.rb', line 81

def special_property_enums(enums_bundle)
  header_footer_wrapper do
    enums = enums_bundle.map do |enum|
      values = enum.values.map do |value|
        {
          case_name: camelize(value),
          value: value
        }
      end

      {
        name: enum.enum_name,
        values: values
      }
    end

    template_from(SPECIAL_PROPERTY_ENUMS_TEMPLATE).result(binding)
  end
end