firestore-odm 0.1.0 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46f4499f36b15866c173936a87f131a1dcd577a910ae756e639e8bb813f40d37
4
- data.tar.gz: aa51288692a8f243edaef885845094f432430a235d76f3e099adbd4da2042894
3
+ metadata.gz: 94b90e6ec000daec07e84a3772c145ff8e6db882205e0f384c022ac51bc8d520
4
+ data.tar.gz: b8b2a75bac4d646ef6dace0502bf9dc80a160b5f6892e5c226aa20a9e6ba130f
5
5
  SHA512:
6
- metadata.gz: e0357329bb07784d50c21c314c6060bd3aef22b6d09021c18e321fb7322274de2f6f945d65a199df80765207109ddbefec3ade2c21e1781b8fb204497519d731
7
- data.tar.gz: 3092565a2150f89088e53ab8b081f797da1c2c99937170f798d8b472a9fa459af79ac46a9c20bbd347713c7572ca436a2f4d41278601cd0d40d5db5dd3ce48bc
6
+ metadata.gz: a0f9405724131206b7d0c2e379a0473bd846ec28ed4edcab94bfe7811cc7d9bf052118707779e4ee4537667b777b22a69188fde0ba28d1ca4d66262dd27b33e1
7
+ data.tar.gz: 20777dcbd8f983af757e9e487ecea5684775aa4722e805998a9cb012a45b0dc01c660e0c7bb7e7a1c297cee7d6334d4e4f4420d8959cb8d2665bf727567a4871
@@ -0,0 +1,4 @@
1
+ .DS_Store
2
+
3
+ *.json
4
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,118 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ firestore-odm (0.1.4)
5
+ google-cloud-firestore (~> 1.4, >= 1.4.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ ast (2.4.0)
13
+ backport (1.1.2)
14
+ benchmark (0.1.0)
15
+ concurrent-ruby (1.1.6)
16
+ e2mmap (0.1.0)
17
+ faraday (1.0.1)
18
+ multipart-post (>= 1.2, < 3)
19
+ google-cloud-core (1.5.0)
20
+ google-cloud-env (~> 1.0)
21
+ google-cloud-errors (~> 1.0)
22
+ google-cloud-env (1.3.2)
23
+ faraday (>= 0.17.3, < 2.0)
24
+ google-cloud-errors (1.0.1)
25
+ google-cloud-firestore (1.4.4)
26
+ concurrent-ruby (~> 1.0)
27
+ google-cloud-core (~> 1.2)
28
+ google-gax (~> 1.8)
29
+ googleapis-common-protos (>= 1.3.9, < 2.0)
30
+ googleapis-common-protos-types (>= 1.0.4, < 2.0)
31
+ rbtree (~> 0.4.2)
32
+ google-gax (1.8.1)
33
+ google-protobuf (~> 3.9)
34
+ googleapis-common-protos (>= 1.3.9, < 2.0)
35
+ googleauth (~> 0.9)
36
+ grpc (~> 1.24)
37
+ rly (~> 0.2.3)
38
+ google-protobuf (3.12.2)
39
+ googleapis-common-protos (1.3.10)
40
+ google-protobuf (~> 3.11)
41
+ googleapis-common-protos-types (>= 1.0.5, < 2.0)
42
+ grpc (~> 1.27)
43
+ googleapis-common-protos-types (1.0.5)
44
+ google-protobuf (~> 3.11)
45
+ googleauth (0.13.0)
46
+ faraday (>= 0.17.3, < 2.0)
47
+ jwt (>= 1.4, < 3.0)
48
+ memoist (~> 0.16)
49
+ multi_json (~> 1.11)
50
+ os (>= 0.9, < 2.0)
51
+ signet (~> 0.14)
52
+ grpc (1.28.0)
53
+ google-protobuf (~> 3.11)
54
+ googleapis-common-protos-types (~> 1.0)
55
+ jaro_winkler (1.5.4)
56
+ jwt (2.2.1)
57
+ maruku (0.7.3)
58
+ memoist (0.16.2)
59
+ mini_portile2 (2.4.0)
60
+ multi_json (1.14.1)
61
+ multipart-post (2.1.1)
62
+ nokogiri (1.10.9)
63
+ mini_portile2 (~> 2.4.0)
64
+ os (1.1.0)
65
+ parallel (1.19.1)
66
+ parser (2.7.1.2)
67
+ ast (~> 2.4.0)
68
+ public_suffix (4.0.5)
69
+ rainbow (3.0.0)
70
+ rbtree (0.4.2)
71
+ reverse_markdown (1.4.0)
72
+ nokogiri
73
+ rexml (3.2.4)
74
+ rly (0.2.3)
75
+ rubocop (0.84.0)
76
+ parallel (~> 1.10)
77
+ parser (>= 2.7.0.1)
78
+ rainbow (>= 2.2.2, < 4.0)
79
+ rexml
80
+ rubocop-ast (>= 0.0.3)
81
+ ruby-progressbar (~> 1.7)
82
+ unicode-display_width (>= 1.4.0, < 2.0)
83
+ rubocop-ast (0.0.3)
84
+ parser (>= 2.7.0.1)
85
+ ruby-progressbar (1.10.1)
86
+ signet (0.14.0)
87
+ addressable (~> 2.3)
88
+ faraday (>= 0.17.3, < 2.0)
89
+ jwt (>= 1.5, < 3.0)
90
+ multi_json (~> 1.10)
91
+ solargraph (0.39.8)
92
+ backport (~> 1.1)
93
+ benchmark
94
+ bundler (>= 1.17.2)
95
+ e2mmap
96
+ jaro_winkler (~> 1.5)
97
+ maruku (~> 0.7, >= 0.7.3)
98
+ nokogiri (~> 1.9, >= 1.9.1)
99
+ parser (~> 2.3)
100
+ reverse_markdown (>= 1.0.5, < 3)
101
+ rubocop (~> 0.52)
102
+ thor (~> 1.0)
103
+ tilt (~> 2.0)
104
+ yard (~> 0.9, >= 0.9.24)
105
+ thor (1.0.1)
106
+ tilt (2.0.10)
107
+ unicode-display_width (1.7.0)
108
+ yard (0.9.25)
109
+
110
+ PLATFORMS
111
+ ruby
112
+
113
+ DEPENDENCIES
114
+ firestore-odm!
115
+ solargraph
116
+
117
+ BUNDLED WITH
118
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,25 @@
1
+ BSD 2-Clause License
2
+
3
+ Copyright (c) 2020, Felipe Cabrera
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ * Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,33 @@
1
+ # Firestore ODM
2
+
3
+ ## Configure
4
+ Add `gem 'firestore-odm'` to your Gemfile, or from the terminal
5
+ ```
6
+ gem install firestore-odm
7
+ ```
8
+
9
+ Then type
10
+ ```
11
+ export GOOGLE_CLOUD_CREDENTIALS=<path_to_google_cloud_credentials>
12
+ ```
13
+
14
+ ## How to use
15
+
16
+ ```ruby
17
+ require 'firestore-odm'
18
+
19
+ class Account < FirestoreODM::Model
20
+ path :accounts
21
+
22
+ field :name, String
23
+ field :email, String
24
+ end
25
+
26
+ account = Account.create do |doc|
27
+ doc.name = 'John Smith'
28
+ doc.email = 'john.smith@example.com'
29
+ end
30
+
31
+ puts account.path #=> accounts/<document_id>
32
+ puts account.to_json #=> {"name": "John Smith", "email": "john.smith@example.com"}
33
+ ```
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = 'firestore-odm'
3
+ spec.version = '0.1.5'
4
+ spec.licenses = 'BSD-2-Clause'
5
+ spec.summary = 'ODM for Firestore in Ruby'
6
+ spec.description = ''
7
+ spec.author = 'Felipe Cabrera'
8
+ spec.email = 'fecabrera0@outlook.com'
9
+ spec.files = `git ls-files`.split("\n")
10
+ spec.homepage = 'https://github.com/fecabrera/firestore-odm'
11
+ spec.metadata = {"source_code_uri" => "https://github.com/fecabrera/firestore-odm"}
12
+
13
+ spec.add_dependency 'google-cloud-firestore', '~> 1.4', '>= 1.4.0'
14
+ end
@@ -14,4 +14,80 @@ class Module
14
14
  def define_setter_method name, &block
15
15
  define_method("#{name}=", &block)
16
16
  end
17
+ end
18
+
19
+ module FirestoreODM
20
+ attr_reader :conversions, :options
21
+
22
+ @conversions = {
23
+ String => :to_s,
24
+ Integer => :to_i,
25
+ Float => :to_f,
26
+ Date => :to_date,
27
+ DateTime => :to_datetime,
28
+ Time => :to_time
29
+ }
30
+
31
+ @options = {}
32
+
33
+ # Sets a conversion rule for a certain type.
34
+ # @param type [Class]
35
+ # @param target [Symbol, Proc]
36
+ def add_conversion type, target
37
+ @conversions[type] = target
38
+ return
39
+ end
40
+
41
+ # Gets a conversion.
42
+ # @param type [Class]
43
+ # @return [Proc]
44
+ def get_conversion type
45
+ case rule = conversions[type]
46
+ when Symbol
47
+ Proc.new { |value| value.method(rule).call }
48
+ when Proc
49
+ rule
50
+ else
51
+ raise "Conversion not found for type `#{type}`"
52
+ end
53
+ end
54
+
55
+ # @param name [Symbol]
56
+ def add_option name, &block
57
+ @options[name] = block
58
+ end
59
+
60
+ def get_option name, option
61
+ method = @options[name]
62
+
63
+ case option
64
+ when Proc
65
+ get_option name, option.call
66
+ else
67
+ method.call option
68
+ end
69
+ end
70
+
71
+ extend self
72
+ end
73
+
74
+
75
+ FirestoreODM.add_option :more_than do |value|
76
+ Proc.new { |o| value < o }
77
+ end
78
+
79
+ FirestoreODM.add_option :min do |value|
80
+ Proc.new { |o| value <= o }
81
+ end
82
+
83
+ FirestoreODM.add_option :max do |value|
84
+ Proc.new { |o| o <= value }
85
+ end
86
+
87
+ FirestoreODM.add_option :less_than do |value|
88
+ Proc.new { |o| o < value }
89
+ end
90
+
91
+ FirestoreODM.add_option :in do |list|
92
+ Proc.new { |o| list.include? o }
17
93
  end
@@ -17,28 +17,33 @@ module FirestoreODM
17
17
  end
18
18
 
19
19
  # Creates a document.
20
- # @return [FirestoreODM::Model] the document.
20
+ # @param opts [Hash] a hash with options.
21
+ # @yield [doc]
22
+ # @return [FirestoreODM::Document] the document.
21
23
  def create opts = {}, &block
22
24
  block.call o = self.new
25
+
23
26
  doc = collection(opts[:relative_to]).add o.changes
24
27
  self.new doc
25
28
  end
26
29
 
27
- # Creates a document.
30
+ # Creates a document with an specific id.
28
31
  # @param id [String] the document id.
29
- # @param root [Google::Cloud::Firestore::DocumentReference]
30
- # @return [FirestoreODM::Model] the document.
32
+ # @param opts [Hash]
33
+ # @yield [doc]
34
+ # @return [FirestoreODM::Document] the document.
31
35
  def create_with_id id, opts = {}, &block
32
36
  block.call o = self.new
33
37
 
34
38
  doc = collection(opts[:relative_to]).doc id
35
39
  doc.create o.changes
36
- self.from_id id
40
+ self.from_id id, opts
37
41
  end
38
42
 
39
43
  # Gets a document.
40
44
  # @param id [String]
41
- # @return [FirestoreODM::Model] the document.
45
+ # @param opts [Hash]
46
+ # @return [FirestoreODM::Document] the document.
42
47
  def from_id id, opts = {}
43
48
  doc = collection(opts[:relative_to]).doc id
44
49
  self.new doc
@@ -5,8 +5,28 @@ module FirestoreODM
5
5
  # @param type [Class] the field's type.
6
6
  # @param opts [Hash] the field's options.
7
7
  def field name, type, opts = {}
8
+ conversion = FirestoreODM.get_conversion type
9
+
10
+ list = []
11
+
12
+ list << FirestoreODM.get_option(:min, opts[:min]) if opts[:min]
13
+ list << FirestoreODM.get_option(:max, opts[:max]) if opts[:max]
14
+ list << FirestoreODM.get_option(:less_than, opts[:less_than]) if opts[:less_than]
15
+ list << FirestoreODM.get_option(:more_than, opts[:more_than]) if opts[:more_than]
16
+ list << FirestoreODM.get_option(:in, opts[:in]) if opts[:in]
17
+
18
+ define_singleton_method("parse_#{name}") do |value|
19
+ list.reduce(true) { |result, o| result and o.call(value) }
20
+ end
21
+
8
22
  define_method(name) { self[name] }
9
- define_setter_method(name) { |value| self[name] = value }
23
+
24
+ define_method("#{name}=") do |value|
25
+ value = conversion.call value
26
+ raise "#{value} is not a valid value for field #{name}" unless self.class.method("parse_#{name}").call value
27
+ self[name] = value
28
+ end
29
+
10
30
  return
11
31
  end
12
32
 
@@ -16,26 +36,30 @@ module FirestoreODM
16
36
  # @param opts [Hash] the reference's options.
17
37
  def reference name, model, opts = {}
18
38
  define_method(name) { model.new self[name] }
19
- define_setter_method(name) { |value| self[name] = value.document }
39
+ define_method("#{name}=") { |value| self[name] = value.document }
20
40
  return
21
41
  end
22
42
 
23
- # @param model [Database::Model]
24
- def contains model, opts = {}
25
- name = model.name.downcase
26
- define_method("create_#{name}") do |opts = {}, &block|
27
- opts = {:relative_to => document}.merge opts
28
- model.create(opts, &block)
29
- end
30
-
31
- define_method("create_#{name}_with_id") do |id, opts = {}, &block|
32
- opts = {:relative_to => document}.merge opts
33
- model.create_with_id(id, opts, &block)
34
- end
35
-
36
- define_method("#{name}_from_id") do |id, opts = {}|
37
- opts = {:relative_to => document}.merge opts
38
- model.from_id(id, opts)
43
+ #
44
+ # @param models [Database::Model]
45
+ def contains *models
46
+ models.each do |model|
47
+ name = model.name.downcase
48
+
49
+ define_method("create_#{name}") do |opts = {}, &block|
50
+ opts = {:relative_to => document}.merge opts
51
+ model.create(opts, &block)
52
+ end
53
+
54
+ define_method("create_#{name}_with_id") do |id, opts = {}, &block|
55
+ opts = {:relative_to => document}.merge opts
56
+ model.create_with_id(id, opts, &block)
57
+ end
58
+
59
+ define_method("#{name}_from_id") do |id, opts = {}|
60
+ opts = {:relative_to => document}.merge opts
61
+ model.from_id(id, opts)
62
+ end
39
63
  end
40
64
  end
41
65
  end
data/test.rb ADDED
@@ -0,0 +1,76 @@
1
+ $:.unshift File.join(Dir.pwd, 'lib')
2
+
3
+ require 'date'
4
+ require 'firestore-odm'
5
+
6
+ class Post < FirestoreODM::Model
7
+ path :posts
8
+
9
+ field :content, String
10
+ field :privacy, String, :in => ['public', 'hidden', 'private']
11
+ field :created_at, DateTime
12
+ end
13
+
14
+ class Account < FirestoreODM::Model
15
+ path :info
16
+
17
+ field :first_name, String
18
+ field :last_name, String
19
+ field :birthday, Date, :max => Proc.new { Date.today.prev_year(18) }
20
+ field :updated_at, DateTime
21
+ end
22
+
23
+ class User < FirestoreODM::Model
24
+ path :users
25
+
26
+ field :name, String
27
+ field :created_at, DateTime
28
+
29
+ contains Post, Account
30
+ end
31
+
32
+ puts Account.parse_birthday(Date.parse('2001-12-02'))
33
+ puts Account.parse_birthday(Date.parse('2002-12-02'))
34
+ puts Account.parse_birthday(Date.parse('2003-12-02'))
35
+ puts Post.parse_privacy('public')
36
+ puts Post.parse_privacy('none')
37
+
38
+ begin
39
+ user = User.create_with_id 'john.smith' do |doc|
40
+ doc.name = 'John Smith'
41
+ doc.created_at = DateTime.now
42
+ end
43
+
44
+ puts "#{user.path}: #{user.to_json}"
45
+
46
+ begin
47
+ user.create_account_with_id 'account' do |doc|
48
+ doc.first_name = 'John'
49
+ doc.last_name = 'Smith'
50
+ doc.birthday = Date.parse('1981-06-25')
51
+ doc.updated_at = DateTime.now
52
+ end
53
+
54
+ begin
55
+ account = user.account_from_id 'account'
56
+
57
+ puts "#{account.path}: #{account.to_json}"
58
+ ensure
59
+ account.delete
60
+ end
61
+
62
+ post = user.create_post do |doc|
63
+ doc.content = 'Hello world!'
64
+ doc.privacy = 'public'
65
+ doc.created_at = DateTime.now
66
+ end
67
+
68
+ begin
69
+ puts "#{post.path}: #{post.to_json}"
70
+ ensure
71
+ post.delete
72
+ end
73
+ ensure
74
+ user.delete
75
+ end
76
+ end
metadata CHANGED
@@ -1,31 +1,58 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: firestore-odm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felipe Cabrera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-31 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-cloud-firestore
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.4.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.4'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.4.0
13
33
  description: ''
14
34
  email: fecabrera0@outlook.com
15
35
  executables: []
16
36
  extensions: []
17
37
  extra_rdoc_files: []
18
38
  files:
39
+ - ".gitignore"
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE
43
+ - README.md
44
+ - firestore-odm.gemspec
19
45
  - lib/firestore-odm.rb
20
46
  - lib/firestore-odm/collection.rb
21
47
  - lib/firestore-odm/document.rb
22
48
  - lib/firestore-odm/model.rb
23
49
  - lib/firestore-odm/schema.rb
24
- homepage:
50
+ - test.rb
51
+ homepage: https://github.com/fecabrera/firestore-odm
25
52
  licenses:
26
53
  - BSD-2-Clause
27
54
  metadata:
28
- source_code_uri: https://github.org/fecabrera/firestore-odm
55
+ source_code_uri: https://github.com/fecabrera/firestore-odm
29
56
  post_install_message:
30
57
  rdoc_options: []
31
58
  require_paths:
@@ -41,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
41
68
  - !ruby/object:Gem::Version
42
69
  version: '0'
43
70
  requirements: []
44
- rubygems_version: 3.0.8
71
+ rubygems_version: 3.1.2
45
72
  signing_key:
46
73
  specification_version: 4
47
74
  summary: ODM for Firestore in Ruby