metamodel 0.1.1 → 0.1.5

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: 3d443b7aba6c0b4f80775f5d7e785d571cbcaee3
4
- data.tar.gz: 0a124cd657c3c8e1584d5a106666077cebeafab8
3
+ metadata.gz: 36ad33602070abacabd6daa90047708d6888e35b
4
+ data.tar.gz: bfbdbeeab3881c3e592b62c1084dc505f7e68d7e
5
5
  SHA512:
6
- metadata.gz: a56c26ba55af301427b5bc9c496230e3a5fb305e36b77360986e21be2fe5e5fc00339cbe3c2393c8e8d5aca3c42c24c2a39a2a1a7fa97bcc8f1be3b74007412c
7
- data.tar.gz: f4c5d900b9c36ea62a24c481cd2c47e7da71402b52f2e1b176f93752bfa44fb57df95632830dff20ee8775a6745c6e2a7487fecd11c91d701d1c5a0f2232a81c
6
+ metadata.gz: 0d464c4eac3190573387c23fb4f2ddd28bb632fbd17833bf618e23bdd64b7c0247f8039020d46d11b87801d269d5321e486b6eae952923955b55fb3bc64404ab
7
+ data.tar.gz: e8a655ea0d4e244f3239a3b1c94ce8dd5b1b6391ab45af7483cbad83313b395b8e8e906800070c439bc7280bf6206f9e9780284d8310d8222fd449ee9f515b81
data/README.md CHANGED
@@ -34,14 +34,22 @@ print(Person.all)
34
34
 
35
35
  ## Usage
36
36
 
37
- Use a meta file in `./meta` folder to define your model:
37
+ Use Metafile in project folder to define your model:
38
38
 
39
39
  ```ruby
40
- define :User do
41
- # define User model like this
42
- attr :nickname, :string
43
- attr :avatar, :string?
44
- attr :email, :string, :unique, default: "default@gmail.com"
40
+ metamodel_version '0.1.0'
41
+
42
+ define :Article do
43
+ attr :title
44
+ attr :content
45
+
46
+ has_many :comments
47
+ end
48
+
49
+ define :Comment do
50
+ attr :content
51
+
52
+ belongs_to :article
45
53
  end
46
54
  ```
47
55
 
@@ -53,8 +61,6 @@ And then run `meta build` will automatically generate all the code you need.
53
61
  sudo gem install metamodel --verbose
54
62
  ```
55
63
 
56
- **System Requirements**: Current version of MetaModel requires macOS with Ruby 2.2.2 or above
57
-
58
64
  ## Quick Start
59
65
 
60
66
  After installation , run `meta init` in your iOS project root folder which will make a `meta` directory in current folder.
@@ -65,21 +71,21 @@ $ meta init
65
71
 
66
72
  Initialing MetaModel project
67
73
 
68
- Creating `meta` folder for MetaModel
74
+ Creating `Metafile` for MetaModel
69
75
  ```
70
76
 
71
77
  Generate your model meta file with `meta generate`.
72
78
 
73
79
  ```shell
74
- $ meta generate User
80
+ $ meta generate Article
75
81
 
76
82
  Generating model meta file
77
83
 
78
- -> Adding `user.rb` to meta folder
84
+ -> Adding `Article` model to Metafile
79
85
 
80
- [!] `user.rb` has already generated, use the command below to edit it.
86
+ [!] Adding `Article` model scaffold to Metafile, use the command below to edit it.
81
87
 
82
- vim meta/user.rb
88
+ vim Metafile
83
89
  ```
84
90
 
85
91
  Edit meta file using vim, Emacs or other editor and run `meta build`.
@@ -90,14 +96,13 @@ $ meta build
90
96
  Building MetaModel.framework in project
91
97
  Existing project `./metamodel/MetaModel.xcodeproj`
92
98
 
93
- Analyzing meta files
94
- -> Resolving `user.rb`
99
+ Analyzing Metafile
100
+ -> Resolving `Article`
101
+ -> Resolving `Comment`
95
102
 
