metamodel 0.1.7 → 0.1.8

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: 0c119cc6df05af0b21c4f244f1b496f5f91ec517
4
- data.tar.gz: 1faf793b51be72320af814d5d8ebcd14e575d420
3
+ metadata.gz: 150f0889587f6a3379d1dd291465dc4aea20c5c0
4
+ data.tar.gz: 616832c86c039688d8c06fdcc59076aa8745704f
5
5
  SHA512:
6
- metadata.gz: 76072a392abaa5c37d6609702f88d4ee6ee9a7087bc43a4c8f22c0677ec0df910f1da5c4a2bfcb908e9fe6e170f5cfb61d6b2038e70afa3a0754f02db480b989
7
- data.tar.gz: e3b57aca292f8b915b6a116b974f0896506012cbc7a421e15c3a1a3f72d3eec6ff9a79b5ce7cb76dbc6f9d612b2bc47c06818d40ef1f2c40937e2c06a419bb95
6
+ metadata.gz: f6a3cd5f7a04e4284887c52574cb50a7622f6aece76de076881176c1a803dffd785e5d9778d8d94aa7de6e4fcb2c0f038ad8acafa897e2be9adba9648b7b341e
7
+ data.tar.gz: 8b2e17fc792955ce237d907e71024a1637fa70d414716c49ffe98419b10f7efa44bf3e0a8634edfe3fe0caa30e95476ce649de3df52f4769b0f9c7cfe4b112c9
@@ -82,14 +82,16 @@ module MetaModel
82
82
  BITCODE_GENERATION_MODE=bitcode \
83
83
  ONLY_ACTIVE_ARCH=NO \
84
84
  CODE_SIGNING_REQUIRED=NO \
85
- CODE_SIGN_IDENTITY="
85
+ CODE_SIGN_IDENTITY= \
86
+ clean build"
86
87
  build_iphonesimulator = "xcodebuild -scheme MetaModel \
87
88
  -project MetaModel/MetaModel.xcodeproj \
88
89
  -configuration Release -sdk iphonesimulator \
89
90
  -derivedDataPath './metamodel' \
90
91
  ONLY_ACTIVE_ARCH=NO \
91
92
  CODE_SIGNING_REQUIRED=NO \
92
- CODE_SIGN_IDENTITY="
93
+ CODE_SIGN_IDENTITY= \
94
+ clean build"
93
95
  result = system "#{build_iphoneos} > /dev/null &&
94
96
  #{build_iphonesimulator} > /dev/null"
95
97
 
@@ -37,6 +37,14 @@ module MetaModel
37
37
  result
38
38
  end
39
39
 
40
+ def secondary_model_instance
41
+ case relation
42
+ when :has_many, :has_one then "#{secondary_model.name}.findBy(#{major_model.foreign_id}: id)"
43
+ when :belongs_to then "#{secondary_model.name}.findBy(id: #{secondary_model.foreign_id})"
44
+ else ""
45
+ end
46
+ end
47
+
40
48
  #-------------------------------------------------------------------------#
41
49
 
42
50
  # @!group Validation
@@ -13,6 +13,10 @@ module MetaModel
13
13
  validate
14
14
  end
15
15
 
16
+ def contains?(property)
17
+ @properties.select { |prop| prop.name == property }.size > 0
18
+ end
19
+
16
20
  def properties_exclude_id
17
21
  @properties.select { |property| property.name != "id" }
18
22
  end
@@ -57,16 +61,16 @@ module MetaModel
57
61
  key_value_pairs_with_property properties_exclude_id, cast
58
62
  end
59
63
 
60
- def property_key_type_pairs
61
- key_type_pairs_with_property @properties
64
+ def property_key_type_pairs(remove_extra_param = true, use_default_value = false)
65
+ key_type_pairs_with_property @properties, remove_extra_param, use_default_value
62
66
  end
63
67
 
64
- def property_key_type_pairs_without_property(property)
65
- key_type_pairs_with_property @properties.select { |element| element.name != property }
68
+ def property_exclude_id_key_type_pairs(remove_extra_param = true, use_default_value = false)
69
+ key_type_pairs_with_property properties_exclude_id, remove_extra_param, use_default_value
66
70
  end
67
71
 
