diff --git a/FMDB.podspec b/FMDB.podspec deleted file mode 100644 index 9cfe71c..0000000 --- a/FMDB.podspec +++ /dev/null @@ -1,12 +0,0 @@ -Pod::Spec.new do |s| - s.name = 'FMDB' - s.version = '2.0' - s.summary = 'A Cocoa / Objective-C wrapper around SQLite.' - s.homepage = 'https://github.com/ccgus/fmdb' - s.license = 'MIT' - s.author = { 'August Mueller' => 'gus@flyingmeat.com' } - s.source = { :git => 'https://github.com/aceofspades/fmdb.git' } - - s.source_files = FileList['src/FM*.{h,m}'].exclude(/fmdb\.m/) - s.library = 'sqlite3' -end diff --git a/Gemfile b/Gemfile index 6f01543..39a90b8 100644 --- a/Gemfile +++ b/Gemfile @@ -1,12 +1,8 @@ -source "https://rubygems.org" +source 'https://rubygems.org' -gem "rake" -gem "bubble-wrap" -gem "motion-stump", '~>0.2' +gem 'rake' +gem 'motion-support' +gem 'motion-cocoapods' +gem 'bubble-wrap' +gem 'motion-stump' -gem "motion-support", ">= 0.2.2" - -gem 'cocoapods', '~>0.19.1' -gem 'motion-cocoapods', '~>1.3.0' - -gemspec diff --git a/Gemfile.lock b/Gemfile.lock index e84a9b6..c4b528f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,75 +1,56 @@ -PATH - remote: . - specs: - motion_model (0.4.6) - bubble-wrap (= 1.3.0) - motion-support (>= 0.2.2) - GEM remote: https://rubygems.org/ specs: - activesupport (3.2.13) - i18n (= 0.6.1) + activesupport (3.2.16) + i18n (~> 0.6, >= 0.6.4) multi_json (~> 1.0) - addressable (2.3.4) - bubble-wrap (1.3.0) - claide (0.2.0) - cocoapods (0.19.1) - activesupport (~> 3.2.13) - claide (~> 0.2.0) - cocoapods-core (= 0.19.1) - cocoapods-downloader (~> 0.1.0) + bubble-wrap (1.4.0) + claide (0.4.0) + cocoapods (0.29.0) + activesupport (>= 3.2.15, < 4) + claide (~> 0.4.0) + cocoapods-core (= 0.29.0) + cocoapods-downloader (~> 0.3.0) + cocoapods-try-release-fix (~> 0.1.1) colored (~> 1.2) escape (~> 0.0.4) - faraday (~> 0.8.1) - json (~> 1.7.3) - octokit (~> 1.7) - open4 (~> 1.3.0) - rake (~> 10.0.0) - xcodeproj (~> 0.5.5) - cocoapods-core (0.19.1) - activesupport (~> 3.2.13) - rake (~> 10.0.0) - cocoapods-downloader (0.1.0) + json_pure (~> 1.8) + nap (~> 0.5) + open4 (~> 1.3) + xcodeproj (~> 0.14.1) + cocoapods-core (0.29.0) + activesupport (>= 3.2.15, < 4) + fuzzy_match (~> 2.0.4) + json_pure (~> 1.8) + nap (~> 0.5) + cocoapods-downloader (0.3.0) + cocoapods-try-release-fix (0.1.1) colored (1.2) escape (0.0.4) - faraday (0.8.7) - multipart-post (~> 1.1) - faraday_middleware (0.9.0) - faraday (>= 0.7.4, < 0.9) - hashie (2.0.5) - i18n (0.6.1) - json (1.7.7) - motion-cocoapods (1.3.1) - cocoapods (>= 0.17.0) - motion-require (0.0.6) - motion-stump (0.2.1) - motion-support (0.2.2) + fuzzy_match (2.0.4) + i18n (0.6.9) + json_pure (1.8.1) + motion-cocoapods (1.4.0) + cocoapods (>= 0.26.2) + motion-require (0.1.0) + motion-stump (0.3.1) + motion-support (0.2.6) motion-require (>= 0.0.6) - multi_json (1.7.3) - multipart-post (1.2.0) - netrc (0.7.7) - octokit (1.24.0) - addressable (~> 2.2) - faraday (~> 0.8) - faraday_middleware (~> 0.9) - hashie (~> 2.0) - multi_json (~> 1.3) - netrc (~> 0.7.7) + multi_json (1.8.4) + nap (0.6.0) open4 (1.3.0) - rake (10.0.4) - xcodeproj (0.5.5) - activesupport (~> 3.2.13) + rake (10.1.1) + xcodeproj (0.14.1) + activesupport (~> 3.0) colored (~> 1.2) + rake PLATFORMS ruby DEPENDENCIES bubble-wrap - cocoapods (~> 0.19.1) - motion-cocoapods (~> 1.3.0) - motion-stump (~> 0.2) - motion-support (>= 0.2.2) - motion_model! + motion-cocoapods + motion-stump + motion-support rake diff --git a/Rakefile b/Rakefile index df46d30..93e350d 100644 --- a/Rakefile +++ b/Rakefile @@ -1,19 +1,25 @@ # -*- coding: utf-8 -*- -require "bundler/gem_tasks" $:.unshift("/Library/RubyMotion/lib") require 'motion/project/template/ios' -require 'bundler' -Bundler.require(:default) + +begin + require 'bundler' + Bundler.require +rescue LoadError +end require 'motion-cocoapods' +$: << File.expand_path('/lib', __FILE__) + require 'motion_model' -require 'motion_model/array' -require 'motion_model/sql' -require 'motion_model/fmdb' Motion::Project::App.setup do |app| # Use `rake config' to see complete project settings. app.name = 'MotionModel' app.delegate_class = 'FakeDelegate' + + app.pods do + pod 'FMDB', '2.0' + end end diff --git a/lib/motion_model.rb b/lib/motion_model.rb index 6c0389d..c20726e 100644 --- a/lib/motion_model.rb +++ b/lib/motion_model.rb @@ -1,6 +1,3 @@ -require 'motion-require' -require 'motion-support' - -%w(*.rb model/*.rb adapters/*.rb).each do |path| +%w[*.rb model/*.rb adapters/*.rb adapters/array/*.rb adapters/sql/sql_db_adapter.rb adapters/sql/sqlite3_adapter.rb adapters/sql/*.rb].each do |path| Motion::Require.all(Dir.glob(File.expand_path("../../motion/#{path}", __FILE__))) -end +end \ No newline at end of file diff --git a/motion/adapters/sql/fmdb_adapter.rb b/motion/adapters/sql/fmdb_adapter.rb index bb7bfd2..e944c08 100644 --- a/motion/adapters/sql/fmdb_adapter.rb +++ b/motion/adapters/sql/fmdb_adapter.rb @@ -44,7 +44,7 @@ def execute_sql(sql) if rset result = [] while rset.next do - result << rset.resultDictionary + result << rset.resultDict end end else diff --git a/motion/adapters/sql/sql_condition.rb b/motion/adapters/sql/sql_condition.rb index 07c1dce..fed9ba5 100644 --- a/motion/adapters/sql/sql_condition.rb +++ b/motion/adapters/sql/sql_condition.rb @@ -49,6 +49,8 @@ def to_sql_str values = @value.map { |v| v.is_a?(Numeric) ? v : %Q["#{v}"] } elsif @value.is_a?(Range) values = [@value.min, @value.max] + elsif @value.is_a?(TrueClass) || @value.is_a?(FalseClass) + value = @value.to_i else value = %Q["#{@value}"] end diff --git a/motion/adapters/sql/sql_db_adapter.rb b/motion/adapters/sql/sql_db_adapter.rb index 932573d..abfbd9a 100644 --- a/motion/adapters/sql/sql_db_adapter.rb +++ b/motion/adapters/sql/sql_db_adapter.rb @@ -154,6 +154,15 @@ def to_delete_sql(scope) SQL end + def to_update_all_sql(scope, attrs) + typed_attrs = _quoted_db_typed_attributes(attrs) + column_values_str = typed_attrs.map { |k, v| %Q["#{k.to_s}" = #{v}] }.join(', ') + + <<-SQL.strip << ';' + UPDATE "#{scope.table_name}" SET #{column_values_str} #{scope.options_str} + SQL + end + private def _quoted_db_typed_attributes(attrs) diff --git a/motion/adapters/sql/sql_model_adapter.rb b/motion/adapters/sql/sql_model_adapter.rb index 49d9d67..8800b01 100644 --- a/motion/adapters/sql/sql_model_adapter.rb +++ b/motion/adapters/sql/sql_model_adapter.rb @@ -322,6 +322,15 @@ def get_loaded_attr(col) _col.type == :belongs_to? ? get_attr(name) : relation(_col).loaded end + def reload + _new_record = self.class.find(self.id) + if _new_record.present? + self.attributes = self.class.find(self.id).attributes + else + nil + end + end + private def before_initialize(options) diff --git a/motion/adapters/sql/sql_scope.rb b/motion/adapters/sql/sql_scope.rb index 6e04cae..ec7c37e 100644 --- a/motion/adapters/sql/sql_scope.rb +++ b/motion/adapters/sql/sql_scope.rb @@ -17,6 +17,7 @@ def initialize(model_class, db_adapter, type = :select) @orders = nil @group = nil @limit = nil + @offset = nil end def method_missing(id, *args) @@ -34,6 +35,7 @@ def deep_clone _orders = @orders.try(:dup) _group = @group.try(:dup) _limit = @limit + _offset = @offset _conditions = @conditions.try(:dup) _type = @type self.class.new(@model_class, @db_adapter).instance_eval do @@ -43,6 +45,7 @@ def deep_clone @orders = _orders @group = _group @limit = _limit + @offset = _offset @conditions = _conditions @type = _type self @@ -190,6 +193,14 @@ def limit(limit) end end + def offset(offset) + return self if null_scope? + deep_clone.instance_eval do + @offset = offset + self + end + end + def all deep_clone end @@ -235,6 +246,20 @@ def delete @model_class.do_delete(self) end + def delete_all_sql + @db_adapter.to_delete_sql(default_scope) + end + + def delete_all + _delete_all_sql = @db_adapter.to_delete_sql(self) + @db_adapter.build_sql_context(:delete, _delete_all_sql).execute + end + + def update_all(_attributes) + _update_all_sql = @db_adapter.to_update_all_sql(self, _attributes) + @db_adapter.build_sql_context(:update, _update_all_sql).execute + end + def to_sql @db_adapter.send("to_#{type.to_s}_sql", self) end @@ -286,8 +311,12 @@ def limit_str @limit ? %Q[LIMIT #{@limit}] : nil end + def offset_str + @offset ? %Q[OFFSET #{@offset}] : nil + end + def options_str - arr = [SQLCondition.to_sql_str(@conditions), group_str, order_str, limit_str].compact + arr = [SQLCondition.to_sql_str(@conditions), group_str, order_str, limit_str, offset_str].compact arr.empty? ? nil : arr.join(' ') end diff --git a/motion/model/model.rb b/motion/model/model.rb index 6442418..97bd65e 100644 --- a/motion/model/model.rb +++ b/motion/model/model.rb @@ -533,9 +533,12 @@ def save_without_transaction(options = {}) end # Set created_at and updated_at fields + def set_auto_date_field(field_name) method = "#{field_name}=" - self.send(method, Time.now) if self.respond_to?(method) + if self.respond_to?(method) + self.send(method, Time.now) if self.send(field_name).blank? + end end def hooks(name) @@ -817,7 +820,8 @@ def unload_relation(col) end def initialize_data_columns(column, value) #nodoc - self.attributes = {column => value || self.class.default(column)} + _value = value.nil? ? self.class.default(column) : value + self.attributes = {column => _value} end def column_as(col) #nodoc diff --git a/motion_model.gemspec b/motion_model.gemspec index 28c244f..124c7db 100644 --- a/motion_model.gemspec +++ b/motion_model.gemspec @@ -12,7 +12,7 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.name = "motion_model" gem.require_paths = ["lib"] - gem.add_dependency 'bubble-wrap', '1.3.0' + gem.add_dependency 'bubble-wrap', '>= 1.3.0' gem.add_dependency 'motion-support', '>= 0.2.2' gem.version = MotionModel::VERSION end