Class: Swift::Boiler::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/swift/boiler/parser.rb

Constant Summary collapse

TYPE =
:type
NAME =
:name
PROPERTY_NAME_INDEX =
0
PROPERTY_TYPE_INDEX =
1
CAPITALIZED_NAME =
:capitalized_name
LABEL_TYPE =
:ui_label
VIEW_TYPE =
:ui_view
UNKNOWN_TYPE =
:unknow_type
SHORTCUTS =
{ tableviewcell: 'table_view_cell', m: 'model', v: 'view', c: 'controller', s: 'singleton', tvc: 'table_view_cell' }

Instance Method Summary collapse

Instance Method Details

#capitalize_name(name) ⇒ Object



128
129
130
131
132
# File 'lib/swift/boiler/parser.rb', line 128

def capitalize_name(name)
  place_holder = name.dup
  first_letter = place_holder.slice!(0).capitalize
  first_letter + place_holder
end

#create_property_dictionary(name, capitalized_name, type) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/swift/boiler/parser.rb', line 109

def create_property_dictionary(name, capitalized_name, type)
  property = {}
  property[NAME] = name
  property[TYPE] = type
  property[CAPITALIZED_NAME] = capitalized_name
  property
end

#create_template_from_tokens(tokens) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/swift/boiler/parser.rb', line 19

def create_template_from_tokens(tokens)
  template = Template.new
  template.template_path = get_template_path(tokens)
  template.class_name = get_class_name(tokens)
  template.options = get_options(tokens)
  template.properties = get_properties(tokens)
  template
end

#downcase_name(name) ⇒ Object



134
135
136
137
138
# File 'lib/swift/boiler/parser.rb', line 134

def downcase_name(name)
  place_holder = name.dup
  first_letter = place_holder.slice!(0).downcase
  first_letter + place_holder
end

#get_class_name(tokens) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/swift/boiler/parser.rb', line 48

def get_class_name(tokens)
  tokens.each do |token|
    if is_class_name_token(token)
      return token.content
    end
  end
end

#get_name_from_abreviation(abreviation) ⇒ Object



140
141
142
143
# File 'lib/swift/boiler/parser.rb', line 140

def get_name_from_abreviation(abreviation)
  print SHORTCUTS[abreviation.to_sym]
  SHORTCUTS[abreviation.to_sym] 
end

#get_name_from_name_token(template_name_token) ⇒ Object



145
146
147
# File 'lib/swift/boiler/parser.rb', line 145

def get_name_from_name_token(template_name_token)
  template_name_token.content.downcase
end

#get_options(tokens) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/swift/boiler/parser.rb', line 56

def get_options(tokens)
  options = {}
  tokens.each do |token|
    options[:protocol] = is_protocol_token(token)
  end
  options
end

#get_properties(tokens) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/swift/boiler/parser.rb', line 64

def get_properties(tokens)
  properties = {}
  properties[LABEL_TYPE] = Array.new
  properties[VIEW_TYPE] = Array.new
  properties[UNKNOWN_TYPE] = Array.new
  tokens.each do |token|
    if token.type == Token::PROPERTY
      new_property = get_property_from_token(token) 
      case new_property[TYPE].to_s
        when "UILabel"
          properties[LABEL_TYPE] << new_property
        when "UIView"
          properties[VIEW_TYPE] << new_property  
        else
          properties[UNKNOWN_TYPE] << new_property
      end
    end
  end
  properties
end

#get_property_from_token(token) ⇒ Object



102
103
104
105
106
107
# File 'lib/swift/boiler/parser.rb', line 102

def get_property_from_token(token)
  name = get_property_name_from_token(token)
  type = get_property_type_from_token(token)
  capitalized_name = capitalize_name(name)
  create_property_dictionary(name, capitalized_name, type)
end

#get_property_name_from_token(token) ⇒ Object



117
118
119
120
121
# File 'lib/swift/boiler/parser.rb', line 117

def get_property_name_from_token(token)
  argument_list = token.content.split(/:/)
  name = argument_list[PROPERTY_NAME_INDEX]
  downcase_name(name)
end

#get_property_type_from_token(token) ⇒ Object



123
124
125
126
# File 'lib/swift/boiler/parser.rb', line 123

def get_property_type_from_token(token)
  argument_list = token.content.split(/:/)
  argument_list[PROPERTY_TYPE_INDEX]
end

#get_template_name_from_token(template_name_token) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/swift/boiler/parser.rb', line 94

def get_template_name_from_token(template_name_token)
  if is_abreviation(template_name_token)
    get_name_from_abreviation(template_name_token.content.downcase)
  else
    get_name_from_name_token(template_name_token)
  end
end

#get_template_path(tokens) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/swift/boiler/parser.rb', line 28

def get_template_path(tokens)
  tokens.each do |token|
    if is_path_generation_possible_from_token(token)
      return get_template_path_from_token(token)
    end
  end
end

#get_template_path_from_name_token(token) ⇒ Object



89
90
91
92
# File 'lib/swift/boiler/parser.rb', line 89

def get_template_path_from_name_token(token)
  template_name = get_template_name_from_token(token)
  File.dirname(__FILE__) + '/templates/' + template_name + ".mustache" 
end

#get_template_path_from_path_token(token) ⇒ Object



85
86
87
# File 'lib/swift/boiler/parser.rb', line 85

def get_template_path_from_path_token(token)
  token.content
end

#get_template_path_from_token(token) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/swift/boiler/parser.rb', line 40

def get_template_path_from_token(token)
  if is_template_path_token(token)
    return get_template_path_from_path_token(token)
  elsif is_template_name_token(token)
    return get_template_path_from_name_token(token)
  end
end

#is_abreviation(template_name_token) ⇒ Object



149
150
151
152
# File 'lib/swift/boiler/parser.rb', line 149

def is_abreviation(template_name_token)
  template_name = template_name_token.content.downcase
  template_name_token.type == Token::TEMPLATE_NAME && SHORTCUTS.has_key?(template_name.to_sym)
end

#is_path_generation_possible_from_token(token) ⇒ Object



36
37
38
# File 'lib/swift/boiler/parser.rb', line 36

def is_path_generation_possible_from_token(token)
  is_template_path_token(token) || is_template_name_token(token)
end

#is_protocol_token(token) ⇒ Object



154
155
156
# File 'lib/swift/boiler/parser.rb', line 154

def is_protocol_token(token) 
  token.type == Token::OPTION && (token.content == "-p" || token.content == "--protocol")
end