Skip to content

Commit

Permalink
Merge branch 'master' of git@github.com:sam/dm-core
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Lerche committed Jul 23, 2008
2 parents b43d419 + 6efdcbe commit 2601ae4
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 25 deletions.
1 change: 1 addition & 0 deletions Manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ spec/lib/mock_adapter.rb
spec/lib/model_loader.rb
spec/lib/publicize_methods.rb
spec/models/vehicles.rb
spec/models/zoo.rb
spec/spec.opts
spec/spec_helper.rb
spec/unit/adapters/abstract_adapter_spec.rb
Expand Down
4 changes: 2 additions & 2 deletions lib/dm-core/adapters/data_objects_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,8 @@ def type_map
tm.map(String).to('VARCHAR').with(:size => Property::DEFAULT_LENGTH)
tm.map(Class).to('VARCHAR').with(:size => Property::DEFAULT_LENGTH)
tm.map(DM::Discriminator).to('VARCHAR').with(:size => Property::DEFAULT_LENGTH)
tm.map(BigDecimal).to('DECIMAL').with(:precision => Property::DEFAULT_PRECISION, :scale => Property::DEFAULT_SCALE)
tm.map(Float).to('FLOAT').with(:precision => Property::DEFAULT_PRECISION, :scale => Property::DEFAULT_SCALE)
tm.map(BigDecimal).to('DECIMAL').with(:precision => Property::DEFAULT_PRECISION, :scale => Property::DEFAULT_SCALE_BIGDECIMAL)
tm.map(Float).to('FLOAT').with(:precision => Property::DEFAULT_PRECISION)
tm.map(DateTime).to('DATETIME')
tm.map(Date).to('DATE')
tm.map(Time).to('TIMESTAMP')
Expand Down
1 change: 1 addition & 0 deletions lib/dm-core/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def inherited(target)
target.instance_variable_set(:@properties, Hash.new { |h,k| h[k] = k == Repository.default_name ? PropertySet.new : h[Repository.default_name].dup })
target.instance_variable_set(:@base_model, self.base_model)
target.instance_variable_set(:@paranoid_properties, @paranoid_properties)
target.instance_variable_set(:@validations, @validations) if self.respond_to?(:validators)

@properties.each do |repository_name,properties|
repository(repository_name) do
Expand Down
48 changes: 30 additions & 18 deletions lib/dm-core/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ class Property

DEFAULT_LENGTH = 50
DEFAULT_PRECISION = 10
DEFAULT_SCALE = 0
DEFAULT_SCALE_BIGDECIMAL = 0
DEFAULT_SCALE_FLOAT = nil

attr_reader :primitive, :model, :name, :instance_variable_name,
:type, :reader_visibility, :writer_visibility, :getter, :options,
Expand Down Expand Up @@ -373,17 +374,7 @@ def custom?
def get(resource)
new_record = resource.new_record?

unless new_record || resource.attribute_loaded?(name)
# TODO: refactor this section
contexts = if lazy?
name
else
model.properties(resource.repository.name).reject do |property|
property.lazy? || resource.attribute_loaded?(property.name)
end
end
resource.send(:lazy_load, contexts)
end
lazy_load(resource) unless new_record || resource.attribute_loaded?(name)

value = get!(resource)

Expand Down Expand Up @@ -412,6 +403,7 @@ def get!(resource)
#-
# @api private
def set(resource, value)
lazy_load(resource) unless resource.new_record? || resource.attribute_loaded?(name)
new_value = typecast(value)
old_value = get!(resource)

Expand All @@ -428,6 +420,21 @@ def set!(resource, value)
resource.instance_variable_set(instance_variable_name, value)
end

# Loads lazy columns when get or set is called.
#-
# @api private
def lazy_load(resource)
# TODO: refactor this section
contexts = if lazy?
name
else
model.properties(resource.repository.name).reject do |property|
property.lazy? || resource.attribute_loaded?(property.name)
end
end
resource.send(:lazy_load, contexts)
end

# typecasts values into a primitive
#
# @return <TrueClass, String, Float, Integer, BigDecimal, DateTime, Date, Time
Expand Down Expand Up @@ -541,18 +548,23 @@ def initialize(model, name, type, options = {})
@length = @options.fetch(:length, @options.fetch(:size, DEFAULT_LENGTH))
elsif BigDecimal == @primitive || Float == @primitive
@precision = @options.fetch(:precision, DEFAULT_PRECISION)
@scale = @options.fetch(:scale, DEFAULT_SCALE)

default_scale = (Float == @primitive) ? DEFAULT_SCALE_FLOAT : DEFAULT_SCALE_BIGDECIMAL
@scale = @options.fetch(:scale, default_scale)
# @scale = @options.fetch(:scale, DEFAULT_SCALE_BIGDECIMAL)