96
103
  Generating model files
97
- -> Using User.swift file
98
-
99
- Generating MetaModel.framework
100
- -> MetaModel.framework located in current folder
104
+ -> Using Article.swift file
105
+ -> Using Comment.swift file
101
106
 
102
107
  [!] Please drag MetaModel.framework into Embedded Binaries phrase.
103
108
  ```
@@ -14,14 +14,9 @@ module MetaModel
14
14
  end
15
15
 
16
16
  def parse
17
- UI.section "Analyzing meta files" do
18
- meta_path = config.meta_path
19
- metas = Dir[meta_path + "*.rb"]
20
- metas.each do |meta_file|
21
- UI.message '-> '.green + "Resolving `#{File.basename(meta_file)}`"
22
- meta_code = File.read(meta_path + meta_file)
23
- eval meta_code
24
- end
17
+ UI.section "Analyzing Metafile" do
18
+ metafile_path = config.metafile_path
19
+ eval File.read(metafile_path)
25
20
  end
26
21
  @models
27
22
  end
@@ -34,6 +29,7 @@ module MetaModel
34
29
  end
35
30
 
36
31
  def define(model_name)
32
+ UI.message '-> '.green + "Resolving `#{model_name.to_s.camelize}`"
37
33
  @models << Model.new(model_name)
38
34
  yield
39
35
  end
@@ -42,17 +38,20 @@ module MetaModel
42
38
  current_model.properties << Property.new(key, type, args)
43
39
  end
44
40
 
45
- def has_one(name, model)
41
+ def has_one(name, model = nil)
42
+ model = name.to_s.singularize.camelize if model.nil?
46
43
  current_model.relation_properties << Property.new(name, model, :has_one)
47
44
  end
48
45
 
49
- def has_many(name, model)
46
+ def has_many(name, model = nil)
47
+ model = name.to_s.singularize.camelize if model.nil?
50
48
  property = Property.new(name, model, :has_many)
51
49
  raise Informative, "Property type in has_many relation can't be optional" if property.is_optional?
52
50
  current_model.relation_properties << property
53
51
  end
54
52
 
55
- def belongs_to(name, model)
53
+ def belongs_to(name, model = nil)
54
+ model = name.to_s.singularize.camelize if model.nil?
56
55
  current_model.relation_properties << Property.new(name, model, :belongs_to)
57
56
  current_model.properties << Property.new("#{name}_id".camelize, "Int", :foreign, :default => 0)
58
57
  end
@@ -6,9 +6,10 @@ module MetaModel
6
6
  require 'metamodel/command/build/parser'
7
7
  require 'metamodel/command/build/renderer'
8
8
 
9
- self.summary = ""
9
+ self.summary = "Build a MetaModel.framework from Metafile"
10
10
  self.description = <<-DESC
11
-
11
+ Clone a metamodel project template from GitHub, parsing Metafile, validating models,
12
+ generate model swift file and build MetaModel.framework.
12
13
  DESC
13
14
 
14
15
  attr_accessor :models
@@ -35,7 +36,7 @@ module MetaModel
35
36
  else
36
37
  UI.section "Cloning MetaModel project into `./metamodel` folder" do
37
38
  Git.clone(config.metamodel_template_uri, 'metamodel', :depth => 1)
38
- UI.message "Using `./metamodel/MetaModel.xcodeproj` to build module"
39
+ UI.message "Using `#{metamodel_xcode_project}` to build module"
39
40
  end
40
41
  end
41
42
  end
@@ -49,12 +50,14 @@ module MetaModel
49
50
  existing_types = @models.map { |m| m.properties.map { |property| property.type } }.flatten.uniq
50
51
  unsupported_types = existing_types - supported_types
51
52
  raise Informative, "Unsupported types #{unsupported_types}" unless unsupported_types == []
