datastax_rails 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (148) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +62 -0
  3. data/Rakefile +34 -0
  4. data/config/schema.xml +266 -0
  5. data/config/schema.xml.erb +70 -0
  6. data/config/solrconfig.xml +1564 -0
  7. data/config/stopwords.txt +58 -0
  8. data/lib/datastax_rails/associations/association.rb +224 -0
  9. data/lib/datastax_rails/associations/association_scope.rb +25 -0
  10. data/lib/datastax_rails/associations/belongs_to_association.rb +64 -0
  11. data/lib/datastax_rails/associations/builder/association.rb +56 -0
  12. data/lib/datastax_rails/associations/builder/belongs_to.rb +30 -0
  13. data/lib/datastax_rails/associations/builder/collection_association.rb +48 -0
  14. data/lib/datastax_rails/associations/builder/has_and_belongs_to_many.rb +36 -0
  15. data/lib/datastax_rails/associations/builder/has_many.rb +54 -0
  16. data/lib/datastax_rails/associations/builder/has_one.rb +52 -0
  17. data/lib/datastax_rails/associations/builder/singular_association.rb +56 -0
  18. data/lib/datastax_rails/associations/collection_association.rb +274 -0
  19. data/lib/datastax_rails/associations/collection_proxy.rb +118 -0
  20. data/lib/datastax_rails/associations/has_and_belongs_to_many_association.rb +44 -0
  21. data/lib/datastax_rails/associations/has_many_association.rb +58 -0
  22. data/lib/datastax_rails/associations/has_one_association.rb +68 -0
  23. data/lib/datastax_rails/associations/singular_association.rb +58 -0
  24. data/lib/datastax_rails/associations.rb +86 -0
  25. data/lib/datastax_rails/attribute_methods/definition.rb +20 -0
  26. data/lib/datastax_rails/attribute_methods/dirty.rb +43 -0
  27. data/lib/datastax_rails/attribute_methods/typecasting.rb +50 -0
  28. data/lib/datastax_rails/attribute_methods.rb +104 -0
  29. data/lib/datastax_rails/base.rb +587 -0
  30. data/lib/datastax_rails/batches.rb +35 -0
  31. data/lib/datastax_rails/callbacks.rb +37 -0
  32. data/lib/datastax_rails/collection.rb +9 -0
  33. data/lib/datastax_rails/connection.rb +21 -0
  34. data/lib/datastax_rails/consistency.rb +33 -0
  35. data/lib/datastax_rails/cql/base.rb +15 -0
  36. data/lib/datastax_rails/cql/column_family.rb +38 -0
  37. data/lib/datastax_rails/cql/consistency.rb +13 -0
  38. data/lib/datastax_rails/cql/create_column_family.rb +63 -0
  39. data/lib/datastax_rails/cql/create_keyspace.rb +30 -0
  40. data/lib/datastax_rails/cql/delete.rb +41 -0
  41. data/lib/datastax_rails/cql/drop_column_family.rb +13 -0
  42. data/lib/datastax_rails/cql/drop_keyspace.rb +13 -0
  43. data/lib/datastax_rails/cql/insert.rb +53 -0
  44. data/lib/datastax_rails/cql/select.rb +51 -0
  45. data/lib/datastax_rails/cql/truncate.rb +13 -0
  46. data/lib/datastax_rails/cql/update.rb +68 -0
  47. data/lib/datastax_rails/cql/use_keyspace.rb +13 -0
  48. data/lib/datastax_rails/cql.rb +25 -0
  49. data/lib/datastax_rails/cursor.rb +90 -0
  50. data/lib/datastax_rails/errors.rb +16 -0
  51. data/lib/datastax_rails/identity/abstract_key_factory.rb +26 -0
  52. data/lib/datastax_rails/identity/custom_key_factory.rb +36 -0
  53. data/lib/datastax_rails/identity/hashed_natural_key_factory.rb +10 -0
  54. data/lib/datastax_rails/identity/natural_key_factory.rb +37 -0
  55. data/lib/datastax_rails/identity/uuid_key_factory.rb +23 -0
  56. data/lib/datastax_rails/identity.rb +53 -0
  57. data/lib/datastax_rails/log_subscriber.rb +37 -0
  58. data/lib/datastax_rails/migrations/migration.rb +15 -0
  59. data/lib/datastax_rails/migrations.rb +36 -0
  60. data/lib/datastax_rails/mocking.rb +15 -0
  61. data/lib/datastax_rails/persistence.rb +133 -0
  62. data/lib/datastax_rails/railtie.rb +20 -0
  63. data/lib/datastax_rails/reflection.rb +472 -0
  64. data/lib/datastax_rails/relation/finder_methods.rb +184 -0
  65. data/lib/datastax_rails/relation/modification_methods.rb +80 -0
  66. data/lib/datastax_rails/relation/search_methods.rb +349 -0
  67. data/lib/datastax_rails/relation/spawn_methods.rb +107 -0
  68. data/lib/datastax_rails/relation.rb +393 -0
  69. data/lib/datastax_rails/schema/migration.rb +106 -0
  70. data/lib/datastax_rails/schema/migration_proxy.rb +25 -0
  71. data/lib/datastax_rails/schema/migrator.rb +212 -0
  72. data/lib/datastax_rails/schema.rb +37 -0
  73. data/lib/datastax_rails/scoping.rb +394 -0
  74. data/lib/datastax_rails/serialization.rb +6 -0
  75. data/lib/datastax_rails/tasks/column_family.rb +162 -0
  76. data/lib/datastax_rails/tasks/ds.rake +63 -0
  77. data/lib/datastax_rails/tasks/keyspace.rb +57 -0
  78. data/lib/datastax_rails/timestamps.rb +19 -0
  79. data/lib/datastax_rails/type.rb +16 -0
  80. data/lib/datastax_rails/types/array_type.rb +77 -0
  81. data/lib/datastax_rails/types/base_type.rb +26 -0
  82. data/lib/datastax_rails/types/binary_type.rb +15 -0
  83. data/lib/datastax_rails/types/boolean_type.rb +22 -0
  84. data/lib/datastax_rails/types/date_type.rb +17 -0
  85. data/lib/datastax_rails/types/float_type.rb +18 -0
  86. data/lib/datastax_rails/types/integer_type.rb +18 -0
  87. data/lib/datastax_rails/types/string_type.rb +16 -0
  88. data/lib/datastax_rails/types/text_type.rb +16 -0
  89. data/lib/datastax_rails/types/time_type.rb +17 -0
  90. data/lib/datastax_rails/types.rb +9 -0
  91. data/lib/datastax_rails/validations/uniqueness.rb +119 -0
  92. data/lib/datastax_rails/validations.rb +48 -0
  93. data/lib/datastax_rails/version.rb +3 -0
  94. data/lib/datastax_rails.rb +87 -0
  95. data/lib/solr_no_escape.rb +28 -0
  96. data/spec/datastax_rails/associations/belongs_to_association_spec.rb +7 -0
  97. data/spec/datastax_rails/associations/has_many_association_spec.rb +37 -0
  98. data/spec/datastax_rails/associations_spec.rb +22 -0
  99. data/spec/datastax_rails/attribute_methods_spec.rb +23 -0
  100. data/spec/datastax_rails/base_spec.rb +15 -0
  101. data/spec/datastax_rails/cql/select_spec.rb +12 -0
  102. data/spec/datastax_rails/cql/update_spec.rb +0 -0
  103. data/spec/datastax_rails/relation/finder_methods_spec.rb +54 -0
  104. data/spec/datastax_rails/relation/modification_methods_spec.rb +41 -0
  105. data/spec/datastax_rails/relation/search_methods_spec.rb +117 -0
  106. data/spec/datastax_rails/relation/spawn_methods_spec.rb +28 -0
  107. data/spec/datastax_rails/relation_spec.rb +130 -0
  108. data/spec/datastax_rails/validations/uniqueness_spec.rb +41 -0
  109. data/spec/datastax_rails_spec.rb +5 -0
  110. data/spec/dummy/Rakefile +8 -0
  111. data/spec/dummy/app/assets/javascripts/application.js +9 -0
  112. data/spec/dummy/app/assets/stylesheets/application.css +7 -0
  113. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  114. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  115. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  116. data/spec/dummy/config/application.rb +47 -0
  117. data/spec/dummy/config/boot.rb +10 -0
  118. data/spec/dummy/config/database.yml +25 -0
  119. data/spec/dummy/config/datastax.yml +18 -0
  120. data/spec/dummy/config/environment.rb +5 -0
  121. data/spec/dummy/config/environments/development.rb +30 -0
  122. data/spec/dummy/config/environments/production.rb +60 -0
  123. data/spec/dummy/config/environments/test.rb +39 -0
  124. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  125. data/spec/dummy/config/initializers/inflections.rb +10 -0
  126. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  127. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  128. data/spec/dummy/config/initializers/session_store.rb +8 -0
  129. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  130. data/spec/dummy/config/locales/en.yml +5 -0
  131. data/spec/dummy/config/routes.rb +58 -0
  132. data/spec/dummy/config/sunspot.yml +17 -0
  133. data/spec/dummy/config.ru +4 -0
  134. data/spec/dummy/ks/migrate/20111117224534_models.rb +20 -0
  135. data/spec/dummy/ks/schema.json +180 -0
  136. data/spec/dummy/log/development.log +298 -0
  137. data/spec/dummy/log/production.log +0 -0
  138. data/spec/dummy/log/test.log +20307 -0
  139. data/spec/dummy/public/404.html +26 -0
  140. data/spec/dummy/public/422.html +26 -0
  141. data/spec/dummy/public/500.html +26 -0
  142. data/spec/dummy/public/favicon.ico +0 -0
  143. data/spec/dummy/script/rails +6 -0
  144. data/spec/spec.opts +5 -0
  145. data/spec/spec_helper.rb +29 -0
  146. data/spec/support/datastax_test_hook.rb +14 -0
  147. data/spec/support/models.rb +72 -0
  148. metadata +353 -0
