dolly 1.1.7 → 3.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +78 -0
  3. data/lib/dolly/attachment.rb +29 -0
  4. data/lib/dolly/bulk_document.rb +27 -26
  5. data/lib/dolly/class_methods_delegation.rb +15 -0
  6. data/lib/dolly/collection.rb +32 -65
  7. data/lib/dolly/configuration.rb +35 -10
  8. data/lib/dolly/connection.rb +93 -22
  9. data/lib/dolly/depracated_database.rb +24 -0
  10. data/lib/dolly/document.rb +61 -208
  11. data/lib/dolly/document_creation.rb +27 -0
  12. data/lib/dolly/document_state.rb +66 -0
  13. data/lib/dolly/document_type.rb +47 -0
  14. data/lib/dolly/exceptions.rb +32 -0
  15. data/lib/dolly/framework_helper.rb +7 -0
  16. data/lib/dolly/identity_properties.rb +29 -0
  17. data/lib/dolly/mango.rb +156 -0
  18. data/lib/dolly/mango_index.rb +73 -0
  19. data/lib/dolly/properties.rb +36 -0
  20. data/lib/dolly/property.rb +76 -46
  21. data/lib/dolly/property_manager.rb +53 -0
  22. data/lib/dolly/property_set.rb +23 -0
  23. data/lib/dolly/query.rb +63 -75
  24. data/lib/dolly/query_arguments.rb +35 -0
  25. data/lib/dolly/request.rb +12 -107
  26. data/lib/dolly/request_header.rb +26 -0
  27. data/lib/dolly/timestamp.rb +24 -0
  28. data/lib/dolly/version.rb +1 -1
  29. data/lib/dolly/view_query.rb +21 -0
  30. data/lib/dolly.rb +2 -23
  31. data/lib/{dolly → railties}/railtie.rb +2 -1
  32. data/lib/refinements/hash_refinements.rb +27 -0
  33. data/lib/refinements/string_refinements.rb +28 -0
  34. data/lib/tasks/db.rake +27 -4
  35. data/test/bulk_document_test.rb +8 -5
  36. data/test/document_test.rb +130 -95
  37. data/test/document_type_test.rb +28 -0
  38. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/test/dummy/log/test.log +46417 -46858
  40. data/test/inheritance_test.rb +23 -0
  41. data/test/mango_index_test.rb +64 -0
  42. data/test/mango_test.rb +273 -0
  43. data/test/property_manager_test.rb +18 -0
  44. data/test/test_helper.rb +63 -18
  45. data/test/view_query_test.rb +27 -0
  46. metadata +67 -140
  47. data/Rakefile +0 -11
  48. data/lib/dolly/bulk_error.rb +0 -16
  49. data/lib/dolly/db_config.rb +0 -20
  50. data/lib/dolly/interpreter.rb +0 -5
  51. data/lib/dolly/logger.rb +0 -9
  52. data/lib/dolly/name_space.rb +0 -28
  53. data/lib/dolly/timestamps.rb +0 -21
  54. data/lib/exceptions/dolly.rb +0 -47
  55. data/test/collection_test.rb +0 -59
  56. data/test/configuration_test.rb +0 -9
  57. data/test/dummy/README.rdoc +0 -28
  58. data/test/dummy/Rakefile +0 -6
  59. data/test/dummy/app/assets/javascripts/application.js +0 -13
  60. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  61. data/test/dummy/app/controllers/application_controller.rb +0 -5
  62. data/test/dummy/app/helpers/application_helper.rb +0 -2
  63. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  64. data/test/dummy/bin/bundle +0 -3
  65. data/test/dummy/bin/rails +0 -4
  66. data/test/dummy/bin/rake +0 -4
  67. data/test/dummy/config/application.rb +0 -27
  68. data/test/dummy/config/boot.rb +0 -5
  69. data/test/dummy/config/couchdb.yml +0 -13
  70. data/test/dummy/config/environment.rb +0 -5
  71. data/test/dummy/config/environments/development.rb +0 -29
  72. data/test/dummy/config/environments/production.rb +0 -80
  73. data/test/dummy/config/environments/test.rb +0 -36
  74. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  75. data/test/dummy/config/initializers/inflections.rb +0 -16
  76. data/test/dummy/config/initializers/mime_types.rb +0 -5
  77. data/test/dummy/config/initializers/secret_token.rb +0 -12
  78. data/test/dummy/config/initializers/session_store.rb +0 -3
  79. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  80. data/test/dummy/config/locales/en.yml +0 -23
  81. data/test/dummy/config/routes.rb +0 -56
  82. data/test/dummy/config.ru +0 -4
  83. data/test/dummy/lib/couch_rest_adapter/railtie.rb +0 -10
  84. data/test/dummy/public/404.html +0 -58
  85. data/test/dummy/public/422.html +0 -58
  86. data/test/dummy/public/500.html +0 -57
  87. data/test/dummy/public/favicon.ico +0 -0
  88. data/test/factories/factories.rb +0 -8
  89. data/test/request_test.rb +0 -25
