Class: Wara::Core

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

Instance Method Summary collapse

Instance Method Details

#create(model, to, lang = Lang::ObjC) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wara.rb', line 32

def create(model, to, lang = Lang::ObjC)
	to = File.dirname(model) unless to
	@xml = REXML::Document.new(open(File.expand_path("contents", model)))
	@objects = @xml.get_elements('model/entity')
	@entities = @objects.map{ |o|
		representedClassName = o.attribute("representedClassName")
		parentEntity = o.attribute("parentEntity")
		a = o.get_elements("attribute")
		{
			"name" => o.attribute("name").value,
			"representedClassName"=>representedClassName ? representedClassName.value : nil,
			"parentEntity"=>parentEntity ? parentEntity.value : nil,
			"attributes"=>a.inject({}) {|s, o|
				s.merge({ o.attribute("name").value => o.attribute("attributeType").value, })
			},
		}
	}.select{|s| s["representedClassName"]}
	output(@entities, to, lang)
end

#custom_header(file_name) ⇒ Object



106
107
108
# File 'lib/wara.rb', line 106

def custom_header(file_name)
	"#import \"_#{file_name}Wrapper.h\"\n\n@interface #{file_name}Wrapper : _#{file_name}Wrapper {}\n// Custom logic goes here.\n@end\n"
end

#custom_implementation(file_name) ⇒ Object



109
110
111
# File 'lib/wara.rb', line 109

def custom_implementation(file_name)
	"#import \"#{file_name}Wrapper.h\"\n\n\n@interface #{file_name}Wrapper ()\n\n// Private interface goes here.\n\n@end\n\n\n@implementation #{file_name}Wrapper\n\n// Custom logic goes here.\n\n@end\n"
end

#custom_swift(file_name) ⇒ Object



146
147
148
# File 'lib/wara.rb', line 146

def custom_swift(file_name)
	"import Foundation\n\nclass #{file_name}Wrapper: _#{file_name}Wrapper {\n    // Custom logic goes here.\n}\n"
end

#entitySwiftType(value) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/wara.rb', line 149

def entitySwiftType(value)
	{
		"Boolean"=>"NSNumber?",
		"Binary"=>"NSData?",
		"Date"=>"NSDate?",
		"Decimal"=>"NSDecimalNumber?",
		"Double"=>"NSNumber?",
		"Float"=>"NSNumber?",
		"Integer 16"=>"NSNumber?",
		"Integer 32"=>"NSNumber?",
		"Integer 64"=>"NSNumber?",
		"String"=>"String?",
		"Transformable"=>"AnyObject?",
	}[value]
end

#entityType(value) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/wara.rb', line 112

def entityType(value)
	{
		"Boolean"=>"NSNumber *",
		"Binary"=>"NSData *",
		"Date"=>"NSDate *",
		"Decimal"=>"NSDecimalNumber *",
		"Double"=>"NSNumber *",
		"Float"=>"NSNumber *",
		"Integer 16"=>"NSNumber *",
		"Integer 32"=>"NSNumber *",
		"Integer 64"=>"NSNumber *",
		"String"=>"NSString *",
		"Transformable"=>"id ",
	}[value]
end

#header(e) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/wara.rb', line 81

def header(e)
	name = e["representedClassName"]
	entity = e["parentEntity"]
	import = entity ? "\#import \"#{entity}Wrapper.h\"\n" : "#import <Foundation/Foundation.h>\n"
	parentEntity = entity ? "#{entity}Wrapper" : "NSObject"
	getter = entity ? "- (#{name} *)entity;\n" : "@property(readonly, strong, nonatomic) #{name} *entity;\n"
	"#{import}\n@class #{name};\n\n\n@interface _#{name}Wrapper : #{parentEntity}\n" +
	e["attributes"].map {|k, v|
		"@property(nonatomic, strong) #{entityType(v)}#{k};\n"
	}.inject("") {|s, v| s + v} + "\n#{getter}\n- (instancetype)initWithEntity:(#{name} *)entity;\n\n- (void)updateEntity:(#{name} *)entity;\n@end\n"
end

#implementation(e) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/wara.rb', line 92