68
- def property_exclude_id_key_type_pairs
69
- key_type_pairs_with_property properties_exclude_id
72
+ def property_key_type_pairs_without_property(property)
73
+ key_type_pairs_with_property @properties.select { |element| element.name != property }
70
74
  end
71
75
 
72
76
  def build_table
@@ -99,13 +103,14 @@ module MetaModel
99
103
  end.join(", ")
100
104
  end
101
105
 
102
- def key_type_pairs_with_property(properties)
106
+ def key_type_pairs_with_property(properties, remove_extra_param = true, use_default_value = false)
103
107
  properties.enum_for(:each_with_index).map do |property, index|
104
108
  has_default_value = property.has_default_value?
105
109
  default_value = property.type_without_optional == "String" ? "\"#{property.default_value}\"" : property.default_value
106
110
 
107
111
  result = "#{property.name}: #{property.type.to_s}#{if has_default_value then " = " + "#{default_value}" end}"
108
- if index == 0
112
+ result = "#{property.name}: #{property.type.to_s} = #{property.type_without_optional}DefaultValue" if use_default_value
113
+ if index == 0 && remove_extra_param
109
114
  "#{property.name} #{result}"
110
115
  else
111
116
  result
@@ -11,11 +11,11 @@
11
11
  }
12
12
 
13
13
  func delete#{association.type}(id: Int) {
14
- #{association.type}.filter(.#{model.foreign_id}, value: id).findBy(id: id).first?.delete
14
+ #{association.type}.findBy(#{model.foreign_id}: id).first?.delete
15
15
  }
16
16
  var #{association.name}: [#{association.type}] {
17
17
  get {
18
- return #{association.type}.filter(.id, value: id).result
18
+ return #{association.type}.filter(id: id).result
19
19
  }
