Skip to content

Commit

Permalink
ActiveSdb: add implementation of default value for columns
Browse files Browse the repository at this point in the history
  • Loading branch information
rubysolo authored and Konstantin committed Apr 2, 2010
1 parent 962b50f commit 2b1ddb7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/sdb/active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,11 @@ def attributes=(attrs)
self.attributes
end

def connection
def columns
self.class.columns
end

def connection
self.class.connection
end

Expand Down Expand Up @@ -1032,6 +1036,9 @@ def raise_on_id_absence

def prepare_for_update
@attributes['id'] = self.class.generate_id if @attributes['id'].blank?
columns.all.each do |col_name|
self[col_name] ||= columns.default(col_name)
end
end

def uniq_values(attributes=nil) # :nodoc:
Expand All @@ -1058,6 +1065,10 @@ def initialize
@columns = {}
end

def all
@columns.keys
end

def column(col_name)
@columns[col_name.to_s]
end
Expand All @@ -1068,7 +1079,9 @@ def type_of(col_name)
end

def default(col_name)
column(col_name) && column(col_name)[:default]
return nil unless include?(col_name)
default = column(col_name)[:default]
default.respond_to?(:call) ? default.call : default
end

def method_missing(method_sym, *args)
Expand Down
5 changes: 5 additions & 0 deletions test/sdb/test_active_sdb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ def test_15_column_emulation
assert person['registered_at'].is_a?(DateTime)
assert person[:registered_at].is_a?(DateTime)

assert ! person[:created_at].nil?
assert_equal DateTime, person.created_at.class
assert person['created_at'].is_a?(DateTime)
assert person[:created_at].is_a?(DateTime)

assert person.is_active

assert_equal 100, person.score
Expand Down

0 comments on commit 2b1ddb7

Please sign in to comment.