Class: Evva::KotlinGenerator

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

Constant Summary collapse

BASE_TEMPLATE =
File.expand_path("./templates/kotlin/base.kt", __dir__)
EVENTS_TEMPLATE =
File.expand_path("./templates/kotlin/events.kt", __dir__)
EVENT_ENUM_TEMPLATE =
File.expand_path("./templates/kotlin/event_enum.kt", __dir__)
PEOPLE_PROPERTIES_TEMPLATE =
File.expand_path("./templates/kotlin/people_properties.kt", __dir__)
PEOPLE_PROPERTIES_ENUM_TEMPLATE =
File.expand_path("./templates/kotlin/people_properties_enum.kt", __dir__)
SPECIAL_PROPERTY_ENUMS_TEMPLATE =
File.expand_path("./templates/kotlin/special_property_enums.kt", __dir__)
DESTINATIONS_TEMPLATE =
File.expand_path("./templates/kotlin/destinations.kt", __dir__)
TAB_SIZE =

t -> 4 spaces

"    "
NATIVE_TYPES =
%w[Long Int String Double Float Boolean Date].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_name) ⇒ KotlinGenerator

Returns a new instance of KotlinGenerator.



5
6
7
# File 'lib/evva/kotlin_generator.rb', line 5

def initialize(package_name)
  @package_name = package_name
end

Instance Attribute Details

#package_nameObject

Returns the value of attribute package_name.



3
4
5
# File 'lib/evva/kotlin_generator.rb', line 3

def package_name
  @package_name
end

Instance Method Details

#destinations(bundle, file_name) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/evva/kotlin_generator.rb', line 135

def destinations(bundle, file_name)
  header_footer_wrapper do
    class_name = file_name

    destinations = bundle.map { |d| constantize(d) }

    template_from(DESTINATIONS_TEMPLATE).result(binding)
  end
end

#event_enum(bundle, file_name) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/evva/kotlin_generator.rb', line 64

def event_enum(bundle, file_name)
  header_footer_wrapper do
    class_name = file_name

    events = bundle.map(&:event_name).map do |event_name|
      {
        name: constantize(event_name),
        value: event_name,
      }
    end

    template_from(EVENT_ENUM_TEMPLATE).result(binding)
  end
end

#events(bundle, file_name, enums_file_name, destinations_file_name) ⇒ Object



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
54
55
56
57
58
59
60
61
62
# File 'lib/evva/kotlin_generator.rb', line 21

def events(bundle, file_name, enums_file_name, destinations_file_name)
  header_footer_wrapper do
    class_name = file_name
    enums_class_name = enums_file_name
    destinations_class_name = destinations_file_name

    events = bundle.map do |event|
      properties = event.properties.map do |name, type|
        type = native_type(type)

        param_name = camelize(name.to_s, false)
        value_fetcher = param_name

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

        {
          param_name: param_name,
          value_fetcher: value_fetcher,
          type: type,
          name: name.to_s,
        }
      end

      destinations = event.destinations.map { |p| constantize(p) }

      {
        class_name: camelize(event.event_name),
        event_name: constantize(event.event_name),
        properties: properties,
        destinations: destinations
      }
    end

    template_from(EVENTS_TEMPLATE).result(binding)
  end
end

#people_properties(people_bundle, file_name, enums_file_name, destinations_file_name) ⇒ Object



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

def people_properties(people_bundle, file_name, enums_file_name, destinations_file_name)
  header_footer_wrapper do
    class_name = file_name
    enums_class_name = enums_file_name
    destinations_class_name = destinations_file_name

    properties = people_bundle.map do |property|
      type = native_type(property.type)
      {
        class_name: camelize(property.property_name),
        property_name: constantize(property.property_name),
        type: type,
        is_special_property: is_special_property?(property.type),
        destinations: property.destinations.map { |p| constantize(p) },
      }
    end

    template_from(PEOPLE_PROPERTIES_TEMPLATE).result(binding)
  end
end

#people_properties_enum(people_bundle, file_name) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/evva/kotlin_generator.rb', line 100

def people_properties_enum(people_bundle, file_name)
  header_footer_wrapper do
    class_name = file_name

    properties = people_bundle.map(&:property_name).map do |property_name|
      {
        name: constantize(property_name),
        value: property_name,
      }
    end

    template_from(PEOPLE_PROPERTIES_ENUM_TEMPLATE).result(binding)
  end
end

#special_property_enums(enums_bundle) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/evva/kotlin_generator.rb', line 115

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

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

    template_from(SPECIAL_PROPERTY_ENUMS_TEMPLATE).result(binding)
  end
end