metamodel 0.1.6 → 0.1.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d1b98b98f7bd126d83e0066d44beb8beb5708351
4
- data.tar.gz: 48f9bb451aeb155aaa3a6cbd7ee426b7e0aa1467
3
+ metadata.gz: 0c119cc6df05af0b21c4f244f1b496f5f91ec517
4
+ data.tar.gz: 1faf793b51be72320af814d5d8ebcd14e575d420
5
5
  SHA512:
6
- metadata.gz: a2de3e544e6e583d81a84d51bc82ec98aebe2b7fc0a0d73b11ebfe4624a0ab06a8c42b00c03132e3a210644d306247360376f0711ecd902ad6406d72413c1e48
7
- data.tar.gz: 0947bc3d2862f25536d744e963bc0213ffaaaf63c4a9dbc60ff4f368813859dd5306d8b10c66822dcb15efcaef943ed9db9fc5e183d971c7566902d631b0eb9a
6
+ metadata.gz: 76072a392abaa5c37d6609702f88d4ee6ee9a7087bc43a4c8f22c0677ec0df910f1da5c4a2bfcb908e9fe6e170f5cfb61d6b2038e70afa3a0754f02db480b989
7
+ data.tar.gz: e3b57aca292f8b915b6a116b974f0896506012cbc7a421e15c3a1a3f72d3eec6ff9a79b5ce7cb76dbc6f9d612b2bc47c06818d40ef1f2c40937e2c06a419bb95
@@ -21,7 +21,7 @@ module MetaModel
21
21
  def templates
22
22
  results = []
23
23
  # file_paths = %w{file_header attributes json recordable initialize static_methods instance_methods model_query model_relation}
24
- file_paths = %w{file_header attributes recordable initialize static_methods instance_methods model_query model_relation foreign_key}
24
+ file_paths = %w{file_header table_initialize model_initialize model_update model_query model_delete static_methods foreign_key helper}
25
25
  file_paths.each do |file_path|
26
26
  template = File.read File.expand_path(File.join(File.dirname(__FILE__), "../../template/#{file_path}.swift"))
27
27
  results << template
@@ -41,26 +41,26 @@ module MetaModel
41
41
  @models << current_model
42
42
  end
43
43
 
44
- def attr(key, type = :string, *args)
44
+ def attr(key, type = :string, **args)
45
45
  current_model.properties << Record::Property.new(key, type, args)
46
46
  end
47
47
 
48
- def has_one(name, model_name = nil)
48
+ def has_one(name, model_name = nil, **args)
49
49
  model_name = name.to_s.singularize.camelize if model_name.nil?
50
- association = Record::Association.new(name, current_model.name, model_name, :has_one)
50
+ association = Record::Association.new(name, current_model.name, model_name, :has_one, args)
51
51
  @associations << association
52
52
  end
53
53
 
54
- def has_many(name, model_name = nil)
54
+ def has_many(name, model_name = nil, **args)
55
55
  model_name = name.to_s.singularize.camelize if model_name.nil?
56
56
  raise Informative, "has_many relation can't be created with optional model name" if model_name.end_with? "?"
57
- association = Record::Association.new(name, current_model.name, model_name, :has_many)
57
+ association = Record::Association.new(name, current_model.name, model_name, :has_many, args)
58
58
  @associations << association
59
59
  end
60
60
 
61
- def belongs_to(name, model_name = nil)
61
+ def belongs_to(name, model_name = nil, **args)
62
62
  model_name = name.to_s.singularize.camelize if model_name.nil?
63
- association = Record::Association.new(name, current_model.name, model_name, :belongs_to)
63
+ association = Record::Association.new(name, current_model.name, model_name, :belongs_to, args)
64
64
  @associations << association
65
65
  end
66
66
 
@@ -26,6 +26,18 @@ module MetaModel
26
26
  association
27
27
  end
28
28
 
