peleteiro-activecouch 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README +28 -0
  3. data/Rakefile +52 -0
  4. data/VERSION +1 -0
  5. data/lib/active_couch.rb +12 -0
  6. data/lib/active_couch/base.rb +608 -0
  7. data/lib/active_couch/callbacks.rb +89 -0
  8. data/lib/active_couch/connection.rb +164 -0
  9. data/lib/active_couch/errors.rb +13 -0
  10. data/lib/active_couch/support.rb +3 -0
  11. data/lib/active_couch/support/exporter.rb +97 -0
  12. data/lib/active_couch/support/extensions.rb +86 -0
  13. data/lib/active_couch/support/inflections.rb +52 -0
  14. data/lib/active_couch/support/inflector.rb +279 -0
  15. data/lib/active_couch/views.rb +3 -0
  16. data/lib/active_couch/views/errors.rb +4 -0
  17. data/lib/active_couch/views/raw_view.rb +40 -0
  18. data/lib/active_couch/views/view.rb +85 -0
  19. data/lib/activecouch.rb +1 -0
  20. data/spec/base/after_delete_spec.rb +110 -0
  21. data/spec/base/after_save_spec.rb +102 -0
  22. data/spec/base/before_delete_spec.rb +109 -0
  23. data/spec/base/before_save_spec.rb +101 -0
  24. data/spec/base/count_all_spec.rb +29 -0
  25. data/spec/base/count_spec.rb +77 -0
  26. data/spec/base/create_spec.rb +28 -0
  27. data/spec/base/database_spec.rb +70 -0
  28. data/spec/base/delete_spec.rb +97 -0
  29. data/spec/base/find_from_url_spec.rb +55 -0
  30. data/spec/base/find_spec.rb +383 -0
  31. data/spec/base/from_json_spec.rb +54 -0
  32. data/spec/base/has_many_spec.rb +89 -0
  33. data/spec/base/has_spec.rb +88 -0
  34. data/spec/base/id_spec.rb +25 -0
  35. data/spec/base/initialize_spec.rb +91 -0
  36. data/spec/base/marshal_dump_spec.rb +64 -0
  37. data/spec/base/marshal_load_spec.rb +58 -0
  38. data/spec/base/module_spec.rb +18 -0
  39. data/spec/base/nested_class_spec.rb +19 -0
  40. data/spec/base/rev_spec.rb +20 -0
  41. data/spec/base/save_spec.rb +130 -0
  42. data/spec/base/site_spec.rb +62 -0
  43. data/spec/base/to_json_spec.rb +73 -0
  44. data/spec/connection/initialize_spec.rb +28 -0
  45. data/spec/exporter/all_databases_spec.rb +24 -0
  46. data/spec/exporter/create_database_spec.rb +47 -0
  47. data/spec/exporter/delete_database_spec.rb +45 -0
  48. data/spec/exporter/delete_spec.rb +36 -0
  49. data/spec/exporter/export_spec.rb +62 -0
  50. data/spec/exporter/export_with_raw_views_spec.rb +66 -0
  51. data/spec/spec_helper.rb +9 -0
  52. data/spec/views/define_spec.rb +34 -0
  53. data/spec/views/include_attributes_spec.rb +30 -0
  54. data/spec/views/raw_view_spec.rb +49 -0
  55. data/spec/views/to_json_spec.rb +58 -0
  56. data/spec/views/with_filter_spec.rb +13 -0
  57. data/spec/views/with_key_spec.rb +19 -0
  58. metadata +117 -0