@@ -0,0 +1,24 @@
1
+ module Dolly
2
+ module Timestamp
3
+ def write_timestamps(is_persisted)
4
+ return unless timestamped?
5
+ write_attribute(:created_at, Time.now) unless is_persisted
6
+ write_attribute(:updated_at, Time.now)
7
+ end
8
+
9
+ def timestamped?
10
+ respond_to?(:created_at) && respond_to?(:updated_at)
11
+ end
12
+
13
+ def self.included(base)
14
+ base.extend(ClassMethods)
15
+ end
16
+
17
+ module ClassMethods
18
+ def timestamps!
19
+ property :created_at, :updated_at, class_name: Time
20
+ end
21
+ end
22
+ end
23
+ end
24
+
data/lib/dolly/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Dolly
2
- VERSION = "1.1.7"
2
+ VERSION = "3.1.1"
3
3
  end
@@ -0,0 +1,21 @@
1
+ require 'dolly/class_methods_delegation'
2
+
3
+ module Dolly
4
+ module ViewQuery
5
+ def raw_view(design, view_name, opts = {})
6
+ design = "_design/#{design}/_view/#{view_name}"
7
+ connection.view(design, opts)
8
+ end
9
+
10
+ def view_value(doc, view_name, opts = {})
11
+ raw_view(doc, view_name, opts)[:rows].flat_map { |result| result[:value] }
12
+ end
13
+
14
+ def collection_view(design, view_name, opts = {})
15
+ opts.delete(:include_docs)
16
+ design = "_design/#{design}/_view/#{view_name}"
17
+ response = connection.view(design, opts)
18
+ Dolly::Collection.new(rows: response, options: opts)
19
+ end
20
+ end
21
+ end
data/lib/dolly.rb CHANGED
@@ -1,28 +1,7 @@
1
1
  require "dolly/version"
2
2
  require "dolly/document"
3
- require "dolly/configuration"
4
- require 'dolly/railtie' if defined?(Rails)
3
+ require 'dolly/mango_index'
4
+ require 'railties/railtie' if defined?(Rails)
5
5
 
6
6
  module Dolly
7
- class << self
8
- def configure
9
- yield config
10
- end
11
-
12
- def config
13
- @config ||= Configuration.new
14
- end
15
-
16
- def reset!
17
- @config = Configuration.new
18
- end
19
-
20
- def log_requests?
21
- !!config.log_requests
22
- end
23
-
24
- def logger
25
- @logger ||= config.logger
26
- end
27
- end
28
7
  end
@@ -1,4 +1,4 @@
1
- require 'rails'
1
+ require 'rails/railtie'
2
2
 
3
3
  module Dolly
4
4
  class Railtie < Rails::Railtie
@@ -10,3 +10,4 @@ module Dolly
10
10
  end
11
11
  end
12
12
 
13
+
@@ -0,0 +1,27 @@
1
+ module HashRefinements
2
+ refine Hash do
3
+ # File activesupport/lib/active_support/core_ext/hash/keys.rb, line 82
4
+ def deep_transform_keys(&block)
5
+ _deep_transform_keys_in_object(self, &block)
6
+ end
7
+
8
+ def slice(*keys)
9
+ keys.each_with_object(Hash.new) { |k, hash| hash[k] = self[k] if has_key?(k) }
10
+ end
11
+
12
+ private
13
+
14
+ def _deep_transform_keys_in_object(object, &block)
15
+ case object
16
+ when Hash
17
+ object.each_with_object({}) do |(key, value), result|
18
+ result[yield(key)] = _deep_transform_keys_in_object(value, &block)
19
+ end
20
+ when Array
21
+ object.map { |e| _deep_transform_keys_in_object(e, &block) }
22
+ else
23
+ object
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ module StringRefinements
2
+ UNESCAPABLE_PATTERNS = [
3
+ %r{_design/.+/_view/.+}
4
+ ]
5
+
6
+ refine String do
7
+ #FROM ActiveModel::Name
8
+ def underscore
9
+ to_s.gsub(/::/, '/').
10
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
11
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
12
+ tr("-", "_").
13
+ downcase
14
+ end
15
+
16
+ def cgi_escape
17
+ return if nil?
18
+ return self unless escapable?
19
+ CGI.escape self
20
+ end
21
+
22
+ def escapable?
23
+ UNESCAPABLE_PATTERNS.none? do |pattern|
24
+ self =~ pattern
25
+ end
26
+ end
27
+ end
28
+ end
data/lib/tasks/db.rake CHANGED
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  namespace :db do
2
4
  desc "Will create if missing database and add default views"