20
20
  set {
21
21
  #{association.name}.forEach { (element) in
@@ -32,7 +32,7 @@
32
32
  <%= """public extension #{model.name} {
33
33
  var #{association.name}: #{association.type}? {
34
34
  get {
35
- return #{association.type}.find(id)
35
+ return #{association.secondary_model_instance}.first
36
36
  }
37
37
  set {
38
38
  guard let newValue = newValue else { return }
@@ -44,10 +44,10 @@
44
44
  <%= """public extension #{model.name} {
45
45
  var #{association.name}: #{association.type}? {
46
46
  get {
47
- return #{association.type}.find(id)
47
+ return #{association.secondary_model_instance}.first
48
48
  }
49
49
  set {
50
- #{association.type}.filter(.#{model.foreign_id}, value: id).deleteAll
50
+ #{association.type}.findBy(#{model.foreign_id}: id).deleteAll
51
51
  guard var newValue = newValue else { return }
52
52
  newValue.update(#{model.foreign_id}: id)
53
53
  }
@@ -30,6 +30,6 @@ extension <%= model.name %> {
30
30
  }
31
31
  }
32
32
 
33
- public extension <%= model.name %> {
33
+ extension <%= model.name %> {
34
34
  var itself: String { get { return "WHERE \(Article.tableName.unwrapped).\("id".unwrapped) = \(id)" } }
35
35
  }
@@ -29,57 +29,3 @@ public class MetaModel {
29
29
  <% end %>
30
30
  }
31
31
  }
32
-
33
- func executeSQL(sql: String, verbose: Bool = false, suppress: Bool = false, success: (() -> ())? = nil) -> Statement? {
34
- if verbose {
35
- print("-> Begin Transaction")
36
- }
37
- let startDate = NSDate()
38
- do {
39
- let result = try db.run(sql)
40
- let endDate = NSDate()
41
- let interval = endDate.timeIntervalSinceDate(startDate) * 1000
42
-
43
- if verbose {
44
- print("\tSQL (\(interval.format("0.2"))ms) \(sql)")
45
- print("-> Commit Transaction")
46
- print("\n")
47
- }
48
- if let success = success {
49
- success()
50
- }
51
-
52
- return result
53
- } catch let error {
54
- let endDate = NSDate()
55
- let interval = endDate.timeIntervalSinceDate(startDate) * 1000
56
- print("\tSQL (\(interval.format("0.2"))ms) \(sql)")
57
- print("\t\(error)")
58
- if verbose {
59
- print("-> Rollback transaction")
60
- print("\n")
61
- }
62
- }
63
- return nil
64
- }
65
-
66
- func executeScalarSQL(sql: String, verbose: Bool = false, suppress: Bool = false, success: (() -> ())? = nil) -> Binding? {
67
- if verbose {
68
- print("-> Begin Transaction")
69
- }
70
- let startDate = NSDate()
71
- let result = db.scalar(sql)
72
- let endDate = NSDate()
73
- let interval = endDate.timeIntervalSinceDate(startDate) * 1000
74
- if verbose {
75
- print("\tSQL (\(interval.format("0.2"))ms) \(sql)")
76
- print("-> Commit Transaction")
77
- print("\n")
78
- }
79
-
80
- if let success = success {
81
- success()
82
- }
83
-
84
- return result
85
- }
@@ -4,17 +4,19 @@ public extension <%= model.name %> {
4
4
  var delete: Bool {
5
5
  get {
6
6
  let deleteSQL = "DELETE FROM \(<%= model.name %>.tableName.unwrapped) \(itself)"
7
- executeSQL(deleteSQL)
7
+ executeSQL(deleteSQL)<% model.associations.each do |association| %>
8
+ <%= association.secondary_model_instance + ".delete" if association.dependent == :destroy %>
9
+ <%= association.secondary_model_instance + ".update(#{model.foreign_id}: 0)" if association.dependent == :nullify && model.contains?(model.foreign_id) %>
10
+ <% end %>
8
11
  return true
9
12
  }
10
13
  }
11
- static func deleteAll() {
12
- let deleteAllSQL = "DELETE FROM \(tableName.unwrapped)"
13
- executeSQL(deleteAllSQL)
14
- }
14
+ static var deleteAll: Bool { get { return <%= model.relation_name %>().deleteAll } }
15
15
  }
16
16
 
17
17
  public extension <%= model.relation_name %> {
18
+ var delete: Bool { get { return deleteAll } }
19
+
18
20
  var deleteAll: Bool {
19
21
  get {
20
22
  self.result.forEach { $0.delete }
@@ -9,7 +9,7 @@ public struct <%= model.name %> {
9
9
  var unwrapped: String { get { return self.rawValue.unwrapped } }
10
10
  }
11
11
 
12
- public init(<%= model.property_key_type_pairs %>) {
12
+ public init(<%= model.property_key_type_pairs false %>) {
13
13
  <% model.properties.each do |property| %><%= """self.#{property.name} = #{property.name}" %>
14
14
  <% end %>
15
15
  }
@@ -29,20 +29,14 @@ public extension <%= model.name %> {
29
29
  return <%= model.relation_name %>().find(id).first
30
30
  }
31
31
 
32
- static func findBy(id id: Int) -> <%= model.name %>? {
33
- return <%= model.relation_name %>().findBy(id: id).first
34
- }
35
- <% model.properties_exclude_id.each do |property| %><%= """
36
- static func findBy(#{property.name} #{property.name}: #{property.type_without_optional}) -\> #{model.name}? {
37
- return #{model.relation_name}().findBy(#{property.name}: #{property.name}).first
38
- }
39
- """ %><% end %>
40
- static func filter(column: <%= model.name %>.Column, value: Any) -> <%= model.relation_name %> {
41
- return <%= model.relation_name %>().filter([column: value])
32
+ static func findBy(<%= model.property_key_type_pairs(true, true) %>) -> <%= model.relation_name %> {
33
+ var attributes: [<%= model.name %>.Column: Any] = [:]
34
+ <% model.properties.each do |property| %><%= "if (#{property.name} != #{property.type_without_optional}DefaultValue) { attributes[.#{property.name}] = #{property.name} }" %>
35
+ <% end %>return <%= model.relation_name %>().filter(attributes)
42
36
  }
43
37
 
44
- static func filter(conditions: [<%= model.name %>.Column: Any]) -> <%= model.relation_name %> {
45
- return <%= model.relation_name %>().filter(conditions)
38
+ static func filter(<%= model.property_key_type_pairs(true, true) %>) -> <%= model.relation_name %> {
39
+ return findBy(<%= model.property_key_value_pairs %>)
46
40
  }
47
41
 
48
42
  static func limit(length: UInt, offset: UInt = 0) -> <%= model.relation_name %> {
@@ -76,17 +70,19 @@ public extension <%= model.name %> {
76
70
 
77
71
  public extension <%= model.relation_name %> {
78
72
  func find(id: Int) -> Self {
79
- return self.findBy(id: id)
73
+ return findBy(id: id)
74
+ }
75
+
76
+ func findBy(<%= model.property_key_type_pairs(true, true) %>) -> Self {
77
+ var attributes: [<%= model.name %>.Column: Any] = [:]
78
+ <% model.properties.each do |property| %><%= "if (#{property.name} != #{property.type_without_optional}DefaultValue) { attributes[.#{property.name}] = #{property.name} }" %>
79
+ <% end %>return self.filter(attributes)
80
80
  }
81
81
 
82
- func findBy(id id: Int) -> Self {
83
- return self.filter([.id: id]).limit(1)
82
+ func filter(<%= model.property_key_type_pairs(true, true) %>) -> Self {
83
+ return findBy(<%= model.property_key_value_pairs %>)
84
84
  }
85
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
86
  func filter(conditions: [<%= model.name %>.Column: Any]) -> Self {
91
87
  for (column, value) in conditions {
92
88
  let columnSQL = "\(expandColumn(column))"
@@ -1,11 +1,14 @@
1
1
  // MARK: - Update
2
2
 
3
3
  public extension <%= model.name %> {
4
- <% model.properties_exclude_id.each do |property| %><%= """mutating func update(#{property.name} #{property.name}: #{property.type}) -> #{model.name} {
5
- return self.update([.#{property.name}: #{property.name}])
4
+ mutating func update(<%= model.property_exclude_id_key_type_pairs(true, true) %>) {
5
+ var attributes: [<%= model.name %>.Column: Any] = [:]
6
+ <% model.properties_exclude_id.each do |property| %><%= "if (#{property.name} != #{property.type_without_optional}DefaultValue) { attributes[.#{property.name}] = #{property.name} }" %>
7
+ <% end %>
8
+ self.update(attributes)
6
9
  }
7
- """ %><% end %>
8
- mutating func update(attributes: [<%= model.name %>.Column: Any]) -> <%= model.name %> {
10
+
11
+ mutating func update(attributes: [<%= model.name %>.Column: Any]) {
9
12
  var setSQL: [String] = []
10
13
  if let attributes = attributes as? [<%= model.name %>.Column: Unwrapped] {
11
14
  for (key, value) in attributes {
@@ -24,8 +27,8 @@ public extension <%= model.name %> {
24
27
  }
25
28
  }
26
29
  }
27
- return self
28
30
  }
31
+
29
32
  var save: <%= model.name %> {
30
33
  mutating get {
31
34
  if let _ = <%= model.name %>.find(id) {
@@ -36,13 +39,27 @@ public extension <%= model.name %> {
36
39
  return self
37
40
  }
38
41
  }
42
+
43
+ var commit: <%= model.name %> {
44
+ mutating get {
45
+ return save
46
+ }
47
+ }
39
48
  }
40
49
 
41
50
  public extension <%= model.relation_name %> {
42
- public func updateAll(column: <%= model.name %>.Column, value: Any) {
43
- self.result.forEach { (element) in
51
+ public func updateAll(<%= model.property_exclude_id_key_type_pairs(true, true) %>) -> Self {
52
+ return update(<%= model.property_exclude_id_key_value_pairs %>)
53
+ }
54
+
55
+ public func update(<%= model.property_exclude_id_key_type_pairs(true, true) %>) -> Self {
56
+ var attributes: [<%= model.name %>.Column: Any] = [:]
57
+ <% model.properties_exclude_id.each do |property| %><%= "if (#{property.name} != #{property.type_without_optional}DefaultValue) { attributes[.#{property.name}] = #{property.name} }" %>
58
+ <% end %>
59
+ result.forEach { (element) in
44
60
  var element = element
45
- element.update([column: value])
61
+ element.update(attributes)
46
62
  }
63
+ return self
47
64
  }
48
65
  }
@@ -1,5 +1,5 @@
1
1
  module MetaModel
2
2
  # The version of the MetaModel command line tool.
3
3
  #
4
- VERSION = '0.1.7' unless defined? MetaModel::VERSION
4
+ VERSION = '0.1.8' 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.7
4
+ version: 0.1.8
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-11 00:00:00.000000000 Z
11
+ date: 2016-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide