Skip to content

Commit

Permalink
Changed Model#method_missing to short-circuit and bypass checking #co…
Browse files Browse the repository at this point in the history
…lumns if the values hash already contains the relevant column (#150).
  • Loading branch information
noteflakes committed Feb 9, 2008
1 parent a8a83d1 commit 6a6a55f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
2 changes: 2 additions & 0 deletions sequel_model/CHANGELOG
@@ -1,5 +1,7 @@
=== SVN

* Changed Model#method_missing to short-circuit and bypass checking #columns if the values hash already contains the relevant column.

* Updated to reflect changes in sequel_core (Dataset#clone_merge renamed to Dataset#clone).

=== 0.4 (2008-02-05)
Expand Down
11 changes: 3 additions & 8 deletions sequel_model/lib/sequel_model/record.rb
Expand Up @@ -299,20 +299,15 @@ def delete
end

ATTR_RE = /^([a-zA-Z_]\w*)(=)?$/.freeze
EQUAL_SIGN = '='.freeze

def method_missing(m, *args) #:nodoc:
if m.to_s =~ ATTR_RE
att = $1.to_sym
write = $2 == '='
write = $2 == EQUAL_SIGN

# check whether the column is legal
unless columns.include?(att)
# if read accessor and a value exists for the column, we return it
if !write && @values.has_key?(att)
return @values[att]
end

# otherwise, raise an error
unless @values.has_key?(att) || columns.include?(att)
raise Error, "Invalid column (#{att.inspect}) for #{self}"
end

Expand Down
10 changes: 4 additions & 6 deletions sequel_model/spec/model_spec.rb
Expand Up @@ -439,10 +439,9 @@ class DummyModelBased < Sequel::Model(:blog)
MODEL_DB.reset

@c = Class.new(Sequel::Model(:items)) do
def columns
[:id, :x, :y]
end
end

@c.dataset.meta_def(:columns) {[:id, :x, :y]}
end

it "should be created dynamically" do
Expand All @@ -469,7 +468,7 @@ def columns

proc {o.yy?}.should raise_error(NoMethodError)
end

it "should not raise for a column not in the dataset, but for which there's a value" do
o = @c.new

Expand All @@ -484,9 +483,8 @@ def columns
o.xx.should == 123
o.yy.should == nil

proc {o.xx = 3}.should raise_error(Sequel::Error)
proc {o.xx = 3}.should_not raise_error(Sequel::Error)
end

end

describe Sequel::Model, ".[]" do
Expand Down

0 comments on commit 6a6a55f

Please sign in to comment.