@@ -0,0 +1,52 @@
1
+ ActiveCouch::Inflector.inflections do |inflect|
2
+ inflect.plural(/$/, 's')
3
+ inflect.plural(/s$/i, 's')
4
+ inflect.plural(/(ax|test)is$/i, '\1es')
5
+ inflect.plural(/(octop|vir)us$/i, '\1i')
6
+ inflect.plural(/(alias|status)$/i, '\1es')
7
+ inflect.plural(/(bu)s$/i, '\1ses')
8
+ inflect.plural(/(buffal|tomat)o$/i, '\1oes')
9
+ inflect.plural(/([ti])um$/i, '\1a')
10
+ inflect.plural(/sis$/i, 'ses')
11
+ inflect.plural(/(?:([^f])fe|([lr])f)$/i, '\1\2ves')
12
+ inflect.plural(/(hive)$/i, '\1s')
13
+ inflect.plural(/([^aeiouy]|qu)y$/i, '\1ies')
14
+ inflect.plural(/(x|ch|ss|sh)$/i, '\1es')
15
+ inflect.plural(/(matr|vert|ind)ix|ex$/i, '\1ices')
16
+ inflect.plural(/([m|l])ouse$/i, '\1ice')
17
+ inflect.plural(/^(ox)$/i, '\1en')
18
+ inflect.plural(/(quiz)$/i, '\1zes')
19
+
20
+ inflect.singular(/s$/i, '')
21
+ inflect.singular(/(n)ews$/i, '\1ews')
22
+ inflect.singular(/([ti])a$/i, '\1um')
23
+ inflect.singular(/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i, '\1\2sis')
24
+ inflect.singular(/(^analy)ses$/i, '\1sis')
25
+ inflect.singular(/([^f])ves$/i, '\1fe')
26
+ inflect.singular(/(hive)s$/i, '\1')
27
+ inflect.singular(/(tive)s$/i, '\1')
28
+ inflect.singular(/([lr])ves$/i, '\1f')
29
+ inflect.singular(/([^aeiouy]|qu)ies$/i, '\1y')
30
+ inflect.singular(/(s)eries$/i, '\1eries')
31
+ inflect.singular(/(m)ovies$/i, '\1ovie')
32
+ inflect.singular(/(x|ch|ss|sh)es$/i, '\1')
33
+ inflect.singular(/([m|l])ice$/i, '\1ouse')
34
+ inflect.singular(/(bus)es$/i, '\1')
35
+ inflect.singular(/(o)es$/i, '\1')
36
+ inflect.singular(/(shoe)s$/i, '\1')
37
+ inflect.singular(/(cris|ax|test)es$/i, '\1is')
38
+ inflect.singular(/(octop|vir)i$/i, '\1us')
39
+ inflect.singular(/(alias|status)es$/i, '\1')
40
+ inflect.singular(/^(ox)en/i, '\1')
41
+ inflect.singular(/(vert|ind)ices$/i, '\1ex')
42
+ inflect.singular(/(matr)ices$/i, '\1ix')
43
+ inflect.singular(/(quiz)zes$/i, '\1')
44
+
45
+ inflect.irregular('person', 'people')
46
+ inflect.irregular('man', 'men')
47
+ inflect.irregular('child', 'children')
48
+ inflect.irregular('sex', 'sexes')
49
+ inflect.irregular('move', 'moves')
50
+
51
+ inflect.uncountable(%w(equipment information rice money species series fish sheep))
52
+ end
@@ -0,0 +1,279 @@
1
+ # This file is copied from the ActiveSupport project, which
2
+ # is a part of the Ruby On Rails web-framework (http://rubyonrails.org).
3
+ require 'singleton'
4
+
5
+ # The Inflector transforms words from singular to plural, class names to table names, modularized class names to ones without,
6
+ # and class names to foreign keys. The default inflections for pluralization, singularization, and uncountable words are kept
7
+ # in inflections.rb.
8
+ module ActiveCouch
9
+ module Inflector
10
+ # A singleton instance of this class is yielded by Inflector.inflections, which can then be used to specify additional
11
+ # inflection rules. Examples:
12
+ #
13
+ # Inflector.inflections do |inflect|
14
+ # inflect.plural /^(ox)$/i, '\1\2en'
15
+ # inflect.singular /^(ox)en/i, '\1'
16
+ #
17
+ # inflect.irregular 'octopus', 'octopi'
18
+ #
19
+ # inflect.uncountable "equipment"
20
+ # end
21
+ #
22
+ # New rules are added at the top. So in the example above, the irregular rule for octopus will now be the first of the
23
+ # pluralization and singularization rules that is runs. This guarantees that your rules run before any of the rules that may
24
+ # already have been loaded.
25
+ class Inflections
26
+ include Singleton
27
+
28
+ attr_reader :plurals, :singulars, :uncountables
29
+
30
+ def initialize
31
+ @plurals, @singulars, @uncountables = [], [], []
32
+ end
33
+
34
+ # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
35
+ # The replacement should always be a string that may include references to the matched data from the rule.
36
+ def plural(rule, replacement)
37
+ @plurals.insert(0, [rule, replacement])
38
+ end
39
+
40
+ # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
41
+ # The replacement should always be a string that may include references to the matched data from the rule.
42
+ def singular(rule, replacement)
43
+ @singulars.insert(0, [rule, replacement])
44
+ end
45
+
46
+ # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
47
+ # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
48
+ #
49
+ # Examples:
50
+ # irregular 'octopus', 'octopi'
51
+ # irregular 'person', 'people'
52
+ def irregular(singular, plural)
53
+ plural(Regexp.new("(#{singular[0,1]})#{singular[1..-1]}$", "i"), '\1' + plural[1..-1])
54
+ singular(Regexp.new("(#{plural[0,1]})#{plural[1..-1]}$", "i"), '\1' + singular[1..-1])
55
+ end
56
+
57
+ # Add uncountable words that shouldn't be attempted inflected.
58
+ #
59
+ # Examples:
60
+ # uncountable "money"
61
+ # uncountable "money", "information"
62
+ # uncountable %w( money information rice )
63
+ def uncountable(*words)
64
+ (@uncountables << words).flatten!
65
+ end
66
+
67
+ # Clears the loaded inflections within a given scope (default is :all). Give the scope as a symbol of the inflection type,
68
+ # the options are: :plurals, :singulars, :uncountables
69
+ #
70
+ # Examples:
71
+ # clear :all
72
+ # clear :plurals
73
+ def clear(scope = :all)
74
+ case scope
75
+ when :all
76
+ @plurals, @singulars, @uncountables = [], [], []
77
+ else
78
+ instance_variable_set "@#{scope}", []
79
+ end
80
+ end
81
+ end
82
+
83
+ extend self
84
+
85
+ def inflections
86
+ if block_given?
87
+ yield Inflections.instance
88
+ else
89
+ Inflections.instance
90
+ end
91
+ end
92
+
93
+ # Returns the plural form of the word in the string.
94
+ #
95
+ # Examples
96
+ # "post".pluralize #=> "posts"
97
+ # "octopus".pluralize #=> "octopi"
98
+ # "sheep".pluralize #=> "sheep"
99
+ # "words".pluralize #=> "words"
100
+ # "the blue mailman".pluralize #=> "the blue mailmen"
101
+ # "CamelOctopus".pluralize #=> "CamelOctopi"
102
+ def pluralize(word)
103
+ result = word.to_s.dup
104
+
105
+ if inflections.uncountables.include?(result.downcase)
106
+ result
107
+ else
108
+ inflections.plurals.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
109
+ result
110
+ end
111
+ end
112
+
113
+ # The reverse of pluralize, returns the singular form of a word in a string.
114
+ #
115
+ # Examples
116
+ # "posts".singularize #=> "post"
117
+ # "octopi".singularize #=> "octopus"
118
+ # "sheep".singluarize #=> "sheep"
119
+ # "word".singluarize #=> "word"
120
+ # "the blue mailmen".singularize #=> "the blue mailman"
121
+ # "CamelOctopi".singularize #=> "CamelOctopus"
122
+ def singularize(word)
123
+ result = word.to_s.dup
124
+
125
+ if inflections.uncountables.include?(result.downcase)
126
+ result
127
+ else
128
+ inflections.singulars.each { |(rule, replacement)| break if result.gsub!(rule, replacement) }
129
+ result
130
+ end
131
+ end
132
+
133
+ # By default, camelize converts strings to UpperCamelCase. If the argument to camelize
134
+ # is set to ":lower" then camelize produces lowerCamelCase.
135
+ #
136
+ # camelize will also convert '/' to '::' which is useful for converting paths to namespaces
137
+ #
138
+ # Examples
139
+ # "active_record".camelize #=> "ActiveRecord"
140
+ # "active_record".camelize(:lower) #=> "activeRecord"
141
+ # "active_record/errors".camelize #=> "ActiveRecord::Errors"
142
+ # "active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
143
+ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
144
+ if first_letter_in_uppercase
145
+ lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase }
146
+ else
147
+ lower_case_and_underscored_word.first + camelize(lower_case_and_underscored_word)[1..-1]
148
+ end
149
+ end
150
+
151
+ # Capitalizes all the words and replaces some characters in the string to create
152
+ # a nicer looking title. Titleize is meant for creating pretty output. It is not
153
+ # used in the Rails internals.
154
+ #
155
+ # titleize is also aliased as as titlecase
156
+ #
157
+ # Examples
158
+ # "man from the boondocks".titleize #=> "Man From The Boondocks"
159
+ # "x-men: the last stand".titleize #=> "X Men: The Last Stand"
160
+ def titleize(word)
161
+ humanize(underscore(word)).gsub(/\b([a-z])/) { $1.capitalize }
162
+ end
163
+
164
+ # The reverse of +camelize+. Makes an underscored form from the expression in the string.
165
+ #
166
+ # Changes '::' to '/' to convert namespaces to paths.
167
+ #
168
+ # Examples
169
+ # "ActiveRecord".underscore #=> "active_record"
170
+ # "ActiveRecord::Errors".underscore #=> active_record/errors
171
+ def underscore(camel_cased_word)
172
+ camel_cased_word.to_s.gsub(/::/, '/').
173
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
174
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
175
+ tr("-", "_").
176
+ downcase
177
+ end
178
+
179
+ # Replaces underscores with dashes in the string.
180
+ #
181
+ # Example
182
+ # "puni_puni" #=> "puni-puni"
183
+ def dasherize(underscored_word)
184
+ underscored_word.gsub(/_/, '-')
185
+ end
186
+
187
+ # Capitalizes the first word and turns underscores into spaces and strips _id.
188
+ # Like titleize, this is meant for creating pretty output.
189
+ #
190
+ # Examples
191
+ # "employee_salary" #=> "Employee salary"
192
+ # "author_id" #=> "Author"
193
+ def humanize(lower_case_and_underscored_word)
194
+ lower_case_and_underscored_word.to_s.gsub(/_id$/, "").gsub(/_/, " ").capitalize
195
+ end
196
+
197
+ # Removes the module part from the expression in the string
198
+ #
199
+ # Examples
200
+ # "ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
201
+ # "Inflections".demodulize #=> "Inflections"
202
+ def demodulize(class_name_in_module)
203
+ class_name_in_module.to_s.gsub(/^.*::/, '')
204
+ end
205
+
206
+ # Create the name of a table like Rails does for models to table names. This method
207
+ # uses the pluralize method on the last word in the string.
208
+ #
209
+ # Examples
210
+ # "RawScaledScorer".tableize #=> "raw_scaled_scorers"
211
+ # "egg_and_ham".tableize #=> "egg_and_hams"
212
+ # "fancyCategory".tableize #=> "fancy_categories"
213
+ def tableize(class_name)
214
+ pluralize(underscore(class_name))
215
+ end
216
+
217
+ # Create a class name from a table name like Rails does for table names to models.
218
+ # Note that this returns a string and not a Class. (To convert to an actual class
219
+ # follow classify with constantize.)
220
+ #
221
+ # Examples
222
+ # "egg_and_hams".classify #=> "EggAndHam"
223
+ # "post".classify #=> "Post"
224
+ def classify(table_name)
225
+ # strip out any leading schema name
226
+ camelize(singularize(table_name.to_s.sub(/.*\./, '')))
227
+ end
228
+
229
+ # Creates a foreign key name from a class name.
230
+ # +separate_class_name_and_id_with_underscore+ sets whether
231
+ # the method should put '_' between the name and 'id'.
232
+ #
233
+ # Examples
234
+ # "Message".foreign_key #=> "message_id"
235
+ # "Message".foreign_key(false) #=> "messageid"
236
+ # "Admin::Post".foreign_key #=> "post_id"
237
+ def foreign_key(class_name, separate_class_name_and_id_with_underscore = true)
238
+ underscore(demodulize(class_name)) + (separate_class_name_and_id_with_underscore ? "_id" : "id")
239
+ end
240
+
241
+ # Constantize tries to find a declared constant with the name specified
242
+ # in the string. It raises a NameError when the name is not in CamelCase
243
+ # or is not initialized.
244
+ #
245
+ # Examples
246
+ # "Module".constantize #=> Module
247
+ # "Class".constantize #=> Class
248
+ def constantize(camel_cased_word)
249
+ unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ camel_cased_word
250
+ raise NameError, "#{camel_cased_word.inspect} is not a valid constant name!"
251
+ end
252
+
253
+ Object.module_eval("::#{$1}", __FILE__, __LINE__)
254
+ end
255
+
256
+ # Ordinalize turns a number into an ordinal string used to denote the
257
+ # position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
258
+ #
259
+ # Examples
260
+ # ordinalize(1) # => "1st"
261
+ # ordinalize(2) # => "2nd"
262
+ # ordinalize(1002) # => "1002nd"
263
+ # ordinalize(1003) # => "1003rd"
264
+ def ordinalize(number)
265
+ if (11..13).include?(number.to_i % 100)
266
+ "#{number}th"
267
+ else
268
+ case number.to_i % 10
269
+ when 1: "#{number}st"
270
+ when 2: "#{number}nd"
271
+ when 3: "#{number}rd"
272
+ else "#{number}th"
273
+ end
274
+ end
275
+ end
276
+ end
277
+ end
278
+
279
+ require File.dirname(__FILE__) + '/inflections'
@@ -0,0 +1,3 @@
1
+ require 'active_couch/views/errors'
2
+ require 'active_couch/views/view'
3
+ require 'active_couch/views/raw_view'
@@ -0,0 +1,4 @@
1
+ module ActiveCouch
2
+ class ViewError < StandardError; end
3
+ class InvalidFilter < ViewError; end
4
+ end
@@ -0,0 +1,40 @@
1
+ require 'json'
2
+
3
+ module ActiveCouch
4
+ class RawView
5
+ class << self # Class Methods
6
+ # Variables will store Javascript functions which represent
7
+ # the map/reduce functions respectively
8
+ @map_function, @reduce_function, @database = nil, nil, nil
9
+ attr_reader :database
10
+
11
+ def map(map_function)
12
+ @map_function = map_function
13
+ end
14
+
15
+ def reduce(reduce_function)
16
+ @reduce_function = reduce_function
17
+ end
18
+
19
+ def for_database(database)
20
+ @database = database
21
+ end
22
+
23
+ def to_json(existing_view = {})
24
+ results_hash = { '_id' => "_design/#{name}", 'language' => 'javascript' }
25
+ results_hash.merge!(existing_view)
26
+ results_hash['views'] = {
27
+ "#{name}" => { 'map' => @map_function }
28
+ }
29
+ results_hash['views']["#{name}"]['reduce'] = @reduce_function unless @reduce_function.nil?
30
+ # Convert to JSON
31
+ results_hash.to_json
32
+ end
33
+
34
+ def name
35
+ "#{self}".underscore
36
+ end
37
+
38
+ end # End Class Methods
39
+ end # End Class RawView
40
+ end # End module ActiveCouch
@@ -0,0 +1,85 @@
1
+ require 'json'
2
+
3
+ module ActiveCouch
4
+ class View
5
+ class << self # Class Methods
6
+ # Class instance variables
7
+ @name = nil; @database = nil
8
+ # These are accessible only at class-scope
9
+ attr_accessor :name, :database
10
+ # Set the view name and database name in the define method and then execute
11
+ # the block
12
+ def define(*args)
13
+ # Borrowed from ActiveRecord::Base.find
14
+ first = args.slice!(0); second = args.slice!(0)
15
+ # Based on the classes of the arguments passed, set instance variables
16
+ case first.class.to_s
17
+ when 'String', 'Symbol' then name = first.to_s; options = second || {}
18
+ when 'Hash' then name = ''; options = first
19
+ else raise ArgumentError, "Wrong arguments used to define the view"
20
+ end
21
+ # Define the view and database instance variables based on the args passed
22
+ # Don't care if the key doesn't exist
23
+ @name, @database = get_name(name), options[:for_db]
24
+ # Block being called to set other parameters for the Migration
25
+ yield if block_given?
26
+ end
27
+
28
+ def with_key(key = "")
29
+ @key = key unless key.nil?
30
+ end
31
+
32
+ def with_filter(filter = "")
33
+ @filter = filter unless filter.nil?
34
+ end
35
+
36
+ def include_attributes(*attrs)
37
+ @attrs = attrs unless attrs.nil? || !attrs.is_a?(Array)
38
+ end
39
+
40
+ def to_json(existing_view = {})
41
+ results_hash = { "_id" => "_design/#{@name}", "language" => view_language }
42
+ results_hash.merge!(existing_view)
43
+ results_hash['views'] = view_function
44
+ # Returns the JSON format for the function
45
+ results_hash.to_json
46
+ end
47
+
48
+ private
49
+ def view_language
50
+ 'javascript'
51
+ end
52
+
53
+ def include_attrs
54
+ attrs = "doc"
55
+ unless @attrs.nil?
56
+ js = @attrs.inject([]) {|result, att| result << "#{att}: doc.#{att}"}
57
+ attrs = "{#{js.join(' , ')}}" if js.size > 0
58
+ end
59
+ attrs
60
+ end
61
+
62
+ def get_name(name)
63
+ view_name = name
64
+ view_name = "#{self}".underscore if name.nil? || name.length == 0
65
+ view_name
66
+ end
67
+
68
+ def view_function
69
+ filter_present = !@filter.nil? && @filter.length > 0
70
+
71
+ js = "function(doc) { "
72
+ js << "if(#{@filter}) { " if filter_present
73
+ js << "emit(#{couchdb_view_mapper}, #{include_attrs});"
74
+ js << " } " if filter_present
75
+ js << " }"
76
+
77
+ { @name => {'map' => js} }
78
+ end
79
+
80
+ def couchdb_view_mapper
81
+ @key.nil? ? 'null' : "doc.#{@key}"
82
+ end
83
+ end # End Class Methods
84
+ end # End Class Migration
85
+ end # End module ActiveCouch