simply_couch 0.1.0

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 (45) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +182 -0
  3. data/LICENSE.txt +15 -0
  4. data/README.md +294 -0
  5. data/lib/core_ext/date.rb +15 -0
  6. data/lib/core_ext/time.rb +23 -0
  7. data/lib/simply_couch/class_methods_base.rb +72 -0
  8. data/lib/simply_couch/has_attachment.rb +225 -0
  9. data/lib/simply_couch/include_relation.rb +160 -0
  10. data/lib/simply_couch/instance_methods.rb +356 -0
  11. data/lib/simply_couch/locale/en.yml +5 -0
  12. data/lib/simply_couch/model/ancestry.rb +307 -0
  13. data/lib/simply_couch/model/association_property.rb +26 -0
  14. data/lib/simply_couch/model/attachments.rb +90 -0
  15. data/lib/simply_couch/model/belongs_to.rb +140 -0
  16. data/lib/simply_couch/model/database.rb +209 -0
  17. data/lib/simply_couch/model/embedded_in.rb +196 -0
  18. data/lib/simply_couch/model/find_by.rb +202 -0
  19. data/lib/simply_couch/model/finders.rb +77 -0
  20. data/lib/simply_couch/model/has_and_belongs_to_many.rb +223 -0
  21. data/lib/simply_couch/model/has_many.rb +177 -0
  22. data/lib/simply_couch/model/has_many_embedded.rb +187 -0
  23. data/lib/simply_couch/model/has_one.rb +75 -0
  24. data/lib/simply_couch/model/pagination.rb +25 -0
  25. data/lib/simply_couch/model/pagination_options.rb +55 -0
  26. data/lib/simply_couch/model/persistence.rb +411 -0
  27. data/lib/simply_couch/model/properties.rb +11 -0
  28. data/lib/simply_couch/model/validations.rb +28 -0
  29. data/lib/simply_couch/model/view/base_view_spec.rb +115 -0
  30. data/lib/simply_couch/model/view/custom_view_spec.rb +49 -0
  31. data/lib/simply_couch/model/view/custom_views.rb +50 -0
  32. data/lib/simply_couch/model/view/lists.rb +25 -0
  33. data/lib/simply_couch/model/view/model_view_spec.rb +106 -0
  34. data/lib/simply_couch/model/view/properties_view_spec.rb +53 -0
  35. data/lib/simply_couch/model/view/raw_view_spec.rb +30 -0
  36. data/lib/simply_couch/model/view/view_query.rb +98 -0
  37. data/lib/simply_couch/model/view.rb +8 -0
  38. data/lib/simply_couch/model/views/array_property_view_spec.rb +26 -0
  39. data/lib/simply_couch/model/views/deleted_model_view_spec.rb +43 -0
  40. data/lib/simply_couch/model/views.rb +2 -0
  41. data/lib/simply_couch/model.rb +195 -0
  42. data/lib/simply_couch/rake.rb +23 -0
  43. data/lib/simply_couch/storage.rb +147 -0
  44. data/lib/simply_couch.rb +26 -0
  45. metadata +144 -0
