lib/parser.rb in veda-apps-0.0.5 vs lib/parser.rb in veda-apps-0.0.6
- old
+ new
@@ -2,24 +2,24 @@
require 'rest-client'
require 'uri'
class String
- def camelize
- splitted = self.split('_')
- camel_text = splitted[1..-1].collect(&:capitalize).join
- camel_text = splitted.first.nil? ? self : (splitted.first + camel_text)
- camel_text.unHyphoniezed
+ def camelize
+ filtered = self.split('_').select {|v| v != ""}
+ camel_text = filtered[1..-1].collect(&:capitalize).join
+ camel_text = filtered.first.nil? ? self : (filtered.first + camel_text)
+ camel_text.unHyphoniezed
+ end
+
+ def unHyphoniezed
+ splitted = self.split('-')
+ camel_text = splitted[1..-1].collect(&:capitalize).join
+ splitted.first.nil? ? self : (splitted.first + camel_text)
+ end
end
- def unHyphoniezed
- splitted = self.split('-')
- camel_text = splitted[1..-1].collect(&:capitalize).join
- splitted.first.nil? ? self : (splitted.first + camel_text)
- end
-end
-
class Parser
def initialize path, realm=false
json = File.read(path)
@json = JSON.parse(json)
@realm = realm
@@ -83,40 +83,39 @@
end
def generate_attributes_literals json
swiftClassAttributes = []
swiftClass = ""
+ # take all hash
if json.is_a? Hash
+ # loop on each key value pair
json.each do |key, value|
+ # if value is not array or hash it is a attribute
if !(value.is_a? Array) && !(value.is_a? Hash)
- # string = "#{get_attribute_literal_prefix}var #{key}: #{attribute_type value} = #{default_value value}\n"
- # swiftClass = swiftClass + string
attribute = Attribute.new(key, "#{attribute_type value}")
swiftClassAttributes.push(attribute)
elsif value.is_a? Hash
newSwiftClass = generate_attributes_literals value
@parsed.store(key.capitalize.camelize, newSwiftClass)
- # string = "#{get_attribute_literal_prefix}var #{key}: #{key.capitalize}?\n"
- # swiftClass = swiftClass + string
attribute = Attribute.new(key, "#{key}")
swiftClassAttributes.push(attribute)
elsif value.is_a? Array
if value.first.is_a? Hash
newSwiftClass = generate_attributes_literals value.first
@parsed.store(key.capitalize.camelize, newSwiftClass)
- # string = "var #{key}#{get_array_attribute_literal key}\n"
- # swiftClass = swiftClass + string
attribute = Attribute.new(key, "#{key}", true)
swiftClassAttributes.push(attribute)
else
- # string = "var #{key}: [#{attribute_type value.first}] = []\n"
- # swiftClass = swiftClass + string
attribute = Attribute.new(key, "#{attribute_type value.first}", true)
swiftClassAttributes.push(attribute)
end
end
end
+ elsif json.is_a? Array
+ if json.first.is_a? Hash
+ generate_attributes_literals json.first
+ end
end
# swiftClass
swiftClassAttributes
end
@@ -160,10 +159,15 @@
attribute_literals += attribute_literal
mapping_literals += mapping_literal
end
class_model = <<-CLASS
+//
+// Created with veda-apps.
+// https://rubygems.org/gems/veda-apps
+//
+import Foundation
import ObjectMapper
#{@realm? "import RealmSwift\nimport ObjectMapper_Realm" : ""}
class #{class_name}:#{@realm? " Object," : ""} Mappable {
#{attribute_literals}
@@ -175,9 +179,62 @@
CLASS
create_file class_name, class_model
end
end
+
+ # parser for moya model mapper
+ def parseForMoya!
+
+ swiftClass = generate_attributes_literals @json
+ if @json.is_a? Hash
+ @parsed.store("Container", swiftClass)
+ end
+
+ @parsed.each do |class_name, attributes|
+ attribute_literals = ""
+ mapping_literals = ""
+ key_literals = ""
+ attributes.each do |attribute|
+ attribute_literal = ""
+ mapping_literal = ""
+ key_literal = ""
+ if attribute.is_array
+ attribute_literal = "\tlet #{attribute.name.camelize}: [#{attribute.type.capitalize}]\n"
+ else
+ attribute_literal = "\tlet #{attribute.name.camelize}: #{attribute.type.capitalize}\n"
+ end
+ mapping_literal = "\t\t#{attribute.name.camelize} = try map.from(Key.#{attribute.name.camelize})\n"
+ key_literal = "\t\tstatic let #{attribute.name.camelize} = \"#{attribute.name}\"\n"
+ attribute_literals += attribute_literal
+ mapping_literals += mapping_literal
+ key_literals += key_literal
+ end
+
+ class_model = <<-CLASS
+//
+// Created with veda-apps.
+// https://rubygems.org/gems/veda-apps
+//
+import Foundation
+import Moya
+import Mapper
+
+struct #{class_name}: Mappable {
+#{attribute_literals}
+\tinit(map: Mapper) throws {
+#{mapping_literals}
+\t}
+
+\tstruct Key {
+#{key_literals}
+\t}
+}\n
+CLASS
+ create_file class_name, class_model
+ end
+end
+
end
class Attribute
attr_accessor :name, :type, :is_array
def initialize name, type, is_array=false