def implementation(e)
	name = e["representedClassName"]
	init = e["parentEntity"] ? "[super initWithEntity:entity]" : "[super init]"
	getter = e["parentEntity"] ? "- (BloodPressure *)entity {\n    return (BloodPressure *) [super entity];\n}\n\n" : ""
	first = e["parentEntity"] ? "" : "        _entity = entity;\n"
	update = e["parentEntity"] ? "    [super updateEntity:entity];\n" : ""
	"#import \"_#{name}Wrapper.h\"\n#import \"#{name}.h\"\n\n@interface _#{name}Wrapper ()\n@end\n\n@implementation _#{name}Wrapper {\n}\n#{getter}- (instancetype)initWithEntity:(#{name} *)entity {\n    self = #{init};\n    if (self) {\n#{first}" +
	e["attributes"].map {|k, v|
		" " * 8 + "self.#{k} = entity.#{k};\n"
	}.inject("") {|s, v| s + v} + "    }\n    return self;\n}\n\n- (void)updateEntity:(#{name} *)entity {\n#{update}" +
	e["attributes"].map {|k, v|
		" " * 4 + "entity.#{k} = self.#{k};\n"
	}.inject("") {|s, v| s + v} + "}\n@end\n"
end

#output(entities, to, lang) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/wara.rb', line 51

def output(entities, to, lang)
	FileUtils.mkdir_p(to) unless FileTest.exist?(to)
	if lang == Lang::ObjC then
		output_objc(entities, to)
	else
		output_swift(entities, to)
	end
end

#output_objc(entities, to) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/wara.rb', line 59

def output_objc(entities, to)
	entities.map {|e|
		file_name = e["representedClassName"]
		hw = header(e)
		File.write( File.expand_path("_#{file_name}Wrapper.h", to), hw)
		mw = implementation(e)
		File.write( File.expand_path("_#{file_name}Wrapper.m", to), mw)
		custom_header_name = File.expand_path("#{file_name}Wrapper.h", to)
		File.write( custom_header_name, custom_header(file_name)) unless FileTest.exist?(custom_header_name)
		custom_implementation_name = File.expand_path("#{file_name}Wrapper.m", to)
		File.write( custom_implementation_name, custom_implementation(file_name)) unless FileTest.exist?(custom_implementation_name)
	}
end

#output_swift(entities, to) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/wara.rb', line 72

def output_swift(entities, to)
	entities.map {|e|
		file_name = e["representedClassName"]
		mw = swift(e)
		File.write( File.expand_path("_#{file_name}Wrapper.swift", to), mw)
		custom_implementation_name = File.expand_path("#{file_name}Wrapper.swift", to)
		File.write( custom_implementation_name, custom_swift(file_name)) unless FileTest.exist?(custom_implementation_name)
	}
end

#swift(e) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wara.rb', line 127

def swift(e)
	name = e["representedClassName"]
	entity = e["parentEntity"]
	init = entity ? "[super initWithEntity:entity]" : "[super init]"
	parentEntity = entity ? ": #{entity}Wrapper" : ": NSObject"
	getter = entity ? "    override func entity() -> #{name}? {\n        return _entity as #{name}?\n" : "    let _entity: #{name}?\n    func entity() -> #{name}? {\n        return _entity\n"
	init = entity ? "        super.init(#{entity.to_scamel}: #{name.to_scamel})\n" : "        _entity = #{name.to_scamel}\n        super.init()\n"
	update = entity ? "        super.update#{entity}(#{name.to_scamel})\n" : ""
	"import Foundation\n\nclass _#{name}Wrapper#{parentEntity} {\n#{getter}    }\n" +
	e["attributes"].map {|k, v|
		" " * 4 + "var #{k}: #{entitySwiftType(v)}\n"
	}.inject("") {|s, v| s + v} + "\n    init(#{name.to_scamel}: #{name}?) {\n#{init}        if let e = #{name.to_scamel} {\n" +
	e["attributes"].map {|k, v|
		" " * 12 + "self.#{k} = e.#{k}\n"
	}.inject("") {|s, v| s + v} + "        }\n    }\n    func update#{name}(#{name.to_scamel}: #{name}) {\n#{update}" +
	e["attributes"].map {|k, v|
		" " * 8 + "#{name.to_scamel}.#{k} = self.#{k}\n"
	}.inject("") {|s, v| s + v} + "    }\n}\n"
end