3
5
  task setup: :environment do
4
- Dolly::Document.database.put "", nil
6
+ Dolly::Document.connection.put '', {}
5
7
  puts "Database created"
6
8
  end
7
9
 
@@ -38,9 +40,9 @@ namespace :db do
38
40
  view_doc.merge!( '_id' => design_doc_name, 'language' => 'coffeescript')
39
41
 
40
42
  begin
41
- hash_doc = JSON::parse Dolly::Document.database.get(view_doc["_id"]).parsed_response
43
+ hash_doc = Dolly::Document.connection.request(:get, view_doc["_id"])
42
44
 
43
- rev = hash_doc.delete('_rev')
45
+ rev = hash_doc.delete(:_rev)
44
46
 
45
47
  if hash_doc == view_doc
46
48
  puts 'everything up to date'
@@ -55,8 +57,29 @@ namespace :db do
55
57
  will_save = true
56
58
  end
57
59
 
58
- Dolly::Document.database.put design_doc_name, view_doc.to_json if will_save
60
+ Dolly::Document.connection.request :put, design_doc_name, view_doc if will_save
59
61
  end
60
62
  end
61
63
 
64
+ namespace :index do
65
+ desc 'Creates indexes for mango querys located in db/indexes/*.json'
66
+ task create: :environment do
67
+ indexes_dir = Rails.root.join('db', 'indexes')
68
+ files = Dir.glob File.join(indexes_dir, '**', '*.json')
69
+
70
+ files.each do |file|
71
+ index_data = JSON.parse(File.read(file))
72
+ database = index_data.fetch('db', 'default').to_sym
73
+ puts "*" * 100
74
+ puts "Creating index: #{index_data["name"]} for database: #{database}"
75
+
76
+ if database == Dolly::Connection::DEFAULT_DATABASE
77
+ puts Dolly::MangoIndex.create(index_data['name'], index_data['fields'])
78
+ else
79
+ puts Dolly::MangoIndex.create_in_database(database, index_data['name'], index_data['fields'])
80
+ end
81
+ end
82
+ end
83
+ end
62
84
  end
85
+
@@ -4,8 +4,7 @@ class Doc < Dolly::Document
4
4
  property :name
5
5
  end
6
6
 
7
- class BulkDocumentTest < ActiveSupport::TestCase
8
-
7
+ class BulkDocumentTest < Test::Unit::TestCase
9
8
  setup do
10
9
  @doc = Dolly::Document.bulk_document
11
10
  @req = "http://localhost:5984/test/_bulk_docs"
@@ -28,17 +27,21 @@ class BulkDocumentTest < ActiveSupport::TestCase
28
27
  test 'save document will remove docs from payload' do
29
28
  docs = 3.times.map{ Doc.new name: "a" }
30
29
  docs.each do |d|
31
- d.doc['_id'] = "foo_bar/#{SecureRandom.uuid}"
30
+ d.id = "doc/#{SecureRandom.uuid}"
32
31
  @doc << d
33
32
  end
34
33
 
35
34
  res = docs.map{|d| {ok: true, id: d.id, rev: "2-#{SecureRandom.uuid}"} }.to_json
36
- FakeWeb.register_uri :post, @req, body: res
35
+
36
+ stub_request(:post, @req).
37
+ to_return(body: res)
38
+
39
+ assert_equal docs, @doc.payload[:docs]
37
40
 
38
41
  @doc.save
39
42
 
40
43
  assert_equal [], @doc.errors
41
44
  assert_equal [], @doc.docs
45
+ assert_equal [], @doc.payload[:docs]
42
46
  end
43
-
44
47
  end