unless @precision > 0
raise ArgumentError, "precision must be greater than 0, but was #{@precision.inspect}"
end

unless @scale >= 0
raise ArgumentError, "scale must be equal to or greater than 0, but was #{@scale.inspect}"
end
if (BigDecimal == @primitive) || (Float == @primitive && !@scale.nil?)
unless @scale >= 0
raise ArgumentError, "scale must be equal to or greater than 0, but was #{@scale.inspect}"
end

unless @precision >= @scale
raise ArgumentError, "precision must be equal to or greater than scale, but was #{@precision.inspect} and scale was #{@scale.inspect}"
unless @precision >= @scale
raise ArgumentError, "precision must be equal to or greater than scale, but was #{@precision.inspect} and scale was #{@scale.inspect}"
end
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/dm-core/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ def initialize(target, operator)

class Path
include Assertions

%w[ id type ].each { |m| undef_method m }

attr_reader :relationships, :model, :property, :operator

Expand Down
2 changes: 1 addition & 1 deletion lib/dm-core/types/discriminator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def self.descendants
after_class_method :inherited, :add_scope_for_discriminator
def self.add_scope_for_discriminator(target)
def self.add_scope_for_discriminator(retval, target)
target.descendants << target
target.default_scope.update(#{property.name.inspect} => target.descendants)
propagate_descendants(target)
Expand Down
9 changes: 9 additions & 0 deletions spec/integration/property_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ class RowBoat
result[1].trip_report.should_not be_nil
result[2].attribute_loaded?(:miles).should be_true
end

it "should lazy load on Property#set" do
repository(ADAPTER) do
boat = RowBoat.first
boat.attribute_loaded?(:notes).should be_false
boat.notes = 'New Note'
boat.original_values[:notes].should == "Note"
end
end
end

describe 'defaults' do
Expand Down
10 changes: 9 additions & 1 deletion spec/integration/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Region

property :id, Serial
property :name, String
property :type, String

def self.default_repository_name
ADAPTER
Expand Down Expand Up @@ -332,7 +333,7 @@ class << operator
before do
[ QuerySpec::Region, QuerySpec::Factory, QuerySpec::Vehicle ].each { |m| m.auto_migrate! }

QuerySpec::Region.create(:id => 1, :name => 'North West')
QuerySpec::Region.create(:id => 1, :name => 'North West', :type => 'commercial')
QuerySpec::Factory.create(:id => 2000, :region_id => 1, :name => 'North West Plant')
QuerySpec::Vehicle.create(:id => 1, :factory_id => 2000, :name => '10 ton delivery truck')

Expand Down Expand Up @@ -431,6 +432,13 @@ class << operator
vehicle.name.should == '10 ton delivery truck'
end

it "should accept 'id' and 'type' as endpoints on ah DM::QueryPath" do
vehicle = QuerySpec::Vehicle.first( QuerySpec::Vehicle.factory.region.type => 'commercial' )
vehicle.name.should == '10 ton delivery truck'
vehicle = QuerySpec::Vehicle.first( QuerySpec::Vehicle.factory.region.id => 1 )
vehicle.name.should == '10 ton delivery truck'
end

it 'should auto generate the link if a DM::Property from a different resource is in the :fields option'

it 'should create links with composite keys'
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/property_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class Tomato
describe '#set' do
before do
@original_values = {}
@resource = mock('resource', :kind_of? => true, :original_values => @original_values)
@resource = mock('resource', :kind_of? => true, :original_values => @original_values, :new_record? => true)
end

it 'should typecast the value' do
Expand Down
2 changes: 1 addition & 1 deletion tasks/hoe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def rubyforge_username

p.rubyforge_name = PROJECT_NAME if PROJECT_NAME

p.clean_globs |= ["{coverage,doc,log}/", "profile_results.*", "**/.*.sw?", "*.gem", ".config", "**/.DS_Store"]
p.clean_globs |= ["{coverage,doc,log}/", "**/*.db", "profile_results.*", "**/.*.sw?", "*.gem", ".config", "**/.DS_Store"]

GEM_DEPENDENCIES.each do |dep|
p.extra_deps << dep
Expand Down
2 changes: 1 addition & 1 deletion tasks/install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
task :install => :gem do
system %{gem install --no-rdoc --no-ri -l pkg/#{GEM_NAME}-#{GEM_VERSION}.gem}
end
GEM_NAMEspace :dev do
namespace :dev do
desc 'Install for development (for windows)'
task :winstall => :gem do
warn "You can now call 'rake install' instead of 'rake dev:winstall'."
Expand Down

0 comments on commit 2601ae4

Please sign in to comment.