Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Bring in SimpleSet code for small speed improvement

  • Loading branch information...
commit cd56e565bfe419167fc5e93e815bfbe7196fd67c 1 parent b377e1d
@sdsykes sdsykes authored
View
4 lib/callsite.rb
@@ -100,9 +100,9 @@ def inheritable?
#
def setup_columns
if inheritable?
- Set.new([primary_key, inheritance_column])
+ SimpleSet.new([primary_key, inheritance_column])
else
- primary_key.blank? ? Set.new : Set.new([primary_key])
+ primary_key.blank? ? SimpleSet.new : SimpleSet.new([primary_key])
end
end
View
2  lib/optimizations/associations/association_set.rb
@@ -18,7 +18,7 @@ class AssociationSet
Mtx = Mutex.new
def initialize
- @associations = Set.new
+ @associations = SimpleSet.new
@as_data_id = :"association_data_#{object_id}"
end
View
1  lib/scrooge.rb
@@ -2,6 +2,7 @@
require 'set'
require 'callsite'
+require 'simple_set'
require 'optimizations/columns/attributes_proxy'
require 'optimizations/columns/macro'
require 'optimizations/associations/macro'
View
91 lib/simple_set.rb
@@ -0,0 +1,91 @@
+module Scrooge
+
+ class SimpleSet < Hash
+
+ class << self
+ ##
+ # Creates a new set containing the given objects
+ #
+ # @return [SimpleSet] The new set
+ #
+ # @api public
+ def [](*ary)
+ new(ary)
+ end
+ end
+
+ ##
+ # Create a new SimpleSet containing the unique members of _arr_
+ #
+ # @param [Array] arr Initial set values.
+ #
+ # @return [Array] The array the Set was initialized with
+ #
+ # @api public
+ def initialize(arr = [])
+ Array(arr).each {|x| self[x] = true}
+ end
+
+ ##
+ # Add a value to the set, and return it
+ #
+ # @param [Object] value Value to add to set.
+ #
+ # @return [SimpleSet] Receiver
+ #
+ # @api public
+ def <<(value)
+ self[value] = true
+ self
+ end
+
+ ##
+ # Merge _arr_ with receiver, producing the union of receiver & _arr_
+ #
+ # s = Extlib::SimpleSet.new([:a, :b, :c])
+ # s.merge([:c, :d, :e, f]) #=> #<SimpleSet: {:e, :c, :f, :a, :d, :b}>
+ #
+ # @param [Array] arr Values to merge with set.
+ #
+ # @return [SimpleSet] The set after the Array was merged in.
+ #
+ # @api public
+ def merge(arr)
+ super(arr.inject({}) {|s,x| s[x] = true; s })
+ end
+ alias_method :|, :merge
+
+ ##
+ # Invokes block once for each item in the set. Creates an array
+ # containing the values returned by the block.
+ #
+ # s = Extlib::SimpleSet.new([1, 2, 3])
+ # s.collect {|s| s + 1} #=> [2, 3, 4]
+ #
+ # @return [Array] The values returned by the block
+ #
+ # @api public
+ def collect(&block)
+ keys.collect(&block)
+ end
+ alias_method :map, :collect
+
+ ##
+ # Get a human readable version of the set.
+ #
+ # s = SimpleSet.new([:a, :b, :c])
+ # s.inspect #=> "#<SimpleSet: {:c, :a, :b}>"
+ #
+ # @return [String] A human readable version of the set.
+ #
+ # @api public
+ def inspect
+ "#<SimpleSet: {#{keys.map {|x| x.inspect}.join(", ")}}>"
+ end
+
+ # def to_a
+ alias_method :to_a, :keys
+
+ end # SimpleSet
+
+end
View
4 test/callsite_test.rb
@@ -10,10 +10,10 @@ def setup
test "should initialize with a default set of columns" do
assert @callsite.columns.empty?
- assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, Set["User"]
+ assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, SimpleSet["User"]
Scrooge::Callsite.any_instance.stubs(:inheritable?).returns(true)
Scrooge::Callsite.any_instance.stubs(:inheritance_column).returns("inheritance")
- assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, Set["User","inheritance"]
+ assert_equal Scrooge::Callsite.new( MysqlUser, 123456 ).columns, SimpleSet["User","inheritance"]
end
test "should be inspectable" do
View
2  test/scrooge_test.rb
@@ -51,7 +51,7 @@ class ScroogeTest < ActiveRecord::TestCase
end
test "should be able to generate a SQL select snippet from a given set" do
- assert_equal MysqlUser.scrooge_select_sql( Set['Password','User','Host'] ), "`user`.User,`user`.Password,`user`.Host"
+ assert_equal MysqlUser.scrooge_select_sql( SimpleSet['Password','User','Host'] ), "`user`.User,`user`.Password,`user`.Host"
end
test "should be able to augment an existing callsite when attributes is referenced that we haven't seen yet" do

0 comments on commit cd56e56

Please sign in to comment.
Something went wrong with that request. Please try again.