metamodel 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7736cf1e138517c6079cfc3c290f943ad285bbee
4
- data.tar.gz: 75cc11f484c32d1a54ef70d457ba9d83a89f75d0
3
+ metadata.gz: 5027faa28bcf3e7d87ba3e152f2f812562237c49
4
+ data.tar.gz: b78d7293ab4a90fdc831f6e7b94b2d802d306f9a
5
5
  SHA512:
6
- metadata.gz: dbad5e08fb83943368a289a2bdf7602b20297a7267f5bcabc5ada3f9264b0ee8a1d8c19e0ee6c5fdc5034094897000486a1b0e3ddcbf8eaa2e304c36d326b2fd
7
- data.tar.gz: 0359d81ec9bcd81b30fa7ae9d7056fc7214dde6ed7b8e57dccba885a7195113f7671f8db6aa791602f7818168d28a846d2b271761a50a130915f818a4dbcbc18
6
+ metadata.gz: c4f4929a6cebae8736d7783a493312251ea668942ab6689f09ad032669f2bce5432ed985402af02e3ed4a27669502c85e6eb3934b515616d2d3a1f47f982bbfe
7
+ data.tar.gz: 24de521af1ada8393bb874aab884be227cdbee438c0984a403535b311131011175340d0e0d78426672c719b866e710aa917009522b9834999494596e1872b1d8
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  [![License](https://img.shields.io/badge/license-MIT-green.svg?style=flat)](https://github.com/draveness/metamodel/blob/master/LICENSE)
6
6
  [![Gem](https://img.shields.io/gem/v/metamodel.svg?style=flat)](http://rubygems.org/gems/metamodel)
7
7
 
8
- MetaModel is an iOS framework that includes everything about model layer, data persistent, JSON to model constructor and a bunch of APIs which provides a way of dealing with client side database very easily.
8
+ MetaModel is an iOS framework designed to help developer to deal with data persistent, JSON to model constructor and a bunch of APIs which provides an approach of handling client side database very easily.
9
9
 
10
10
  > MetaModel is under-development, API may constantly change before gets to 1.0.0.
11
11
 
@@ -13,11 +13,30 @@ MetaModel is an iOS framework that includes everything about model layer, data p
13
13
  + [x] Dealing with database without writing SQL
14
14
  + [x] Most concise API to retrieve data from persistent level
15
15
 
16
- Use a scaffold file to define your model:
16
+ MetaModel provides convenience chainable APIs to manipulate models like ActiveRecord.
17
17
 
18
- ```ruby
19
- metamodel_version '0.0.1'
18
+ ```swift
19
+ let json = ["id": 1, "name": "Buzz", "email": "i@metamodel.info"]
20
+ let person = Person.parse(json)
21
+
22
+ // INSERT INTO "people" (id, name, email) VALUES (1, "Buzz", "i@metamodel.info")
23
+ person.save
20
24
 
25
+ // SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1
26
+ if var person = Person.find(1) {
27
+ // UPDATE "people" SET "people"."name") = "Buzz" WHERE "people"."id" = 1
28
+ person.update(name: "Draven")
29
+ }
30
+
31
+ // SELECT "people".* FROM "people"
32
+ print(Person.all)
33
+ ```
34
+
35
+ ## Usage
36
+
37
+ Use a meta file in `./meta` folder to define your model:
38
+
39
+ ```ruby
21
40
  define :User do |j|
22
41
  # define User model like this
23
42
  j.nickname :string
@@ -38,7 +57,7 @@ sudo gem install metamodel --verbose
38
57
 
39
58
  ## Quick Start
40
59
 
41
- After installation , run `meta init` in your iOS project root folder which will make a `scaffold` directory in current folder.
60
+ After installation , run `meta init` in your iOS project root folder which will make a `meta` directory in current folder.
42
61
 
43
62
  ```shell
44
63
  $ cd /path/to/project
@@ -46,24 +65,24 @@ $ meta init
46
65
 
47
66
  Initialing MetaModel project
48
67
 
49
- Creating `scaffold` folder for MetaModel
68
+ Creating `meta` folder for MetaModel
50
69
  ```
51
70
 
52
- Generate your model scaffold file with `meta generate`.
71
+ Generate your model meta file with `meta generate`.
53
72
 
54
73
  ```shell
55
74
  $ meta generate User
56
75
 
57
- Generating model scaffold file
76
+ Generating model meta file
58
77
 
59
- -> Adding `user.rb` to scaffold folder
78
+ -> Adding `user.rb` to meta folder
60
79
 
61
80
  [!] `user.rb` has already generated, use the command below to edit it.
62
81
 
63
- vim scaffold/user.rb
82
+ vim meta/user.rb
64
83
  ```
65
84
 
66
- Edit scaffold file using vim, Emacs or other editor and run `meta build`.
85
+ Edit meta file using vim, Emacs or other editor and run `meta build`.
67
86
 
68
87
  ```shell
69
88
  $ meta build
@@ -73,7 +92,7 @@ Building MetaModel project
73
92
  Cloning MetaModel project into `./MetaModel` folder
74
93
  Using `./MetaModel/MetaModel.xcodeproj` to build module
75
94
 
76
- Analyzing scaffold files
95
+ Analyzing meta files
77
96
  -> Resolving `user.rb`
78
97
 
79
98
  Generating model files
@@ -30,12 +30,12 @@ module MetaModel
30
30
 
31
31
  private
32
32
 
33
- # Checks that scaffold folder exists
33
+ # Checks that meta folder exists
34
34
  #
35
35
  # @return [void]
36
- def verify_scaffold_exists!
37
- unless config.scaffold_folder
38
- raise Informative, "No `scaffold' folder found in the project directory."
36
+ def verify_meta_exists!
37
+ unless config.meta_folder
38
+ raise Informative, "No `meta' folder found in the project directory."
39
39
  end
40
40
  end
41
41
  end
@@ -57,7 +57,7 @@ module MetaModel
57
57
 
58
58
  def validate!
59
59
  super
60
- raise Informative, 'No scaffold folder in directory' unless config.scaffold_path_in_dir(Pathname.pwd)
60
+ raise Informative, 'No meta folder in directory' unless config.meta_path_in_dir(Pathname.pwd)
61
61
  end
62
62
 
63
63
  private
@@ -15,13 +15,13 @@ module MetaModel
15
15
  end
16
16
 
17
17
  def parse
18
- UI.section "Analyzing scaffold files" do
19
- scaffold_path = config.scaffold_path
20
- scaffolds = Dir[scaffold_path + "*.rb"]
21
- scaffolds.each do |scaffold_file|
22
- UI.message '-> '.green + "Resolving `#{File.basename(scaffold_file)}`"
23
- scaffold_code = File.read(scaffold_path + scaffold_file)
24
- eval scaffold_code
18
+ UI.section "Analyzing meta files" do
19
+ meta_path = config.meta_path
20
+ metas = Dir[meta_path + "*.rb"]
21
+ metas.each do |meta_file|
22
+ UI.message '-> '.green + "Resolving `#{File.basename(meta_file)}`"
23
+ meta_code = File.read(meta_path + meta_file)
24
+ eval meta_code
25
25
  end
26
26
  end
27
27
  @models
@@ -31,7 +31,7 @@ module MetaModel
31
31
 
32
32
  def metamodel_version(version)
33
33
  raise Informative,
34
- "Scaffold file #{version} not matched with current metamodel version #{VERSION}" if version != VERSION
34
+ "Meta file #{version} not matched with current metamodel version #{VERSION}" if version != VERSION
35
35
  end
36
36
 
37
37
  def define(model_name)
@@ -10,19 +10,19 @@ module MetaModel
10
10
 
11
11
  def initialize(argv)
12
12
  @model_name = argv.shift_argument
13
- @file_path = config.scaffold_path + "#{@model_name.underscore}.rb"
13
+ @file_path = config.meta_path + "#{@model_name.underscore}.rb"
14
14
  super
15
15
  end
16
16
 
17
17
  def run
18
- verify_scaffold_exists!
19
- UI.section "Generating model scaffold file" do
18
+ verify_meta_exists!
19
+ UI.section "Generating model meta file" do
20
20
  title_options = { :verbose_prefix => '-> '.green }
21
- UI.titled_section "Adding `#{File.basename(@file_path)}` to scaffold folder", title_options do
21
+ UI.titled_section "Adding `#{File.basename(@file_path)}` to meta folder", title_options do
22
22
  @file_path.open('w') { |f| f << model_template(@model_name) }
23
23
  end
24
24
  UI.notice "`#{File.basename(@file_path)}` has already generated, use the command below to edit it.\n"
25
- UI.message "vim scaffold/#{File.basename(@file_path)}"
25
+ UI.message "vim meta/#{File.basename(@file_path)}"
26
26
  end
27
27
  end
28
28
 
@@ -2,27 +2,27 @@ module MetaModel
2
2
  class Command
3
3
 
4
4
  class Init < Command
5
- self.summary = "Generate a scaffold folder for the current directory."
5
+ self.summary = "Generate a meta folder for the current directory."
6
6
  self.description = <<-DESC
7
- Creates a scaffold folder for the current directory if none exits. Call
7
+ Creates a meta folder for the current directory if none exits. Call
8
8
  this command before all other metamodel command.
9
9
  DESC
10
10
 
11
11
  def initialize(argv)
12
- @scaffold_path = Pathname.pwd + 'scaffold'
12
+ @meta_path = Pathname.pwd + 'meta'
13
13
  @project_path = argv.shift_argument
14
14
  super
15
15
  end
16
16
 
17
17
  def validate!
18
18
  super
19
- raise Informative, 'Existing scaffold folder in directory' unless config.scaffold_path_in_dir(Pathname.pwd).nil?
19
+ raise Informative, 'Existing meta folder in directory' unless config.meta_path_in_dir(Pathname.pwd).nil?
20
20
  end
21
21
 
22
22
  def run
23
23
  UI.section "Initialing MetaModel project" do
24
- UI.section "Creating `scaffold` folder for MetaModel" do
25
- FileUtils.mkdir(@scaffold_path)
24
+ UI.section "Creating `meta` folder for MetaModel" do
25
+ FileUtils.mkdir(@meta_path)
26
26
  end
27
27
  end
28
28
  end
@@ -39,14 +39,14 @@ module MetaModel
39
39
  # @!group Paths
40
40
 
41
41
  # @return [Pathname] the root of the MetaModel installation where the
42
- # scaffold folder is located.
42
+ # meta folder is located.
43
43
  #
44
44
  def installation_root
45
45
  current_dir = ActiveSupport::Multibyte::Unicode.normalize(Dir.pwd)
46
46
  current_path = Pathname.new(current_dir)
47
47
  unless @installation_root
48
48
  until current_path.root?
49
- if scaffold_path_in_dir(current_path)
49
+ if meta_path_in_dir(current_path)
50
50
  @installation_root = current_path
51
51
  break
52
52
  else
@@ -77,33 +77,33 @@ module MetaModel
77
77
  "./MetaModel/MetaModel.xcodeproj"
78
78
  end
79
79
 
80
- # Returns whether or not scaffold folder is in current project.
80
+ # Returns whether or not meta folder is in current project.
81
81
  #
82
82
  # @return [Bool]
83
83
  #
84
- def scaffold_folder
85
- Pathname.new(scaffold_path).exist?
84
+ def meta_folder
85
+ Pathname.new(meta_path).exist?
86
86
  end
87
87
 
88
- # Returns the path of the scaffold.
88
+ # Returns the path of the meta.
89
89
  #
90
90
  # @return [Pathname]
91
91
  # @return [Nil]
92
92
  #
93
- def scaffold_path
94
- @scaffold_path_in_dir ||= installation_root + 'scaffold'
93
+ def meta_path
94
+ @meta_path_in_dir ||= installation_root + 'meta'
95
95
  end
96
96
 
97
- # Returns the path of the scaffold folder in the given dir if any exists.
97
+ # Returns the path of the meta folder in the given dir if any exists.
98
98
  #
99
99
  # @param [Pathname] dir
100
- # The directory where to look for the scaffold.
100
+ # The directory where to look for the meta.
101
101
  #
102
- # @return [Pathname] The path of the scaffold.
103
- # @return [Nil] If not scaffold was found in the given dir
102
+ # @return [Pathname] The path of the meta.
103
+ # @return [Nil] If not meta was found in the given dir
104
104
  #
105
- def scaffold_path_in_dir(dir)
106
- candidate = dir + 'scaffold'
105
+ def meta_path_in_dir(dir)
106
+ candidate = dir + 'meta'
107
107
  if candidate.exist?
108
108
  return candidate
109
109
  end
@@ -32,6 +32,10 @@ module MetaModel
32
32
  end
33
33
  end
34
34
 
35
+ def hash_value
36
+ self.hash.to_s(16)
37
+ end
38
+
35
39
  def property_key_value_pairs
36
40
  @properties.map { |property| "#{property.key.to_s}: #{property.key.to_s}" }.join(", ")
37
41
  end
@@ -40,8 +44,13 @@ module MetaModel
40
44
  @properties.map { |property| "#{property.key.to_s}: #{property.type.to_s}" }.join(", ")
41
45
  end
42
46
 
43
- def property_exclude_id_key_value_pairs(prefix = true)
44
- result = properties_exclude_id.map { |property| "#{property.key.to_s}: #{property.key.to_s}" }.join(", ")
47
+ def property_exclude_id_key_value_pairs(prefix = true, cast = false)
48
+ result = ""
49
+ if cast
50
+ result = properties_exclude_id.map { |property| "#{property.key.to_s}: #{property.type_without_optional == "Int" ? "Int(#{property.key.to_s})" : property.key.to_s}" }.join(", ")
51
+ else
52
+ result = properties_exclude_id.map { |property| "#{property.key.to_s}: #{property.key.to_s}" }.join(", ")
53
+ end
45
54
  return result unless prefix
46
55
  return result.length > 0 ? ", #{result}" : ""
47
56
  end
@@ -36,6 +36,10 @@ module MetaModel
36
36
  end
37
37
  end
38
38
 
39
+ def real_type
40
+ type_without_optional == "Int" ? "Int64" : type_without_optional
41
+ end
42
+
39
43
  def is_unique?
40
44
  @modifiers.include? :unique
41
45
  end
@@ -3,4 +3,8 @@ extension <%= model.name %> {
3
3
  let initializeTableSQL = "<%= model.build_table %>"
4
4
  executeSQL(initializeTableSQL)
5
5
  }
6
+ static func deinitialize() {
7
+ let dropTableSQL = "DROP TABLE \(tableName.unwrapped)"
8
+ executeSQL(dropTableSQL)
9
+ }
6
10
  }
@@ -6,35 +6,33 @@ public extension <%= model.name %> {
6
6
  executeSQL(deleteSQL)
7
7
  }
8
8
 
9
- <% model.properties_exclude_id.each do |property| %>
10
- <%= """mutating func update(#{property.key} #{property.key}: #{property.type}) -> #{model.name} {
9
+ <% model.properties_exclude_id.each do |property| %><%= """mutating func update(#{property.key} #{property.key}: #{property.type}) -> #{model.name} {
11
10
  return self.update([.#{property.key}: #{property.key}])
12
11
  }""" %>
13
12
  <% end %>
14
-
15
13
  mutating func update(attributes: [<%= model.name %>.Column: Any]) -> <%= model.name %> {
16
14
  var setSQL: [String] = []
17
15
  for (key, _) in attributes {
18
16
  switch key {
19
- <% model.properties_exclude_id.each do |property| %>
20
- <%= """case .#{property.key}: setSQL.append(\"\(key.unwrapped) = \(self.(#{property.key}#{property.is_optional? ? "?" : ""}.unwrapped)\")
21
- """ %>
22
- <% end %>
23
- default: break
17
+ <% model.properties_exclude_id.each do |property| %><%= """case .#{property.key}: setSQL.append(\"\\(key.unwrapped) = \\(#{property.key}#{property.is_optional? ? "?" : ""}.unwrapped)\")""" %>
18
+ <% end %>default: break
24
19
  }
25
20
  }
26
21
  let updateSQL = "UPDATE \(<%= model.name %>.tableName.unwrapped) SET \(setSQL.joinWithSeparator(", ")) \(itself)"
27
22
  executeSQL(updateSQL) {
28
23
  for (key, value) in attributes {
29
24
  switch key {
30
- <% model.properties_exclude_id.each do |property| %>
31
- <%= """case .#{property.key}: self.#{property.key} = value as#{property.is_optional? ? "?" : "!"} #{property.type_without_optional}
32
- """ %>
33
- <% end %>
34
- default: break
25
+ <% model.properties_exclude_id.each do |property| %><%= """case .#{property.key}: self.#{property.key} = value as#{property.is_optional? ? "?" : "!"} #{property.type_without_optional}""" %>
26
+ <% end %>default: break
35
27
  }
36
28
  }
37
29
  }
38
30
  return self
39
31
  }
32
+ var save: <%= model.name %> {
33
+ get {
34
+ <%= model.name %>.create(id<%= model.property_exclude_id_key_value_pairs %>)
35
+ return self
36
+ }
37
+ }
40
38
  }
@@ -1,16 +1,28 @@
1
1
  extension <%= model.name %> {
2
- public init(json: [String: Any]) {
2
+ public static func parse(json: [String: AnyObject]) -> <%= model.name %> {
3
3
  let id: Int = json["id"] as! Int
4
4
  <% model.properties_exclude_id.each do |property| %>
5
- <%= """let #{property.key}: #{property.type} = json[\"#{property.key}\"] as! #{property.type}
6
- """ %>
5
+ <%= """let #{property.key}: #{property.type} = json[\"#{property.key}\"] as! #{property.type}""" %>
7
6
  <% end %>
7
+ return <%= model.name %>(<%= model.property_key_value_pairs %>)
8
+ }
9
+
10
+ public static func parse(jsons: [[String: AnyObject]]) -> [<%= model.name %>] {
11
+ var results: [<%= model.name %>] = []
12
+ for json in jsons {
13
+ results.append(<%= model.name %>.parse(json))
14
+ }
15
+ return results
16
+ }
8
17
 
9
- self.init(<%= model.property_key_value_pairs %>)
18
+ public static func parse(data: NSData) throws -> <%= model.name %> {
19
+ let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as! [String: AnyObject]
20
+ return <%= model.name %>.parse(json)
10
21
  }
11
22
 
12
- public init(jsonData: NSData) throws {
13
- let json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .AllowFragments) as! [String: Any]
14
- self.init(json: json)
23
+ public static func parses(data: NSData) throws -> [<%= model.name %>] {
24
+ let json = try NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments) as! [[String: AnyObject]]
25
+ return <%= model.name %>.parse(json)
15
26
  }
27
+
16
28
  }
@@ -17,12 +17,22 @@ let db = try! Connection("\(path)/metamodel_db.sqlite3")
17
17
 
18
18
  public class MetaModel {
19
19
  public static func initialize() {
20
+ validateMetaModelTables()
20
21
  <% models.each do |model| %><%= "#{model.name}.initialize()" %>
21
22
  <% end %>
22
23
  }
24
+ static func validateMetaModelTables() {
25
+ createMetaModelTable()
26
+ let infos = retrieveMetaModelTableInfos()
27
+ <% models.each do |model| %><%= """if infos[#{model.name}.tableName] != \"#{model.hash_value}\" {
28
+ updateMetaModelTableInfos(#{model.name}.tableName, hashValue: \"#{model.hash_value}\")
29
+ #{model.name}.deinitialize()
30
+ }""" %>
31
+ <% end %>
32
+ }
23
33
  }
24
34
 
25
- func executeSQL(sql: String, success: (() -> ())? = nil) -> Statement? {
35
+ func executeSQL(sql: String, silent: Bool = false, success: (() -> ())? = nil) -> Statement? {
26
36
  defer { print("\n") }
27
37
  print("-> Begin Transaction")
28
38
  let startDate = NSDate()
@@ -1,10 +1,7 @@
1
1
  extension <%= model.name %>: Recordable {
2
2
  public init(values: Array<Optional<Binding>>) {
3
- let id: Int64 = values[0] as! Int64
4
- <% model.properties_exclude_id.each_with_index do |property, index| %>
5
- <%= """let #{property.key}: #{property.type} = values[#{index + 1}] as! #{property.type}
6
- """ %>
3
+ <% model.properties.each_with_index do |property, index| %><%= """let #{property.key}: #{property.real_type} = values[#{index}] as! #{property.real_type}""" %>
7
4
  <% end %>
8
- self.init(id: Int(id)<%= model.property_exclude_id_key_value_pairs %>)
5
+ self.init(id: Int(id)<%= model.property_exclude_id_key_value_pairs(true, true) %>)
9
6
  }
10
7
  }
@@ -1,5 +1,5 @@
1
1
  module MetaModel
2
2
  # The version of the MetaModel command line tool.
3
3
  #
4
- VERSION = '0.0.2'.freeze unless defined? MetaModel::MetaModel
4
+ VERSION = '0.0.3'.freeze unless defined? MetaModel::MetaModel
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metamodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Draveness Zuo
@@ -157,7 +157,6 @@ files:
157
157
  - lib/metamodel/template/instance_methods.swift.erb
158
158
  - lib/metamodel/template/json.swift.erb
159
159
  - lib/metamodel/template/metamodel.swift.erb
160
- - lib/metamodel/template/model.swift.erb
161
160
  - lib/metamodel/template/model_query.swift.erb
162
161
  - lib/metamodel/template/model_relation.swift.erb
163
162
  - lib/metamodel/template/recordable.swift.erb
@@ -184,7 +183,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
183
  version: '0'
185
184
  requirements: []
186
185
  rubyforge_project:
187
- rubygems_version: 2.6.6
186
+ rubygems_version: 2.6.4
188
187
  signing_key:
189
188
  specification_version: 4
190
189
  summary: The Cocoa models generator.
@@ -1,122 +0,0 @@
1
- //
2
- // <%= model.name %>.swift
3
- // MetaModel
4
- //
5
- // Created by MetaModel script.
6
- //
7
-
8
- import Foundation
9
- import SQLite
10
-
11
- public struct <%= model.name %> {
12
- public let id: Int
13
- <% model.properties_exclude_id.each do |property| %>
14
- <%= """public var #{property.key}: #{property.type} {
15
- didSet { try! db.run(itself.update(#{model.name}.#{property.key} <- #{property.key})) }
16
- }""" %>
17
- <% end %>
18
- }
19
-
20
- extension <%= model.name %>: Recordable {
21
- public init(record: SQLite.Row) {
22
- self.init(<%= model.properties.map { |property| "#{property.key}: record[#{model.name}.#{property.key}]" }.join(", ") %>)
23
- }
24
- }
25
-
26
- extension <%= model.name %> {
27
- static let table = Table("<%= "#{model.table_name}" %>")
28
- <% model.properties.each do |property| %><%= "static let #{property.key} = Expression<#{property.type}>(\"#{property.key}\")" %>
29
- <% end %>
30
- var itself: QueryType { get { return <%= model.name %>.table.filter(<%= model.name %>.id == self.id) } }
31
-
32
- static func createTable() {
33
- let _ = try? db.run(table.create { t in
34
- <%= model.build_table.chop %>
35
- })
36
- }
37
- }
38
-
39
- public extension <%= model.name %> {
40
- static func deleteAll() {
41
- let _ = try? db.run(<%= model.name %>.table.delete())
42
- }
43
-
44
- static func count() -> Int {
45
- return db.scalar(<%= model.name %>.table.count)
46
- }
47
-
48
- static func create(<%= model.properties.map { |property| "#{property.key}: #{property.type}" }.join(", ") %>) -> <%= model.name %> {
49
- let insert = <%= model.name %>.table.insert(<%= model.properties.map { |property| "#{model.name}.#{property.key} <- #{property.key}" }.join(", ") %>)
50
- let _ = try? db.run(insert)
51
- return <%= model.name %>(<%= model.properties.map { |property| "#{property.key}: #{property.key}" }.join(", ") %>)
52
- }
53
-
54
- }
55
-
56
- public extension <%= model.name %> {
57
- func delete() {
58
- try! db.run(itself.delete())
59
- }
60
- <% model.properties_exclude_id.each do |property| %>
61
- <%= """mutating func update(#{property.key} #{property.key}: #{property.type}) -> #{model.name} {
62
- self.#{property.key} = #{property.key}
63
- return self
64
- }""" %>
65
- <% end %>
66
- }
67
-
68
-
69
- public extension <%= model.name %> {
70
- static private func findAll(query: QueryType) -> [<%= model.name %>] {
71
- var result: [<%= model.name %>] = []
72
- for record in try! db.prepare(query) {
73
- result.append(<%= model.name %>(record: record))
74
- }
75
- return result
76
- }
77
-
78
- static var all: <%= model.relation_name %> {
79
- get {
80
- return <%= model.relation_name %>()
81
- }
82
- }
83
- <% model.properties.each do |property| %>
84
- <%= """static func findBy(#{property.key} #{property.key}: #{property.type}) -> #{model.relation_name} {
85
- return #{model.relation_name}().findBy(#{property.key}: #{property.key})
86
- }""" %>
87
- <% end %>
88
- static func limit(length: Int, offset: Int = 0) -> <%= model.relation_name %> {
89
- return <%= model.relation_name %>().limit(length, offset: offset)
90
- }
91
-
92
- static func group(params: Expressible...) -> <%= model.relation_name %> {
93
- return <%= model.relation_name %>().group(params)
94
- }
95
- }
96
-
97
- public class <%= model.relation_name %>: Relation<<%= model.name %>> {
98
- init() {
99
- super.init(query: <%= model.name %>.table)
100
- }
101
-
102
- <% model.properties.each do |property| %>
103
- <%= """public func findBy(#{property.key} #{property.key}: #{property.type}) -> Self {
104
- query = query.filter(#{model.name}.#{property.key} == #{property.key})
105
- return self
106
- }""" %>
107
- <% end %>
108
- public func limit(length: Int, offset: Int = 0) -> Self {
109
- query = query.limit(length, offset: offset)
110
- return self
111
- }
112
-
113
- public func group(params: Expressible...) -> Self {
114
- return group(params)
115
- }
116
-
117
- public func group(params: [Expressible]) -> Self {
118
- query = query.group(params)
119
- return self
120
- }
121
-
122
- }