lib/sequel/model.rb in sequel-0.3.1 vs lib/sequel/model.rb in sequel-0.3.2

- old
+ new

@@ -77,16 +77,25 @@ # # New records can be created by calling Model.create: # # post = Post.create(:title => 'hello world') # - # You can also create a new instance and save it: + # Another way is to construct a new instance and save it: # # post = Post.new # post.title = 'hello world' # post.save # + # You can also supply a block to Model.new and Model.create: + # + # post = Post.create {|p| p.title = 'hello world'} + # + # post = Post.new do |p| + # p.title = 'hello world' + # p.save + # end + # # === Hooks # # You can execute custom code when creating, updating, or deleting records by using hooks. The before_create and after_create hooks wrap record creation. The before_update and after_update wrap record updating. The before_save and after_save wrap record creation and updating. The before_destroy and after_destroy wrap destruction. # # Hooks are defined by supplying a block: @@ -125,15 +134,18 @@ # # class Author < Sequel::Model(:authors) # def posts; Post.filter(:author_id => pk); end # end # - # Sequel also provides two macros to assist with common types of associations. The one_to_one macro is roughly equivalent to ActiveRecord's belongs_to macro: + # Sequel also provides two macros to assist with common types of associations. The one_to_one macro is roughly equivalent to ActiveRecord?'s belongs_to macro. It defines both getter and setter methods for the association: # # class Post < Sequel::Model(:posts) # one_to_one :author, :from => Author # end + # + # post = Post.create(:name => 'hi!') + # post.author = Author[:name => 'Sharon'] # # The one_to_many macro is roughly equivalent to ActiveRecord's has_many macro: # # class Author < Sequel::Model(:authors) # one_to_many :posts, :from => Post, :key => :author_id @@ -268,28 +280,22 @@ end # Deletes all records. def self.delete_all dataset.delete end - - FIND_BY_REGEXP = /^find_by_(.*)/.freeze - FILTER_BY_REGEXP = /^filter_by_(.*)/.freeze - ALL_BY_REGEXP = /^all_by_(.*)/.freeze - + + def self.is_dataset_magic_method?(m) + method_name = m.to_s + Sequel::Dataset::MAGIC_METHODS.each_key do |r| + return true if method_name =~ r + end + false + end + def self.method_missing(m, *args, &block) #:nodoc: Thread.exclusive do - method_name = m.to_s - if method_name =~ FIND_BY_REGEXP - c = $1.to_sym - meta_def(method_name) {|arg| find(c => arg)} - elsif method_name =~ FILTER_BY_REGEXP - c = $1.to_sym - meta_def(method_name) {|arg| filter(c => arg)} - elsif method_name =~ ALL_BY_REGEXP - c = $1.to_sym - meta_def(method_name) {|arg| filter(c => arg).all} - elsif dataset.respond_to?(m) + if dataset.respond_to?(m) || is_dataset_magic_method?(m) instance_eval("def #{m}(*args, &block); dataset.#{m}(*args, &block); end") end end respond_to?(m) ? send(m, *args, &block) : super(m, *args) end @@ -299,15 +305,15 @@ table_name = dataset.opts[:from].first dataset.join(*args).select(table_name.to_sym.ALL) end # Returns value of attribute. - def [](field) - @values[field] + def [](column) + @values[column] end # Sets value of attribute. - def []=(field, value) - @values[field] = value + def []=(column, value) + @values[column] = value end # Enumerates through all attributes. # # === Example: