act_as_fire_record_beta 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f91046f509170198a4f91b0530f583bfe4dcf525d860f4951dd42c53d8511cd3
4
+ data.tar.gz: 8ffec0b70b05f93b64a8f6ebe4399d8d85478782f43d8dcb8adf8f49d8ce463b
5
+ SHA512:
6
+ metadata.gz: 07da3548becfcd449bf53f4d056fbea0a7eadf9012f984dd7274a08805e41b51e6dbc287ddb3c48fa89ad9c5f6c50d1850fe1b6af2d91ea472c6230107857965
7
+ data.tar.gz: a9f732055b79f3ae0985c2b7d2c6f6c4c42dd63923cae92bef16a5473f4631f6b43219ca5cc507a6c0fa6ec6459826cf9e887591aa6c5ca668225ee12c6f4434
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2022 Junichi Ito
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # ActAsFireRecordBeta
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem "act_as_fire_record_beta"
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install act_as_fire_record_beta
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,38 @@
1
+ module Google
2
+ module Cloud
3
+ module Firestore
4
+ class Query
5
+ def find_one(&block)
6
+ find_many(limit: 1, &block)[0]
7
+ end
8
+
9
+ def find_one!(&block)
10
+ find_one(&block) || raise(ActiveRecord::RecordNotFound)
11
+ end
12
+
13
+ def find_many(limit: nil, &block)
14
+ scope = limit ? limit(limit) : self
15
+ scope.get.map do |data|
16
+ model_lass = ActAsFireRecordBeta.class_mapping[query.from[0].collection_id]
17
+ record = model_lass.to_instance(data)
18
+ block.call(record) if block
19
+ record
20
+ end
21
+ end
22
+
23
+ def destroy_all
24
+ find_many.each(&:destroy)
25
+ end
26
+
27
+ def exists?
28
+ !!find_one
29
+ end
30
+
31
+ def first(limit = 1)
32
+ records = find_many(limit: limit)
33
+ limit == 1 ? records[0] : records
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ module ActAsFireRecordBeta
2
+ class Railtie < ::Rails::Railtie
3
+ initializer "act_as_fire_record_beta" do
4
+ require "act_as_fire_record_beta/google/cloud/firestore/query"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ActAsFireRecordBeta
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,181 @@
1
+ require "google/cloud/firestore"
2
+ require "act_as_fire_record_beta/version"
3
+ require "act_as_fire_record_beta/railtie"
4
+
5
+ module ActAsFireRecordBeta
6
+ extend ActiveSupport::Concern
7
+ extend Forwardable
8
+
9
+ include ActiveModel::Model
10
+ include ActiveModel::Attributes
11
+ include ActiveModel::Validations
12
+ include ActiveModel::Validations::Callbacks
13
+
14
+ delegate :logger => Rails
15
+
16
+ @class_mapping = {}
17
+ def self.class_mapping
18
+ @class_mapping
19
+ end
20
+
21
+ included do
22
+ attribute :id, :string
23
+ attribute :created_at, :time
24
+ attribute :updated_at, :time
25
+
26
+ ActAsFireRecordBeta.class_mapping[collection_name_with_env] = self
27
+ end
28
+
29
+ class_methods do
30
+ extend Forwardable
31
+ delegate :logger => Rails
32
+
33
+ def firestore_attributes(*names)
34
+ @_firestore_attributes ||= []
35
+ @_firestore_attributes += names.map(&:to_sym)
36
+ end
37
+
38
+ def col
39
+ @_col ||= client.col(collection_name_with_env)
40
+ end
41
+
42
+ def doc(id)
43
+ client.doc("#{collection_name_with_env}/#{id}")
44
+ end
45
+
46
+ def all(&block)
47
+ col.find_many(&block)
48
+ end
49
+
50
+ def find(id)
51
+ data = doc(id).get
52
+ raise ActiveRecord::RecordNotFound if data.missing?
53
+
54
+ to_instance(data)
55
+ end
56
+
57
+ def find_by(param)
58
+ raise ArgumentError, "param size should 1: #{param}" unless param.size == 1
59
+ field, value = param.to_a.flatten
60
+
61
+ col.where(field, :==, value).limit(1).get do |data|
62
+ return to_instance(data)
63
+ end
64
+
65
+ nil
66
+ end
67
+
68
+ def where(field, operator, value)
69
+ col.where(field, operator, value)
70
+ end
71
+
72
+ def order(field, direction = :asc)
73
+ col.order(field, direction)
74
+ end
75
+
76
+ def first(limit = 1)
77
+ records = col.find_many(limit: limit)
78
+ limit == 1 ? records[0] : records
79
+ end
80
+
81
+ def create(params)
82
+ record = new(params)
83
+ record.save
84
+ record
85
+ end
86
+
87
+ def create!(params)
88
+ record = new(params)
89
+ record.save!
90
+ record
91
+ end
92
+
93
+ def destroy_all
94
+ col.list_documents.each(&:delete)
95
+ end
96
+
97
+ def save_params(record)
98
+ @_firestore_attributes.to_h { |key| [key, record.send(key)] }
99
+ end
100
+
101
+ def to_instance(data)
102
+ params = {
103
+ id: data.document_id,
104
+ created_at: data.created_at,
105
+ updated_at: data.updated_at,
106
+ }
107
+ @_firestore_attributes.each do |key|
108
+ params[key] = data[key]
109
+ end
110
+ new(params)
111
+ end
112
+
113
+ private
114
+
115
+ def collection_name
116
+ self.to_s.tableize
117
+ end
118
+
119
+ def client
120
+ @_client ||= Google::Cloud::Firestore.new
121
+ end
122
+
123
+ def collection_name_with_env
124
+ "#{collection_name}-#{Rails.env}".freeze
125
+ end
126
+ end # ClassMethods
127
+
128
+ def new_record?
129
+ id.blank?
130
+ end
131
+
132
+ def persisted?
133
+ !new_record?
134
+ end
135
+
136
+ def save
137
+ raise "insert only." unless new_record?
138
+ return false if invalid?
139
+
140
+ ref = col.add(save_params)
141
+ self.id = ref.document_id
142
+
143
+ true
144
+ end
145
+
146
+ def save!
147
+ save || raise(ActiveRecord::RecordNotSaved.new("Failed to save the record", self))
148
+ end
149
+
150
+ def update(params)
151
+ raise "update only" if new_record?
152
+ self.attributes = params
153
+ return false if invalid?
154
+
155
+ doc.set(save_params, merge: true)
156
+ true
157
+ end
158
+
159
+ def update!(params)
160
+ update(params) || raise(ActiveRecord::RecordNotSaved.new("Failed to save the record", self))
161
+ end
162
+
163
+ def destroy
164
+ doc.delete
165
+ true
166
+ end
167
+
168
+ private
169
+
170
+ def save_params
171
+ self.class.save_params(self)
172
+ end
173
+
174
+ def col
175
+ self.class.col
176
+ end
177
+
178
+ def doc
179
+ self.class.doc(id)
180
+ end
181
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :act_as_fire_record_beta do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: act_as_fire_record_beta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Junichi Ito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-10-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google-cloud-firestore
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ActiveRecord like interface for Firestore
42
+ email:
43
+ - me@jnito.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/act_as_fire_record_beta.rb
52
+ - lib/act_as_fire_record_beta/google/cloud/firestore/query.rb
53
+ - lib/act_as_fire_record_beta/railtie.rb
54
+ - lib/act_as_fire_record_beta/version.rb
55
+ - lib/tasks/act_as_fire_record_beta_tasks.rake
56
+ homepage: https://github.com/JunichiIto/act_as_fire_record_beta
57
+ licenses:
58
+ - MIT
59
+ metadata:
60
+ allowed_push_host: https://rubygems.org
61
+ homepage_uri: https://github.com/JunichiIto/act_as_fire_record_beta
62
+ source_code_uri: https://github.com/JunichiIto/act_as_fire_record_beta
63
+ changelog_uri: https://github.com/JunichiIto/act_as_fire_record_beta/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 2.6.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.3.7
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: ActiveRecord like interface for Firestore
83
+ test_files: []