52
- # class_types = supported_types - built_in_types
53
- # @models.each do |model|
54
- # model.properties do |property|
55
- #
56
- # end
57
- # end
53
+
54
+ @models.each do |main|
55
+ main.relation_properties.each do |property|
56
+ @models.each do |secondary|
57
+ property.relation_model = secondary if property.type == secondary.name
58
+ end
59
+ end
60
+ end
58
61
  end
59
62
 
60
63
  def render_model_files
@@ -110,7 +113,7 @@ module MetaModel
110
113
 
111
114
  def validate!
112
115
  super
113
- raise Informative, 'No meta folder in directory' unless config.meta_path_in_dir(Pathname.pwd)
116
+ raise Informative, 'No Metafile in current directory' unless config.metafile_in_dir(Pathname.pwd)
114
117
  end
115
118
 
116
119
  private
@@ -125,14 +128,22 @@ module MetaModel
125
128
  end
126
129
  end
127
130
 
131
+ CURRENT_SUPPORTED_BUILT_IN_TYPES = %w[
132
+ Int
133
+ Double
134
+ String
135
+ Bool
136
+ NSDate
137
+ ]
138
+
128
139
  def built_in_types
129
- %w[Int Double String].map do |t|
140
+ CURRENT_SUPPORTED_BUILT_IN_TYPES.map do |t|
130
141
  [t, "#{t}?"]
131
142
  end.flatten
132
143
  end
133
144
 
134
145
  def supported_types