@@ -0,0 +1,147 @@
1
+ module SimplyCouch
2
+ module Storage
3
+ module InstanceMethods
4
+ def _s3_options
5
+ self.class._s3_options
6
+ end
7
+
8
+ def s3_connection(name)
9
+ @_s3_connection ||= RightAws::S3.new(_s3_options[name][:access_key], _s3_options[name][:secret_access_key], :multi_thread => true, :ca_file => _s3_options[name][:ca_file], :logger => _s3_options[name][:logger])
10
+ end
11
+
12
+ def s3_bucket(name)
13
+ if !@_s3_bucket
14
+ @_s3_bucket = s3_connection(name).bucket(_s3_options[name][:bucket])
15
+ location = (_s3_options[name][:location] == :eu) ? :eu : nil
16
+ @_s3_bucket = s3_connection(name).bucket(_s3_options[name][:bucket], true, _s3_options[name][:permissions], :location => location) if @_s3_bucket.nil?
17
+ end
18
+ @_s3_bucket
19
+ rescue Exception => e
20
+ raise ArgumentError, "Could not access/create S3 bucket '#{name}': #{e} #{e.backtrace.join("\n")}"
21
+ end
22
+
23
+ def save(validate = true)
24
+ update_attachment_sizes
25
+ if ret = super(validate)
26
+ save_attachments
27
+ end
28
+ ret
29
+ end
30
+
31
+ def save!(*args)
32
+ update_attachment_sizes
33
+ super
34
+ save_attachments
35
+ end
36
+
37
+ def delete(*args)
38
+ delete_attachments
39
+ super
40
+ end
41
+
42
+ def destroy(*args)
43
+ delete_attachments
44
+ super
45
+ end
46
+
47
+ def save_attachments
48
+ return unless id.present?
49
+ if @_s3_attachments
50
+ @_s3_attachments.each do |name, attachment|
51
+ if attachment[:dirty]
52
+ value = attachment[:value].is_a?(String) ? attachment[:value] : attachment[:value].to_json
53
+ s3_bucket(name).put(s3_attachment_key(name), value, {}, _s3_options[name][:permissions])
54
+ attachment[:dirty] = false
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ def delete_attachments
61
+ return unless id.present?
62
+ (@_s3_attachments || {}).each do |name, attachment|
63
+ if _s3_options[name][:after_delete] == :delete
64
+ key = s3_bucket(name).key(s3_attachment_key(name), true)
65
+ key.delete
66
+ end
67
+ end
68
+ end
69
+
70
+ def update_attachment_sizes
71
+ if @_s3_attachments
72
+ @_s3_attachments.each do |name, attachment|
73
+ if attachment[:dirty]
74
+ value = attachment[:value].is_a?(String) ? attachment[:value] : attachment[:value].to_json
75
+ send("#{name}_size=", (value.size rescue nil))
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ def s3_attachment_key(name)
82
+ "#{self.class.name.tableize}/#{name}/#{id}"
83
+ end
84
+ end
85
+
86
+ module ClassMethods
87
+ def has_s3_attachment(name, options = {})
88
+ require 'right_aws' rescue nil
89
+
90
+ name = name.to_sym
91
+
92
+ self.class.instance_eval do
93
+ attr_accessor :_s3_options
94
+ end
95
+
96
+ self.class_eval do
97
+ if respond_to?(:property)
98
+ property "#{name}_size"
99
+ else
100
+ simpledb_integer "#{name}_size"
101
+ end
102
+ end
103
+
104
+ raise ArgumentError, "No bucket name specified for attachment #{name}" if options[:bucket].blank?
105
+ options = {
106
+ :permissions => 'private',
107
+ :ssl => true,
108
+ :location => :us, # use :eu for European buckets
109
+ :ca_file => nil, # point to CA file for SSL certificate verification
110
+ :after_delete => :nothing, # or :delete to delete the item on S3 after it is deleted in the DB,
111
+ :logger => nil # use the default RightAws logger (stdout)
112
+ }.update(options)
113
+ self._s3_options ||= {}
114
+ self._s3_options[name] = options
115
+
116
+ define_attachment_accessors(name)
117
+ attr_reader :_s3_attachments
118
+ include InstanceMethods
119
+ end
120
+
121
+ def define_attachment_accessors(name)
122
+ define_method(name) do
123
+ unless @_s3_attachments and @_s3_attachments[name]
124
+ @_s3_attachments = {name => {}}
125
+ @_s3_attachments[name][:value] = s3_bucket(name).get(s3_attachment_key(name))
126
+ end
127
+ @_s3_attachments[name][:value]
128
+ end
129
+
130
+ define_method("#{name}=") do |value|
131
+ @_s3_attachments ||= {}
132
+ @_s3_attachments[name] ||= {}
133
+ @_s3_attachments[name].update(:value => value, :dirty => true)
134
+ value
135
+ end
136
+
137
+ define_method("#{name}_url") do
138
+ if _s3_options[name][:permissions] == 'private'
139
+ RightAws::S3Generator.new(_s3_options[name][:access_key], _s3_options[name][:secret_access_key], :multi_thread => true, :ca_file => _s3_options[name][:ca_file]).bucket(_s3_options[name][:bucket]).get(s3_attachment_key(name), 5.minutes)
140
+ else
141
+ "http://#{_s3_options[name][:bucket].to_s}.s3.amazonaws.com/#{s3_attachment_key(name)}"
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,26 @@
1
+ # Please move me to a proper location
2
+ class String
3
+ def property_name
4
+ underscore.gsub('/','__').gsub('::','__')
5
+ end
6
+ end
7
+
8
+ unless defined?(SimplyCouch)
9
+ $:<<(File.expand_path(File.dirname(__FILE__) + "/lib"))
10
+ require 'simply_couch/instance_methods'
11
+ require 'simply_couch/storage'
12
+ require 'simply_couch/class_methods_base'
13
+
14
+ module SimplyCouch
15
+ VERSION = '1.0.0'
16
+ class Error < RuntimeError; end
17
+ class RecordNotFound < RuntimeError; end
18
+ class NotImplementedError < RuntimeError; end
19
+ class ModelNotInstantiatedError < RuntimeError; end
20
+ end
21
+
22
+ require 'simply_couch/model'
23
+ require 'core_ext/time'
24
+ require 'core_ext/date'
25
+ end
26
+ class SimplyCouch::Conflict < StandardError; end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simply_couch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin ter Kuile
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: activemodel
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '6.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - ">="
24
+ - !ruby/object:Gem::Version
25
+ version: '6.0'
26
+ - !ruby/object:Gem::Dependency
27
+ name: activesupport
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: rspec
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '3.0'
54
+ - !ruby/object:Gem::Dependency
55
+ name: couchrest
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ description: CouchDB ORM with ActiveModel compliance, associations, validations, callbacks,
69
+ views, and soft delete. No driver dependency — the host app brings its own CouchDB
70
+ client.
71
+ email: bterkuile@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - CHANGELOG.md
77
+ - LICENSE.txt
78
+ - README.md
79
+ - lib/core_ext/date.rb
80
+ - lib/core_ext/time.rb
81
+ - lib/simply_couch.rb
82
+ - lib/simply_couch/class_methods_base.rb
83
+ - lib/simply_couch/has_attachment.rb
84
+ - lib/simply_couch/include_relation.rb
85
+ - lib/simply_couch/instance_methods.rb
86
+ - lib/simply_couch/locale/en.yml
87
+ - lib/simply_couch/model.rb
88
+ - lib/simply_couch/model/ancestry.rb
89
+ - lib/simply_couch/model/association_property.rb
90
+ - lib/simply_couch/model/attachments.rb
91
+ - lib/simply_couch/model/belongs_to.rb
92
+ - lib/simply_couch/model/database.rb
93
+ - lib/simply_couch/model/embedded_in.rb
94
+ - lib/simply_couch/model/find_by.rb
95
+ - lib/simply_couch/model/finders.rb
96
+ - lib/simply_couch/model/has_and_belongs_to_many.rb
97
+ - lib/simply_couch/model/has_many.rb
98
+ - lib/simply_couch/model/has_many_embedded.rb
99
+ - lib/simply_couch/model/has_one.rb
100
+ - lib/simply_couch/model/pagination.rb
101
+ - lib/simply_couch/model/pagination_options.rb
102
+ - lib/simply_couch/model/persistence.rb
103
+ - lib/simply_couch/model/properties.rb
104
+ - lib/simply_couch/model/validations.rb
105
+ - lib/simply_couch/model/view.rb
106
+ - lib/simply_couch/model/view/base_view_spec.rb
107
+ - lib/simply_couch/model/view/custom_view_spec.rb
108
+ - lib/simply_couch/model/view/custom_views.rb
109
+ - lib/simply_couch/model/view/lists.rb
110
+ - lib/simply_couch/model/view/model_view_spec.rb
111
+ - lib/simply_couch/model/view/properties_view_spec.rb
112
+ - lib/simply_couch/model/view/raw_view_spec.rb
113
+ - lib/simply_couch/model/view/view_query.rb
114
+ - lib/simply_couch/model/views.rb
115
+ - lib/simply_couch/model/views/array_property_view_spec.rb
116
+ - lib/simply_couch/model/views/deleted_model_view_spec.rb
117
+ - lib/simply_couch/rake.rb
118
+ - lib/simply_couch/storage.rb
119
+ homepage: https://github.com/bterkuile/simply_couch
120
+ licenses:
121
+ - MIT
122
+ metadata:
123
+ source_code_uri: https://github.com/bterkuile/simply_couch
124
+ bug_tracker_uri: https://github.com/bterkuile/simply_couch/issues
125
+ changelog_uri: https://github.com/bterkuile/simply_couch/blob/main/CHANGELOG.md
126
+ rubygems_mfa_required: 'true'
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '3.1'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubygems_version: 3.7.2
142
+ specification_version: 4
143
+ summary: Simple CouchDB ORM for Rails
144
+ test_files: []