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

Instance Method Summary collapse

Instance Method Details

#destinations(destinations_bundle, _file_name) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/evva/swift_generator.rb', line 95

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



50
51
52
# File 'lib/evva/swift_generator.rb', line 50

def event_enum
  # empty
end

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



15
16
17
18
19
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
# File 'lib/evva/swift_generator.rb', line 15

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 is_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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/evva/swift_generator.rb', line 54

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: is_special_property?(type),
        destinations: p.destinations.map { |p| camelize(p) },
      }
    end

    template_from(PEOPLE_PROPERTIES_TEMPLATE).result(binding)
  end
end

#people_properties_enumObject



71
72
73
# File 'lib/evva/swift_generator.rb', line 71

def people_properties_enum
  # empty
end

#special_property_enums(enums_bundle) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/evva/swift_generator.rb', line 75

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