firestore-odm 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/firestore-odm.rb +64 -0
- data/lib/firestore-odm/collection.rb +10 -5
- data/lib/firestore-odm/schema.rb +20 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c19b9311e05a559ae948eb18b2c1dcd45bab13f779e856f55304928fffdfe9b
|
4
|
+
data.tar.gz: e25f77db855209bc069a7ca45aae3f776bbb03c47d983fd8e25abbed8e96b472
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 04b5050dbb29a05f521b0bb82830808fc2cbf4af3beac1338d99b07daf4e754429317579b2c6f3c01912ac0fb42d865fe52bd5f81139c41ba091c6b8e8427c34
|
7
|
+
data.tar.gz: a87df1fadd1972422370c484f81f0550ffb1265e957763fa1338e51888ae3003ff84c6db8066e8f889234472065a3173307c4cfd13db1e43ea2a1014b8c8a5ce
|
data/lib/firestore-odm.rb
CHANGED
@@ -14,4 +14,68 @@ 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
|
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
|
+
# Sets a conversion rule for a certain type.
|
32
|
+
# @param type [Class]
|
33
|
+
# @param target [Symbol, Proc]
|
34
|
+
def add_conversion type, target
|
35
|
+
@conversions[type] = target
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
# Gets a conversion.
|
40
|
+
# @param type [Class]
|
41
|
+
# @return [Proc]
|
42
|
+
def get_conversion type
|
43
|
+
case rule = conversions[type]
|
44
|
+
when Symbol
|
45
|
+
Proc.new { |value| value.method(rule).call }
|
46
|
+
when Proc
|
47
|
+
rule
|
48
|
+
else
|
49
|
+
raise "Conversion not found for type `#{type}`"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def more_than value
|
54
|
+
Proc.new { |o| value < o }
|
55
|
+
end
|
56
|
+
|
57
|
+
def min value
|
58
|
+
Proc.new { |o| value <= o }
|
59
|
+
end
|
60
|
+
|
61
|
+
def max value
|
62
|
+
Proc.new { |o| o <= value }
|
63
|
+
end
|
64
|
+
|
65
|
+
def less_than value
|
66
|
+
Proc.new { |o| o < value }
|
67
|
+
end
|
68
|
+
|
69
|
+
def check_option name, option
|
70
|
+
method = FirestoreODM.method name
|
71
|
+
|
72
|
+
case option
|
73
|
+
when Proc
|
74
|
+
check_option name, option.call
|
75
|
+
else
|
76
|
+
method.call option
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
extend self
|
17
81
|
end
|
@@ -17,17 +17,21 @@ module FirestoreODM
|
|
17
17
|
end
|
18
18
|
|
19
19
|
# Creates a document.
|
20
|
-
# @
|
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
|
30
|
-
# @
|
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
|
|
@@ -38,7 +42,8 @@ module FirestoreODM
|
|
38
42
|
|
39
43
|
# Gets a document.
|
40
44
|
# @param id [String]
|
41
|
-
# @
|
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
|
data/lib/firestore-odm/schema.rb
CHANGED
@@ -5,8 +5,27 @@ 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.check_option(:min, opts[:min]) if opts[:min]
|
13
|
+
list << FirestoreODM.check_option(:max, opts[:max]) if opts[:max]
|
14
|
+
list << FirestoreODM.check_option(:less_than, opts[:less_than]) if opts[:less_than]
|
15
|
+
list << FirestoreODM.check_option(:more_than, opts[:more_than]) if opts[:more_than]
|
16
|
+
|
17
|
+
define_singleton_method("parse_#{name}") do |value|
|
18
|
+
list.reduce(true) { |result, o| result and o.call(value) }
|
19
|
+
end
|
20
|
+
|
8
21
|
define_method(name) { self[name] }
|
9
|
-
|
22
|
+
|
23
|
+
define_method("#{name}=") do |value|
|
24
|
+
value = conversion.call value
|
25
|
+
raise "#{value} is not a valid value for field #{name}" unless self.class.method("parse_#{name}").call value
|
26
|
+
self[name] = value
|
27
|
+
end
|
28
|
+
|
10
29
|
return
|
11
30
|
end
|
12
31
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: firestore-odm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
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-
|
11
|
+
date: 2020-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ''
|
14
14
|
email: fecabrera0@outlook.com
|