@@ -0,0 +1,86 @@
1
+ module DatastaxRails
2
+ module Associations
3
+ extend ActiveSupport::Concern
4
+ extend ActiveSupport::Autoload
5
+
6
+ autoload :Association
7
+ autoload :AssociationScope
8
+ autoload :SingularAssociation
9
+ autoload :CollectionAssociation
10
+ autoload :CollectionProxy
11
+ autoload :BelongsToAssociation
12
+ autoload :HasOneAssociation
13
+ autoload :HasManyAssociation
14
+
15
+ module Builder
16
+ extend ActiveSupport::Autoload
17
+
18
+ autoload :Association
19
+ autoload :SingularAssociation
20
+ autoload :CollectionAssociation
21
+
22
+ autoload :BelongsTo
23
+ autoload :HasOne
24
+ autoload :HasMany
25
+ end
26
+
27
+ # Clears out the association cache.
28
+ def clear_association_cache #:nodoc:
29
+ @association_cache.clear if persisted?
30
+ end
31
+
32
+ # :nodoc:
33
+ attr_reader :association_cache
34
+
35
+ # Returns the association instance for the given name, instantiating it if it doesn't already exist
36
+ def association(name) #:nodoc:
37
+ association = association_instance_get(name)
38
+
39
+ if association.nil?
40
+ reflection = self.class.reflect_on_association(name)
41
+ association = reflection.association_class.new(self, reflection)
42
+ association_instance_set(name, association)
43
+ end
44
+
45
+ association
46
+ end
47
+
48
+ private
49
+ # Returns the specified association instance if it responds to :loaded?, nil otherwise.
50
+ def association_instance_get(name)
51
+ @association_cache ||= {}
52
+ @association_cache[name.to_sym]
53
+ end
54
+
55
+ # Set the specified association instance.
56
+ def association_instance_set(name, association)
57
+ @association_cache ||= {}
58
+ @association_cache[name] = association
59
+ end
60
+
61
+ module ClassMethods
62
+ def belongs_to(name, options = {})
63
+ Builder::BelongsTo.build(self, name, options)
64
+ end
65
+
66
+ def has_many(name, options = {})
67
+ Builder::HasMany.build(self, name, options)
68
+ # klass = options[:class_name]
69
+ # klass ||= name.to_s.singularize
70
+ # foreign_key = options[:foreign_key]
71
+ # foreign_key ||= self.name.foreign_key
72
+ # define_method name do
73
+ # klass.where(foreign_key)
74
+ # end
75
+ end
76
+
77
+ def has_and_belongs_to_many(name, options = {})
78
+
79
+ end
80
+
81
+ def has_one(name, options = {})
82
+ Builder::HasOne.build(self, name, options)
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ module DatastaxRails
2
+ module AttributeMethods
3
+ class Definition
4
+ attr_reader :name, :coder, :lazy
5
+ def initialize(name, coder, options)
6
+ @name = name.to_s
7
+ @lazy = options.delete(:lazy)
8
+ @coder = coder.new(options)
9
+ end
10
+
11
+ def instantiate(record, value)
12
+ value ||= coder.default
13
+ return unless value
14
+
15
+ value = value.kind_of?(String) ? coder.decode(value) : value
16
+ coder.wrap(record, name, value)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,43 @@
1
+ module DatastaxRails
2
+ module AttributeMethods
3
+ module Dirty
4
+ extend ActiveSupport::Concern
5
+ include ActiveModel::Dirty
6
+
7
+ # Attempts to +save+ the record and clears changed attributes if successful.
8
+ def save(*) #:nodoc:
9
+ if status = super
10
+ @previously_changed = changes
11
+ @changed_attributes.clear
12
+ end
13
+ status
14
+ end
15
+
16
+ # Attempts to <tt>save!</tt> the record and clears changed attributes if successful.
17
+ def save!(*) #:nodoc:
18
+ super.tap do
19
+ @previously_changed = changes
20
+ @changed_attributes.clear
21
+ end
22
+ end
23
+
24
+ # <tt>reload</tt> the record and clears changed attributes.
25
+ def reload
26
+ super.tap do
27
+ @previously_changed.try :clear
28
+ @changed_attributes.try :clear
29
+ end
30
+ end
31
+
32
+ def write_attribute(name, value)
33
+ name = name.to_s
34
+ old = read_attribute(name)
35
+ super
36
+
37
+ unless attribute_changed?(name) || old == read_attribute(name)
38
+ changed_attributes[name] = old
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,50 @@
1
+ module DatastaxRails
2
+ module AttributeMethods
3
+ module Typecasting
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ class_attribute :attribute_definitions
8
+ class_attribute :lazy_attributes
9
+ class_attribute :readonly_attributes
10
+ self.attribute_definitions = {}
11
+ self.lazy_attributes = []
12
+ self.readonly_attributes = []
13
+
14
+ %w(array boolean date float integer json string text time time_with_zone).each do |type|
15
+ instance_eval <<-EOV, __FILE__, __LINE__ + 1
16
+ def #{type}(name, options = {}) # def string(name, options = {})
17
+ attribute(name, options.update(:type => :#{type})) # attribute(name, options.update(type: :string))
18
+ end # end
19
+ EOV
20
+ end
21
+
22
+ def self.binary(name, options = {})
23
+ options.reverse_merge!(:lazy => true)
24
+ attribute(name, options.update(:type => :binary))
25
+ end
26
+
27
+ def self.timestamps(options = {})
28
+ attribute(:created_at, options.update(:type => :time))
29
+ attribute(:updated_at, options.update(:type => :time))
30
+ end
31
+ end
32
+
33
+ module ClassMethods
34
+ def inherited(child)
35
+ super
36
+ child.attribute_definitions = attribute_definitions.dup
37
+ self.models << child
38
+ end
39
+
40
+ def typecast_attribute(record, name, value)
41
+ if attribute_definition = attribute_definitions[name.to_sym]
42
+ attribute_definition.instantiate(record, value)
43
+ else
44
+ raise NoMethodError, "Unknown attribute #{name.inspect}"
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,104 @@
1
+ module DatastaxRails
2
+ module AttributeMethods
3
+ extend ActiveSupport::Concern
4
+ include ActiveModel::AttributeMethods
5
+
6
+ included do
7
+ alias :[] :read_attribute
8
+ alias :[]= :write_attribute
9
+
10
+ attribute_method_suffix("=")
11
+ end
12
+
13
+ module ClassMethods
14
+ def define_attribute_methods
15
+ return if attribute_methods_generated?
16
+ super(attribute_definitions.keys)
17
+ # Remove setter methods from readonly attributes
18
+ readonly_attributes.each do |attr|
19
+ remove_method("#{attr}=".to_sym) if method_defined?("#{attr}=".to_sym)
20
+ end
21
+ @attribute_methods_generated = true
22
+ end
23
+
24
+ def attribute_methods_generated?
25
+ @attribute_methods_generated ||= false
26
+ end
27
+
28
+ #
29
+ # attribute :name, :type => :string
30
+ # attribute :ammo, :type => Ammo, :coder => AmmoCodec
31
+ #
32
+ def attribute(name, options)
33
+ type = options.delete :type
34
+ coder = options.delete :coder
35
+
36
+ if type.is_a?(Symbol)
37
+ coder = DatastaxRails::Type.get_coder(type) || (raise "Unknown type #{type}")
38
+ elsif coder.nil?
39
+ raise "Must supply a :coder for #{name}"
40
+ end
41
+
42
+ if(options[:lazy])
43
+ lazy_attributes << name.to_sym
44
+ end
45
+
46
+ if(options[:readonly])
47
+ readonly_attributes << name.to_sym
48
+ end
49
+
50
+ attribute_definitions[name.to_sym] = AttributeMethods::Definition.new(name, coder, options)
51
+ end
52
+ end
53
+
54
+ def write_attribute(name, value)
55
+ @attributes[name.to_s] = self.class.typecast_attribute(self, name, value)
56
+ end
57
+
58
+ def read_attribute(name)
59
+ if(lazy_attributes.include?(name.to_sym) && @attributes[name.to_s].nil? && persisted?)
60
+ @attributes[name.to_s] = self.class.select(name).with_cassandra.find(self.id).read_attribute(name)
61
+ end
62
+
63
+ @attributes[name.to_s]
64
+ end
65
+
66
+ def attribute_exists?(name)
67
+ @attributes.key?(name.to_s)
68
+ end
69
+
70
+ def attributes=(attributes)
71
+ attributes.each do |(name, value)|
72
+ send("#{name}=", value)
73
+ end
74
+ end
75
+
76
+ def method_missing(method_id, *args, &block)
77
+ if !self.class.attribute_methods_generated?
78
+ self.class.define_attribute_methods
79
+ send(method_id, *args, &block)
80
+ else
81
+ super
82
+ end
83
+ end
84
+
85
+ def respond_to?(*args)
86
+ self.class.define_attribute_methods unless self.class.attribute_methods_generated?
87
+ super
88
+ end
89
+
90
+ protected
91
+ def attribute_method?(name)
92
+ !!attribute_definitions[name.to_sym]
93
+ end
94
+
95
+ private
96
+ def attribute(name)
97
+ read_attribute(name)
98
+ end
99
+
100
+ def attribute=(name, value)
101
+ write_attribute(name, value)
102
+ end
103
+ end
104
+ end