135
- @models.reduce(%w[Int Double String]) { |types, model|
146
+ @models.reduce(CURRENT_SUPPORTED_BUILT_IN_TYPES) { |types, model|
136
147
  types << model.name.to_s
137
148
  }.map { |type|
138
149
  [type, "#{type}?"]
@@ -17,6 +17,7 @@ module MetaModel
17
17
  def run
18
18
  UI.section "Removing MetaModel project" do
19
19
  FileUtils.rm_rf 'MetaModel'
20
+ FileUtils.rm_rf 'MetaModel.framework'
20
21
  UI.message "Already clean up the whole MetaModel project from current folder"
21
22
  end
22
23
  end
@@ -10,34 +10,34 @@ module MetaModel
10
10
 
11
11
  def initialize(argv)
12
12
  @model_name = argv.shift_argument
13
- @file_path = config.meta_path + "#{@model_name.underscore}.rb"
13
+ @metafile_path = config.metafile_path
14
14
  super
15
15
  end
16
16
 
17
17
  def run
18
18
  verify_meta_exists!
19
- UI.section "Generating model meta file" do
19
+ UI.section "Generating model scaffold" do
20
20
  title_options = { :verbose_prefix => '-> '.green }
21
- UI.titled_section "Adding `#{File.basename(@file_path)}` to meta folder", title_options do
22
- @file_path.open('w') { |f| f << model_template(@model_name) }
21
+ UI.titled_section "Adding `#{@model_name.camelize} model to Metafile", title_options do
22
+ @metafile_path.open('a') do |source|
23
+ source.puts model_template(@model_name)
24
+ end
23
25
  end
24
- UI.notice "`#{File.basename(@file_path)}` has already generated, use the command below to edit it.\n"
25
- UI.message "vim meta/#{File.basename(@file_path)}"
26
+ UI.notice "Adding `#{@model_name.camelize}` model scaffold to Metafile, use the command below to edit it.\n"
27
+ UI.message "vim Metafile"
26
28
  end
27
29
  end
28
30
 
29
31
  private
30
32
 
31
33
  def model_template(model)
32
- modelfile = ''
33
- modelfile << "metamodel_version '#{VERSION}'\n\n"
34
- modelfile << <<-TEMPLATE.strip_heredoc
34
+ <<-TEMPLATE.strip_heredoc
35
+
35
36
  define :#{model} do
36
37
  # define #{model} model like this
37
38
  # attr nickname, :string
38
39
  end
39
40
  TEMPLATE
40
- modelfile
41
41
  end
42
42
  end
43
43
  end
@@ -9,20 +9,23 @@ module MetaModel
9
9
  DESC
10
10
 
11
11
  def initialize(argv)
12
- @meta_path = Pathname.pwd + 'meta'
12
+ @metafile_path = Pathname.pwd + 'Metafile'
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 meta folder in directory' unless config.meta_path_in_dir(Pathname.pwd).nil?
19
+ raise Informative, 'Existing Metafile in directory' unless config.metafile_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 `meta` folder for MetaModel" do
25
- FileUtils.mkdir(@meta_path)
24
+ UI.section "Creating `Metafile` for MetaModel" do
25
+ FileUtils.touch(@metafile_path)
26
+ @metafile_path.open('w') do |source|
27
+ source.puts "metamodel_version '#{VERSION}'\n\n"
28
+ end
26
29
  end
27
30
  end
28
31
  end
@@ -16,8 +16,19 @@ module MetaModel
16
16
  self.description = 'MetaModel, the Model generator.'
17
17
  self.plugin_prefixes = %w(claide meta)
18
18
 
19
+ METAMODEL_COMMAND_ALIAS = {
20
+ "g" => "generate",
21
+ "i" => "init",
22
+ "b" => "build",
23
+ "c" => "clean"
24
+ }
25
+
19
26
  def self.run(argv)
20
- super(argv)
27
+ if METAMODEL_COMMAND_ALIAS[argv.first]
28
+ super([METAMODEL_COMMAND_ALIAS[argv.first]] + argv[1..-1])
29
+ else
30
+ super(argv)
31
+ end
21
32
  end
22
33
 
23
34
  def initialize(argv)
@@ -34,7 +45,7 @@ module MetaModel
34
45
  #
35
46
  # @return [void]
36
47
  def verify_meta_exists!
37
- unless config.meta_folder
48
+ unless config.metefile_exist?
38
49
  raise Informative, "No `meta' folder found in the project directory."
39
50
  end
40
51
  end
@@ -46,7 +46,7 @@ module MetaModel
46
46
  current_path = Pathname.new(current_dir)
47
47
  unless @installation_root
48
48
  until current_path.root?
49
- if meta_path_in_dir(current_path)
49
+ if metafile_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 meta folder is in current project.
80
+ # Returns whether or not metafile is in current project.
81
81
  #
82
82
  # @return [Bool]
83
83
  #
84
- def meta_folder
85
- Pathname.new(meta_path).exist?
84
+ def metefile_exist?
85
+ Pathname.new(metafile_path).exist?
86
86
  end
87
87
 
88
- # Returns the path of the meta.
88
+ # Returns the path of the Metafile.
89
89
  #
90
90
  # @return [Pathname]
91
91
  # @return [Nil]
92
92
  #
93
- def meta_path
94
- @meta_path_in_dir ||= installation_root + 'meta'
93
+ def metafile_path
94
+ @metafile_in_dir ||= installation_root + 'Metafile'
95
95
  end
96
96
 
97
- # Returns the path of the meta folder in the given dir if any exists.
97
+ # Returns the path of the Metafile in the given dir if any exists.
98
98
  #
99
99
  # @param [Pathname] dir
100
100
  # The directory where to look for the meta.
101
101
  #
102
- # @return [Pathname] The path of the meta.
102
+ # @return [Pathname] The path of the metafile.
103
103
  # @return [Nil] If not meta was found in the given dir
104
104
  #
105
- def meta_path_in_dir(dir)
106
- candidate = dir + 'meta'
105
+ def metafile_in_dir(dir)
106
+ candidate = dir + 'Metafile'
107
107
  if candidate.exist?
108
108
  return candidate
109
109
  end
@@ -6,7 +6,7 @@ module MetaModel
6
6
  attr_reader :relation_properties
7
7
 
8
8
  def initialize(name)
9
- @name = name
9
+ @name = name.to_s
10
10
  @properties = []
11
11
  @relation_properties = []
12
12
 
@@ -14,7 +14,7 @@ module MetaModel
14
14
  end
15
15
 
16
16
  def properties_exclude_id
17
- @properties.select { |property| property.name != :id }
17
+ @properties.select { |property| property.name != "id" }
18
18
  end
19
19
 
20
20
  def foreign_id
@@ -22,7 +22,7 @@ module MetaModel
22
22
  end
23
23
 
24
24
  def table_name
25
- name.to_s.tableize
25
+ name.tableize
26
26
  end
27
27
 
28
28
  def relation_name
@@ -38,7 +38,7 @@ module MetaModel
38
38
  def validate
39
39
  property_keys = @properties.map { |property| property.name }
40
40
 
41
- unless property_keys.include? :id
41
+ unless property_keys.include? "id"
42
42
  property_id = Property.new(:id, :int, :unique, :default => 0)
43
43
  @properties << property_id
44
44
  end
@@ -48,38 +48,28 @@ module MetaModel
48
48
  self.hash.to_s(16)
49
49
  end
50
50
 
51
- def property_key_value_pairs
52
- @properties.map { |property| "#{property.name.to_s}: #{property.name.to_s}" }.join(", ")
51
+ def property_key_value_pairs(cast = false)
52
+ key_value_pairs_with_property @properties, cast
53
53
  end
54
54
 
55
- def property_key_type_pairs
56
- key_type_pairs_with_property(@properties, false)
55
+ def property_key_value_pairs_without_property(property)
56
+ key_value_pairs_with_property @properties.select { |element| element.name != property }
57
57
  end
58
58
 
59
- def property_exclude_id_key_value_pairs(prefix = true, cast = false)
60
- result = ""
61
- if cast
62
- result = properties_exclude_id.map { |property| "#{property.name.to_s}: #{property.type_without_optional == "Int" ? "Int(#{property.name.to_s})" : property.name.to_s}" }.join(", ")
63
- else
64
- result = properties_exclude_id.map { |property| "#{property.name.to_s}: #{property.name.to_s}" }.join(", ")
65
- end
66
- return result unless prefix
67
- return result.length > 0 ? ", #{result}" : ""
59
+ def property_exclude_id_key_value_pairs(cast = false)
60
+ key_value_pairs_with_property properties_exclude_id, cast
68
61
  end
69
62
 
70
- def property_exclude_id_key_type_pairs(prefix = true)
71
- key_type_pairs_with_property(properties_exclude_id, prefix)
63
+ def property_key_type_pairs
64
+ key_type_pairs_with_property @properties
72
65
  end
73
66
 
74
- def key_type_pairs_with_property(properties, prefix = true)
75
- result = properties.map { |property|
76
- has_default_value = property.has_default_value?
77
- default_value = property.type_without_optional == "String" ?
78
- "\"#{property.default_value}\"" : property.default_value
79
- "#{property.name.to_s}: #{property.type.to_s}#{if has_default_value then " = " + "#{default_value}" end}"
80
- }.join(", ")
81
- return result unless prefix
82
- return result.length > 0 ? ", #{result}" : ""
67
+ def property_key_type_pairs_without_property(property)
68
+ key_type_pairs_with_property @properties.select { |element| element.name != property }
69
+ end
70
+
71
+ def property_exclude_id_key_type_pairs
72
+ key_type_pairs_with_property properties_exclude_id
83
73
  end
84
74
 
85
75
  def build_table
@@ -98,7 +88,29 @@ module MetaModel
98
88
  end
99
89
 
100
90
  table + "(_id INTEGER PRIMARY KEY, #{(main_sql + foreign_sql).compact.join(", ")});"
91
+
101
92
  end
93
+
94
+ private
95
+
96
+ def key_value_pairs_with_property(properties, cast = false)
97
+ properties.map do |property|
98
+ if cast
99
+ "#{property.name}: #{property.type_without_optional}(#{property.name})"
100
+ else
101
+ "#{property.name}: #{property.name}"
102
+ end
103
+ end.join(", ")
104
+ end
105
+
106
+ def key_type_pairs_with_property(properties)
107
+ properties.map do |property|
108
+ has_default_value = property.has_default_value?
109
+ default_value = property.type_without_optional == "String" ? "\"#{property.default_value}\"" : property.default_value
110
+ "#{property.name} #{property.name}: #{property.type.to_s}#{if has_default_value then " = " + "#{default_value}" end}"
111
+ end.join(", ")
112
+ end
113
+
102
114
  end
103
115
 
104
116
  end
@@ -1,14 +1,15 @@
1
1
  module MetaModel
2
2
 
3
3
  class Property
4
- attr_reader :json_key
5
4
  attr_accessor :name
6
- attr_reader :type
7
- attr_reader :modifiers
5
+ attr_accessor :relation_model
6
+ attr_reader :json_key
7
+ attr_reader :type
8
+ attr_reader :modifiers
8
9
 
9
10
  def initialize(json_key, type = :string, *modifiers)
10
11
  @json_key = json_key
11
- @name = json_key.to_s.camelize(:lower).to_sym
12
+ @name = json_key.to_s.camelize(:lower)
12
13
  @type = convert_symbol_to_type type
13
14
 
14
15
  @modifiers = {}
@@ -34,20 +35,39 @@ module MetaModel
34
35
  end
35
36
 
36
37
  def database_type
37
- lowercase_type = self.type.downcase
38
- if lowercase_type == "string"
39
- return "TEXT"
40
- elsif lowercase_type == "int"
41
- return "INTEGER"
42
- elsif lowercase_type == "double"
43
- return "REAL"
38
+ case type_without_optional
39
+ when "String" then "TEXT"
40
+ when "Int" then "INTEGER"
41
+ when "Bool" then "INTEGER"
42
+ when "Double" then "REAL"
43
+ when "NSDate" then "REAL"
44
+ else raise Informative, "Unsupported type #{self.type}"
44
45
  end
45
46
  end
46
47
 
47
48
  def real_type
48
- type_without_optional == "Int" ? "Int64" : type_without_optional
49
+ case type_without_optional
50
+ when "String" then "String"
51
+ when "Int" then "Int64"
52
+ when "Bool" then "Int64"
53
+ when "Double" then "Double"
54
+ when "NSDate" then "Double"
55
+ else raise Informative, "Unsupported type #{self.type}"
56
+ end
57
+ end
58
+
59
+ def convert_symbol_to_type(symbol)
60
+ case symbol
61
+ when :int then "Int"
62
+ when :double then "Double"
63
+ when :bool then "Bool"
64
+ when :string then "String"
65
+ when :date then "NSDate"
66
+ else symbol.to_s.camelize
67
+ end
49
68
  end
50
69
 
70
+
51
71
  def is_array?
52
72
  @type.pluralize == str
53
73
  end
@@ -89,11 +109,6 @@ module MetaModel
89
109
  end
90
110
 
91
111
  private
92
-
93
- def convert_symbol_to_type(symbol)
94
- symbol.to_s.capitalize
95
- end
96
-
97
112
  end
98
113
 
99
114
  end
@@ -5,12 +5,12 @@
5
5
  element.update(#{model.foreign_id}: id)
6
6
  }
7
7
 
8
- func create#{property.type}(id: Int, content: String) -> #{property.type}? {
9
- return #{property.type}.create(id, content: content, #{model.foreign_id}: id)
8
+ func create#{property.type}(#{property.relation_model.property_key_type_pairs_without_property model.foreign_id}) -> #{property.type}? {
9
+ return #{property.type}.create(#{property.relation_model.property_key_value_pairs_without_property model.foreign_id}, #{model.foreign_id}: self.id)
10
10
  }
11
11
 
12
12
  func delete#{property.type}(id: Int) {
13
- #{property.type}.filter(.#{model.foreign_id}, value: id).findBy(id: id).first?.delete()
13
+ #{property.type}.filter(.#{model.foreign_id}, value: id).findBy(id: id).first?.delete
14
14
  }
15
15
  var #{property.name}: [#{property.type}] {
16
16
  get {
@@ -46,7 +46,7 @@
46
46
  return #{property.type}.find(id)
47
47
  }
48
48
  set {
49
- #{property.type}.filter(.#{model.name.to_s.camelize(:lower)}Id, value: id).deleteAll
49
+ #{property.type}.filter(.#{model.name.camelize(:lower)}Id, value: id).deleteAll
50
50
  guard var newValue = newValue else { return }
51
51
  newValue.update(articleId: id)
52
52
  }
@@ -39,7 +39,7 @@ public extension <%= model.name %> {
39
39
  if let _ = <%= model.name %>.find(id) {
40
40
  update([<% column_values = model.properties.map do |property| %><% ".#{property.name}: #{property.name}" %><% end %><%= column_values.join(", ") %>])
41
41
  } else {
42
- <%= model.name %>.create(id<%= model.property_exclude_id_key_value_pairs %>)
42
+ <%= model.name %>.create(<%= model.property_key_value_pairs %>)
43
43
  }
44
44
  return self
45
45
  }
@@ -17,8 +17,6 @@ let db = try! Connection("\(path)/metamodel_db.sqlite3")
17
17
  public class MetaModel {
18
18
  public static func initialize() {
19
19
  validateMetaModelTables()
20
- <% models.each do |model| %><%= "#{model.name}.initialize()" %>
21
- <% end %>
22
20
  }
23
21
  static func validateMetaModelTables() {
24
22
  createMetaModelTable()
@@ -26,12 +24,13 @@ public class MetaModel {
26
24
  <% models.each do |model| %><%= """if infos[#{model.name}.tableName] != \"#{model.hash_value}\" {
27
25
  updateMetaModelTableInfos(#{model.name}.tableName, hashValue: \"#{model.hash_value}\")
28
26
  #{model.name}.deinitialize()
27
+ #{model.name}.initialize()
29
28
  }""" %>
30
29
  <% end %>
31
30
  }
32
31
  }
33
32
 
34
- func executeSQL(sql: String, verbose: Bool = true, success: (() -> ())? = nil) -> Statement? {
33
+ func executeSQL(sql: String, verbose: Bool = false, suppress: Bool = false, success: (() -> ())? = nil) -> Statement? {
35
34
  if verbose {
36
35
  print("-> Begin Transaction")
37
36
  }
@@ -54,9 +53,9 @@ func executeSQL(sql: String, verbose: Bool = true, success: (() -> ())? = nil) -
54
53
  } catch let error {
55
54
  let endDate = NSDate()
56
55
  let interval = endDate.timeIntervalSinceDate(startDate) * 1000
56
+ print("\tSQL (\(interval.format("0.2"))ms) \(sql)")
57
+ print("\t\(error)")
57
58
  if verbose {
58
- print("\tSQL (\(interval.format("0.2"))ms) \(sql)")
59
- print("\t\(error)")
60
59
  print("-> Rollback transaction")
61
60
  print("\n")
62
61
  }
@@ -64,7 +63,7 @@ func executeSQL(sql: String, verbose: Bool = true, success: (() -> ())? = nil) -
64
63
  return nil
65
64
  }
66
65
 
67
- func executeScalarSQL(sql: String, verbose: Bool = false, success: (() -> ())? = nil) -> Binding? {
66
+ func executeScalarSQL(sql: String, verbose: Bool = false, suppress: Bool = false, success: (() -> ())? = nil) -> Binding? {
68
67
  if verbose {
69
68
  print("-> Begin Transaction")
70
69
  }
@@ -2,6 +2,6 @@ extension <%= model.name %>: Recordable {
2
2
  public init(values: Array<Optional<Binding>>) {
3
3
  <% model.properties.each_with_index do |property, index| %><%= """let #{property.name}: #{property.real_type} = values[#{index+1}] as! #{property.real_type}""" %>
4
4
  <% end %>
5
- self.init(id: Int(id)<%= model.property_exclude_id_key_value_pairs(true, true) %>)
5
+ self.init(<%= model.property_key_value_pairs true %>)
6
6
  }
7
7
  }
@@ -3,10 +3,13 @@ public extension <%= model.name %> {
3
3
  let deleteAllSQL = "DELETE FROM \(tableName.unwrapped)"
4
4
  executeSQL(deleteAllSQL)
5
5
  }
6
- static func count() -> Int {
7
- let countSQL = "SELECT count(*) FROM \(tableName.unwrapped)"
8
- guard let count = executeScalarSQL(countSQL) as? Int64 else { return 0 }
9
- return Int(count)
6
+
7
+ static var count: Int {
8
+ get {
9
+ let countSQL = "SELECT count(*) FROM \(tableName.unwrapped)"
10
+ guard let count = executeScalarSQL(countSQL) as? Int64 else { return 0 }
11
+ return Int(count)
12
+ }
10
13
  }
11
14
 
12
15
  static func new(<%= model.property_key_type_pairs %>) -> <%= model.name %> {
@@ -14,7 +17,7 @@ public extension <%= model.name %> {
14
17
  }
15
18
 
16
19
  static func create(<%= model.property_key_type_pairs %>) -> <%= model.name %>? {
17
- if <%= model.properties.select { |p| p.name.to_s.downcase.end_with? "id" }.map { |p| "#{p.name} == 0" }.join(" || ") %> { return nil }
20
+ if <%= model.properties.select { |p| p.name.downcase.end_with? "id" }.map { |p| "#{p.name} == 0" }.join(" || ") %> { return nil }
18
21
 
19
22
  var columnsSQL: [<%= model.name %>.Column] = []
20
23
  var valuesSQL: [Unwrapped] = []
@@ -1,5 +1,5 @@
1
1
  module MetaModel
2
2
  # The version of the MetaModel command line tool.
3
3
  #
4
- VERSION = '0.1.1'.freeze unless defined? MetaModel::MetaModel
4
+ VERSION = '0.1.5' unless defined? MetaModel::VERSION
5
5
  end
data/lib/metamodel.rb CHANGED
@@ -13,7 +13,8 @@ module MetaModel
13
13
  end
14
14
 
15
15
  require 'pathname'
16
- require 'active_record'
16
+ require 'active_support/inflector'
17
+ require 'active_support/core_ext/string'
17
18
 
18
19
  require 'metamodel/version'
19
20
  require 'metamodel/config'
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.1
4
+ version: 0.1.5
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-08 00:00:00.000000000 Z
11
+ date: 2016-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: claide
@@ -59,17 +59,23 @@ dependencies:
59
59
  - !ruby/object:Gem::Version
60
60
  version: '1.2'
61
61
  - !ruby/object:Gem::Dependency
62
- name: activerecord
62
+ name: activesupport
63
63
  requirement: !ruby/object:Gem::Requirement
64
64
  requirements:
65
- - - "~>"
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: 4.2.6
68
+ - - "<"
66
69
  - !ruby/object:Gem::Version
67
70
  version: '5.0'
68
71
  type: :runtime
69
72
  prerelease: false
70
73
  version_requirements: !ruby/object:Gem::Requirement
71
74
  requirements:
72
- - - "~>"
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 4.2.6
78
+ - - "<"
73
79
  - !ruby/object:Gem::Version
74
80
  version: '5.0'
75
81
  - !ruby/object:Gem::Dependency
@@ -183,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
189
  version: '0'
184
190
  requirements: []
185
191
  rubyforge_project:
186
- rubygems_version: 2.6.4
192
+ rubygems_version: 2.5.1
187
193
  signing_key:
188
194
  specification_version: 4
189
195
  summary: The Cocoa models generator.