29
+ satisfy_constraint = @associations.reduce([]) do |remain, association|
30
+ expect = remain.select { |assoc| assoc.expect_constraint? association }
31
+ if expect.empty?
32
+ remain << association
33
+ else
34
+ remain.delete expect.first
35
+ end
36
+ remain
37
+ end
38
+ raise Informative, "Unsatisfied constraints in #{satisfy_constraint.map { |x| x.major_model.name }}" \
39
+ if satisfy_constraint.size > 0
40
+
29
41
  @associations.each do |association|
30
42
  major_model = association.major_model
31
43
  secondary_model = association.secondary_model
@@ -3,6 +3,7 @@ require 'git'
3
3
  module MetaModel
4
4
  class Command
5
5
  class Build < Command
6
+ include Config::Mixin
6
7
  require 'metamodel/command/build/resolver'
7
8
  require 'metamodel/command/build/renderer'
8
9
  require 'metamodel/command/build/translator'
@@ -27,7 +28,7 @@ module MetaModel
27
28
  validate_models
28
29
  render_model_files
29
30
  update_initialize_method
30
- build_metamodel_framework
31
+ build_metamodel_framework unless config.skip_build?
31
32
  end
32
33
  UI.notice "Please drag MetaModel.framework into Embedded Binaries phrase.\n"
33
34
  end
@@ -130,6 +131,7 @@ module MetaModel
130
131
  CURRENT_SUPPORTED_BUILT_IN_TYPES = %w[
131
132
  Int
132
133
  Double
134
+ Float
133
135
  String
134
136
  Bool
135
137
  NSDate
@@ -23,6 +23,10 @@ module MetaModel
23
23
  "c" => "clean"
24
24
  }
25
25
 
26
+ METAMODEL_OPTION_ALIAS = {
27
+ "-s" => "--skip-build"
28
+ }
29
+
26
30
  def self.run(argv)
27
31
  if METAMODEL_COMMAND_ALIAS[argv.first]
28
32
  super([METAMODEL_COMMAND_ALIAS[argv.first]] + argv[1..-1])
@@ -31,10 +35,17 @@ module MetaModel
31
35
  end
32
36
  end
33
37
 
38
+ def self.options
39
+ [
40
+ ['--skip-build', 'Skip building MetaModel framework process']
41
+ ].concat(super)
42
+ end
43
+
34
44
  def initialize(argv)
35
- super
45
+ config.skip_build = argv.flag?("skip-build", false)
36
46
  # config.verbose = self.verbose?
37
47
  config.verbose = true
48
+ super
38
49
  end
39
50
 
40
51
  #-------------------------------------------------------------------------#
@@ -8,10 +8,15 @@ module MetaModel
8
8
  DEFAULTS = {
9
9
  :verbose => true,
10
10
  :silent => false,
11
+ :skip_build => false,
11
12
  }
12
13
 
13
14
  public
14
15
 
16
+ attr_accessor :skip_build
17
+ alias_method :skip_build?, :skip_build
18
+
19
+
15
20
  #-------------------------------------------------------------------------#
16
21
 
17
22
  # @!group UI
@@ -5,17 +5,58 @@ module MetaModel
5
5
  attr_reader :type
6
6
  attr_reader :relation
7
7
  attr_reader :through
8
+ attr_reader :dependent
8
9
  attr_accessor :major_model
9
10
  attr_accessor :secondary_model
10
11
 
11
- def initialize(name, major_model, secondary_model, relation, through = nil)
12
+ def initialize(name, major_model, secondary_model, relation, args)
13
+ through = args[:through]
14
+ dependent = args[:dependent] || :nullify
15
+
12
16
  @name = name.to_s.camelize :lower
13
17
  @relation = relation
14
18
  @through = through
19
+ @dependent = dependent
15
20
  @major_model = major_model
16
21
  @secondary_model = secondary_model
22
+
23
+ validate_association
24
+ end
25
+
26
+ def expect_constraint?(constraint)
27
+ result = true
28
+ result &= self.major_model == constraint.secondary_model
29
+ result &= self.secondary_model == constraint.major_model
30
+ result &= case [self.relation, constraint.relation]
31
+ when [:has_one, :belongs_to], [:belongs_to, :has_one] then true
32
+ when [:belongs_to, :has_many], [:has_many, :belongs_to] then true
33
+ when [:has_many, :has_many] then
34
+ self.through == constraint.through
35
+ else false
36
+ end
37
+ result
38
+ end
39
+
40
+ #-------------------------------------------------------------------------#
41
+
42
+ # @!group Validation
43
+
44
+ def validate_association
45
+ validate_dependent(@dependent)
46
+ end
47
+
48
+ def validate_dependent(dependent)
49
+ supported_dependent_options = [:nullify, :destroy]
50
+ raise Informative, "Unknown dependent option #{dependent}, \
51
+ MetaModel only supports #{supported_dependent_options} now" \
52
+ unless supported_dependent_options.include? dependent
17
53
  end
18
54
 
55
+
56
+ #-------------------------------------------------------------------------#
57
+
58
+ # @!group Relation
59
+
19
60
  def has_one?
20
61
  @relation == :has_one
21
62
  end
@@ -32,7 +73,7 @@ module MetaModel
32
73
  case @relation
33
74
  when :has_one then secondary_model.name
34
75
  when :has_many then secondary_model.name
35
- when :belongs_to then major_model.name
76
+ when :belongs_to then secondary_model.name
36
77
  end
37
78
  end
38
79
  end
@@ -34,10 +34,8 @@ module MetaModel
34
34
  def database_type
35
35
  case type_without_optional
36
36
  when "String" then "TEXT"
37
- when "Int" then "INTEGER"
38
- when "Bool" then "INTEGER"
39
- when "Double" then "REAL"
40
- when "NSDate" then "REAL"
37
+ when "Int", "Bool" then "INTEGER"
38
+ when "Double", "NSDate", "Float" then "REAL"
41
39
  else raise Informative, "Unsupported type #{self.type}"
42
40
  end
43
41
  end
@@ -45,10 +43,8 @@ module MetaModel
45
43
  def real_type
46
44
  case type_without_optional
47
45
  when "String" then "String"
48
- when "Int" then "Int64"
49
- when "Bool" then "Int64"
50
- when "Double" then "Double"
51
- when "NSDate" then "Double"
46
+ when "Int", "Bool" then "Int64"
47
+ when "Double", "NSDate", "Float" then "Double"
52
48
  else raise Informative, "Unsupported type #{self.type}"
53
49
  end
54
50
  end
@@ -1,3 +1,4 @@
1
+ // MARK: - Association
1
2
  <% model.associations.each do |association| %><% if association.has_many? %>
2
3
  <%= """public extension #{model.name} {
3
4
  func append#{association.type}(element: #{association.type}) {
@@ -0,0 +1,35 @@
1
+ // MAKR: - Helper
2
+
3
+ public class <%= model.relation_name %>: Relation<<%= model.name %>> {
4
+ override init() {
5
+ super.init()
6
+ self.select = "SELECT \(<%= model.name %>.tableName.unwrapped).* FROM \(<%= model.name %>.tableName.unwrapped)"
7
+ }
8
+
9
+ override var result: [<%= model.name %>] {
10
+ get {
11
+ var models: [<%= model.name %>] = []
12
+ guard let stmt = executeSQL(query) else { return models }
13
+ for values in stmt {
14
+ models.append(<%= model.name %>(values: values))
15
+ }
16
+ return models
17
+ }
18
+ }
19
+
20
+ func expandColumn(column: <%= model.name %>.Column) -> String {
21
+ return "\(<%= model.name %>.tableName.unwrapped).\(column.unwrapped)"
22
+ }
23
+ }
24
+
25
+ extension <%= model.name %> {
26
+ init(values: Array<Optional<Binding>>) {
27
+ <% model.properties.each_with_index do |property, index| %><%= """let #{property.name}: #{property.real_type} = values[#{index+1}] as! #{property.real_type}""" %>
28
+ <% end %>
29
+ self.init(<%= model.property_key_value_pairs true %>)
30
+ }
31
+ }
32
+
33
+ public extension <%= model.name %> {
34
+ var itself: String { get { return "WHERE \(Article.tableName.unwrapped).\("id".unwrapped) = \(id)" } }
35
+ }
@@ -0,0 +1,24 @@
1
+ // MARK: - Delete
2
+
3
+ public extension <%= model.name %> {
4
+ var delete: Bool {
5
+ get {
6
+ let deleteSQL = "DELETE FROM \(<%= model.name %>.tableName.unwrapped) \(itself)"
7
+ executeSQL(deleteSQL)
8
+ return true
9
+ }
10
+ }
11
+ static func deleteAll() {
12
+ let deleteAllSQL = "DELETE FROM \(tableName.unwrapped)"
13
+ executeSQL(deleteAllSQL)
14
+ }
15
+ }
16
+
17
+ public extension <%= model.relation_name %> {
18
+ var deleteAll: Bool {
19
+ get {
20
+ self.result.forEach { $0.delete }
21
+ return true
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,41 @@
1
+ public struct <%= model.name %> {
2
+ <% model.properties.each do |property| %><%= """public var #{property.name}: #{property.type}""" %>
3
+ <% end %>
4
+ static let tableName = "<%= model.table_name %>"
5
+
6
+ public enum Column: String, Unwrapped {
7
+ <% model.properties.each do |property| %><%= """case #{property.name} = \"#{property.name}\"""" %>
8
+ <% end %>
9
+ var unwrapped: String { get { return self.rawValue.unwrapped } }
10
+ }
11
+
12
+ public init(<%= model.property_key_type_pairs %>) {
13
+ <% model.properties.each do |property| %><%= """self.#{property.name} = #{property.name}" %>
14
+ <% end %>
15
+ }
16
+
17
+ static public func new(<%= model.property_key_type_pairs %>) -> <%= model.name %> {
18
+ return <%= model.name %>(<%= model.property_key_value_pairs %>)
19
+ }
20
+
21
+ static public func create(<%= model.property_key_type_pairs %>) -> <%= model.name %>? {
22
+ if <%= model.properties.select { |p| p.name.downcase.end_with? "id" }.map { |p| "#{p.name} == 0" }.join(" || ") %> { return nil }
23
+
24
+ var columnsSQL: [<%= model.name %>.Column] = []
25
+ var valuesSQL: [Unwrapped] = []
26
+
27
+ columnsSQL.append(.id)
28
+ valuesSQL.append(id)
29
+ <% model.properties_exclude_id.each do |property| %><% if property.is_optional? %>
30
+ <%= """if let #{property.name} = #{property.name} {
31
+ columnsSQL.append(.#{property.name})
32
+ valuesSQL.append(#{property.name})
33
+ }""" %><% else %>
34
+ <%= """columnsSQL.append(.#{property.name})
35
+ valuesSQL.append(#{property.name})
36
+ """ %><% end %><% end %>
37
+ let insertSQL = "INSERT INTO \(tableName.unwrapped) (\(columnsSQL.map { $0.rawValue }.joinWithSeparator(", "))) VALUES (\(valuesSQL.map { $0.unwrapped }.joinWithSeparator(", ")))"
38
+ guard let _ = executeSQL(insertSQL) else { return nil }
39
+ return <%= model.name %>(<%= model.property_key_value_pairs %>)
40
+ }
41
+ }
@@ -1,3 +1,5 @@
1
+ // MARK: - Query
2
+
1
3
  public extension <%= model.name %> {
2
4
  static var all: <%= model.relation_name %> {
3
5
  get { return <%= model.relation_name %>() }
@@ -71,3 +73,72 @@ public extension <%= model.name %> {
71
73
  return <%= model.relation_name %>().orderBy(column, asc: asc)
72
74
  }
73
75
  }
76
+
77
+ public extension <%= model.relation_name %> {
78
+ func find(id: Int) -> Self {
79
+ return self.findBy(id: id)
80
+ }
81
+
82
+ func findBy(id id: Int) -> Self {
83
+ return self.filter([.id: id]).limit(1)
84
+ }
85
+
86
+ <% model.properties_exclude_id.each do |property| %><%= """func findBy(#{property.name} #{property.name}: #{property.type_without_optional}) -\> Self {
87
+ return self.filter([.#{property.name}: #{property.name}])
88
+ }""" %>
89
+ <% end %>
90
+ func filter(conditions: [<%= model.name %>.Column: Any]) -> Self {
91
+ for (column, value) in conditions {
92
+ let columnSQL = "\(expandColumn(column))"
93
+
94
+ func filterByEqual(value: Any) {
95
+ self.filter.append("\(columnSQL) = \(value)")
96
+ }
97
+
98
+ func filterByIn(value: [String]) {
99
+ self.filter.append("\(columnSQL) IN (\(value.joinWithSeparator(", ")))")
100
+ }
101
+
102
+ if let value = value as? String {
103
+ filterByEqual(value.unwrapped)
104
+ } else if let value = value as? Int {
105
+ filterByEqual(value)
106
+ } else if let value = value as? Double {
107
+ filterByEqual(value)
108
+ } else if let value = value as? [String] {
109
+ filterByIn(value.map { $0.unwrapped })
110
+ } else if let value = value as? [Int] {
111
+ filterByIn(value.map { $0.description })
112
+ } else if let value = value as? [Double] {
113
+ filterByIn(value.map { $0.description })
114
+ } else {
115
+ let valueMirror = Mirror(reflecting: value)
116
+ print("!!!: UNSUPPORTED TYPE \(valueMirror.subjectType)")
117
+ }
118
+
119
+ }
120
+ return self
121
+ }
122
+
123
+ func groupBy(columns: <%= model.name %>.Column...) -> Self {
124
+ return self.groupBy(columns)
125
+ }
126
+
127
+ func groupBy(columns: [<%= model.name %>.Column]) -> Self {
128
+ func groupBy(column: <%= model.name %>.Column) {
129
+ self.group.append("\(expandColumn(column))")
130
+ }
131
+ _ = columns.flatMap(groupBy)
132
+ return self
133
+ }
134
+
135
+ func orderBy(column: <%= model.name %>.Column) -> Self {
136
+ self.order.append("\(expandColumn(column))")
137
+ return self
138
+ }
139
+
140
+ func orderBy(column: <%= model.name %>.Column, asc: Bool) -> Self {
141
+ self.order.append("\(expandColumn(column)) \(asc ? "ASC".unwrapped : "DESC".unwrapped)")
142
+ return self
143
+ }
144
+ }
@@ -1,18 +1,10 @@
1
- public extension <%= model.name %> {
2
- var itself: String { get { return "WHERE \(<%= model.name %>.tableName.unwrapped).\("id".unwrapped) = \(id)" } }
3
-
4
- var delete: Bool {
5
- get {
6
- let deleteSQL = "DELETE FROM \(<%= model.name %>.tableName.unwrapped) \(itself)"
7
- executeSQL(deleteSQL)
8
- return true
9
- }
10
- }
1
+ // MARK: - Update
11
2
 
3
+ public extension <%= model.name %> {
12
4
  <% model.properties_exclude_id.each do |property| %><%= """mutating func update(#{property.name} #{property.name}: #{property.type}) -> #{model.name} {
13
5
  return self.update([.#{property.name}: #{property.name}])
14
- }""" %>
15
- <% end %>
6
+ }
7
+ """ %><% end %>
16
8
  mutating func update(attributes: [<%= model.name %>.Column: Any]) -> <%= model.name %> {
17
9
  var setSQL: [String] = []
18
10
  if let attributes = attributes as? [<%= model.name %>.Column: Unwrapped] {
@@ -45,3 +37,12 @@ public extension <%= model.name %> {
45
37
  }
46
38
  }
47
39
  }
40
+
41
+ public extension <%= model.relation_name %> {
42
+ public func updateAll(column: <%= model.name %>.Column, value: Any) {
43
+ self.result.forEach { (element) in
44
+ var element = element
45
+ element.update([column: value])
46
+ }
47
+ }
48
+ }
@@ -1,9 +1,4 @@
1
1
  public extension <%= model.name %> {
2
- static func deleteAll() {
3
- let deleteAllSQL = "DELETE FROM \(tableName.unwrapped)"
4
- executeSQL(deleteAllSQL)
5
- }
6
-
7
2
  static var count: Int {
8
3
  get {
9
4
  let countSQL = "SELECT count(*) FROM \(tableName.unwrapped)"
@@ -11,30 +6,4 @@ public extension <%= model.name %> {
11
6
  return Int(count)
12
7
  }
13
8
  }
14
-
15
- static func new(<%= model.property_key_type_pairs %>) -> <%= model.name %> {
16
- return <%= model.name %>(<%= model.property_key_value_pairs %>)
17
- }
18
-
19
- static func create(<%= model.property_key_type_pairs %>) -> <%= model.name %>? {
20
- if <%= model.properties.select { |p| p.name.downcase.end_with? "id" }.map { |p| "#{p.name} == 0" }.join(" || ") %> { return nil }
21
-
22
- var columnsSQL: [<%= model.name %>.Column] = []
23
- var valuesSQL: [Unwrapped] = []
24
-
25
- columnsSQL.append(.id)
26
- valuesSQL.append(id)
27
- <% model.properties_exclude_id.each do |property| %><% if property.is_optional? %>
28
- <%= """if let #{property.name} = #{property.name} {
29
- columnsSQL.append(.#{property.name})
30
- valuesSQL.append(#{property.name})
31
- }""" %><% else %>
32
- <%= """columnsSQL.append(.#{property.name})
33
- valuesSQL.append(#{property.name})
34
- """ %><% end %><% end %>
35
-
36
- let insertSQL = "INSERT INTO \(tableName.unwrapped) (\(columnsSQL.map { $0.rawValue }.joinWithSeparator(", "))) VALUES (\(valuesSQL.map { $0.unwrapped }.joinWithSeparator(", ")))"
37
- guard let _ = executeSQL(insertSQL) else { return nil }
38
- return <%= model.name %>(<%= model.property_key_value_pairs %>)
39
- }
40
9
  }
@@ -1,5 +1,5 @@
1
1
  module MetaModel
2
2
  # The version of the MetaModel command line tool.
3
3
  #
4
- VERSION = '0.1.6' unless defined? MetaModel::VERSION
4
+ VERSION = '0.1.7' unless defined? MetaModel::VERSION
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metamodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Draveness Zuo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-10 00:00:00.000000000 Z
11
+ date: 2016-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide
@@ -158,17 +158,17 @@ files:
158
158
  - lib/metamodel/record/association.rb
159
159
  - lib/metamodel/record/model.rb
160
160
  - lib/metamodel/record/property.rb
161
- - lib/metamodel/template/attributes.swift
162
161
  - lib/metamodel/template/file_header.swift
163
162
  - lib/metamodel/template/foreign_key.swift
164
- - lib/metamodel/template/initialize.swift
165
- - lib/metamodel/template/instance_methods.swift
163
+ - lib/metamodel/template/helper.swift
166
164
  - lib/metamodel/template/json.swift
167
165
  - lib/metamodel/template/metamodel.swift
166
+ - lib/metamodel/template/model_delete.swift
167
+ - lib/metamodel/template/model_initialize.swift
168
168
  - lib/metamodel/template/model_query.swift
169
- - lib/metamodel/template/model_relation.swift
170
- - lib/metamodel/template/recordable.swift
169
+ - lib/metamodel/template/model_update.swift
171
170
  - lib/metamodel/template/static_methods.swift
171
+ - lib/metamodel/template/table_initialize.swift
172
172
  - lib/metamodel/user_interface.rb
173
173
  - lib/metamodel/version.rb
174
174
  homepage: https://github.com/MModel/MetaModel
@@ -1,11 +0,0 @@
1
- public struct <%= model.name %> {
2
- <% model.properties.each do |property| %><%= """public var #{property.name}: #{property.type}""" %>
3
- <% end %>
4
- static let tableName = "<%= model.table_name %>"
5
-
6
- public enum Column: String, Unwrapped {
7
- <% model.properties.each do |property| %><%= """case #{property.name} = \"#{property.name}\"""" %>
8
- <% end %>
9
- var unwrapped: String { get { return self.rawValue.unwrapped } }
10
- }
11
- }
@@ -1,95 +0,0 @@
1
- public class <%= model.relation_name %>: Relation<<%= model.name %>> {
2
- override init() {
3
- super.init()
4
- self.select = "SELECT \(<%= model.name %>.tableName.unwrapped).* FROM \(<%= model.name %>.tableName.unwrapped)"
5
- }
6
-
7
- func expandColumn(column: <%= model.name %>.Column) -> String {
8
- return "\(<%= model.name %>.tableName.unwrapped).\(column.unwrapped)"
9
- }
10
-
11
- // MARK: Query
12
-
13
- public func find(id: Int) -> Self {
14
- return self.findBy(id: id)
15
- }
16
-
17
- public func findBy(id id: Int) -> Self {
18
- return self.filter([.id: id]).limit(1)
19
- }
20
-
21
- <% model.properties_exclude_id.each do |property| %>
22
- <%= """public func findBy(#{property.name} #{property.name}: #{property.type_without_optional}) -\> Self {
23
- return self.filter([.#{property.name}: #{property.name}])
24
- }""" %>
25
- <% end %>
26
-
27
- public func filter(conditions: [<%= model.name %>.Column: Any]) -> Self {
28
- for (column, value) in conditions {
29
- let columnSQL = "\(expandColumn(column))"
30
-
31
- func filterByEqual(value: Any) {
32
- self.filter.append("\(columnSQL) = \(value)")
33
- }
34
-
35
- func filterByIn(value: [String]) {
36
- self.filter.append("\(columnSQL) IN (\(value.joinWithSeparator(", ")))")
37
- }
38
-
39
- if let value = value as? String {
40
- filterByEqual(value.unwrapped)
41
- } else if let value = value as? Int {
42
- filterByEqual(value)
43
- } else if let value = value as? Double {
44
- filterByEqual(value)
45
- } else if let value = value as? [String] {
46
- filterByIn(value.map { $0.unwrapped })
47
- } else if let value = value as? [Int] {
48
- filterByIn(value.map { $0.description })
49
- } else if let value = value as? [Double] {
50
- filterByIn(value.map { $0.description })
51
- } else {
52
- let valueMirror = Mirror(reflecting: value)
53
- print("!!!: UNSUPPORTED TYPE \(valueMirror.subjectType)")
54
- }
55
-
56
- }
57
- return self
58
- }
59
-
60
- public func groupBy(columns: <%= model.name %>.Column...) -> Self {
61
- return self.groupBy(columns)
62
- }
63
-
64
- public func groupBy(columns: [<%= model.name %>.Column]) -> Self {
65
- func groupBy(column: <%= model.name %>.Column) {
66
- self.group.append("\(expandColumn(column))")
67
- }
68
- _ = columns.flatMap(groupBy)
69
- return self
70
- }
71
-
72
- public func orderBy(column: <%= model.name %>.Column) -> Self {
73
- self.order.append("\(expandColumn(column))")
74
- return self
75
- }
76
-
77
- public func orderBy(column: <%= model.name %>.Column, asc: Bool) -> Self {
78
- self.order.append("\(expandColumn(column)) \(asc ? "ASC".unwrapped : "DESC".unwrapped)")
79
- return self
80
- }
81
-
82
- public func updateAll(column: <%= model.name %>.Column, value: Any) {
83
- self.result.forEach { (element) in
84
- var element = element
85
- element.update([column: value])
86
- }
87
- }
88
-
89
- public var deleteAll: Bool {
90
- get {
91
- self.result.forEach { $0.delete }
92
- return true
93
- }
94
- }
95
- }
@@ -1,7 +0,0 @@
1
- extension <%= model.name %>: Recordable {
2
- public init(values: Array<Optional<Binding>>) {
3
- <% model.properties.each_with_index do |property, index| %><%= """let #{property.name}: #{property.real_type} = values[#{index+1}] as! #{property.real_type}""" %>
4
- <% end %>
5
- self.init(<%= model.property_key_value_pairs true %>)
6
- }
7
- }