lazy_record 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -1
- data/example/person.rb +1 -1
- data/lib/lazy_record/base_module.rb +2 -2
- data/lib/lazy_record/relation.rb +8 -3
- data/lib/lazy_record/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02e7026ed898c508dbf1d414caa9a49d46fc768b
|
4
|
+
data.tar.gz: d47a262f3619021a1bb2fc0441fe3ece573bca78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d02068fc6418b585f478f0b70260da72849d8945510a0724d9847376a9ec2ec5ec149c9566d76b776b52650124acf1177a61e81278f8d830750e9f8245085ef
|
7
|
+
data.tar.gz: 50f028b5d8c2cf45130a9d58b702cd4aad0c0d31eeb3fe1f91817d7a4a595cb6c4dd5304c369858c50e31a9b44d645dad745d37c5173ad6b9949b7b8d9bbd035
|
data/README.md
CHANGED
@@ -137,7 +137,23 @@ Whatever.low_sleepy
|
|
137
137
|
Whatever.where('id == 1')
|
138
138
|
# => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>
|
139
139
|
```
|
140
|
-
|
140
|
+
You can also use hash syntax and block syntax with `.where`. Block syntax will yield each object in the collection to the block for evaluation.
|
141
|
+
```ruby
|
142
|
+
Whatever.where { |w| w.sleepy_value > 5 }
|
143
|
+
# => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>, #<Whatever id: 3, party_value: 4, sleepy_value: 11>]>
|
144
|
+
Whatever.where { |w| w.sleepy_value == w.party_value }
|
145
|
+
# => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>]>
|
146
|
+
```
|
147
|
+
When using hash syntax can be a value, an expression, or a Proc object.
|
148
|
+
```ruby
|
149
|
+
Whatever.where party_value: 12
|
150
|
+
# => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>]>
|
151
|
+
Whatever.where party_value: 7 + 6, sleepy_value: 3
|
152
|
+
# => #<WhateverRelation [#<Whatever id: 2, party_value: 13, sleepy_value: 3>]>
|
153
|
+
num = 6
|
154
|
+
Whatever.where party_value: -> { num * 2 }
|
155
|
+
# => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>]>
|
156
|
+
```
|
141
157
|
Use `lr_method` for an alternative API for defining short instance methods. Can use lambda syntax or string syntax. Best for quick one-liners. If the method references `self` of the instance, either explicitly or implicitly, it needs to use the string syntax, since anything passed into the lambda will be evaluated in the context of the Class level scope.
|
142
158
|
|
143
159
|
```ruby
|
data/example/person.rb
CHANGED
@@ -10,7 +10,7 @@ class Person < LazyRecord::Base
|
|
10
10
|
new(opts) { |p| p.adopt_a_dog(opts[:dog]) }
|
11
11
|
}
|
12
12
|
lr_scope :young, -> { where('age < 30') }
|
13
|
-
lr_scope :old, -> { where
|
13
|
+
lr_scope :old, -> { where { |x| x.age > 30 } }
|
14
14
|
lr_scope :short_hair, -> { where(haircut: 'short') }
|
15
15
|
|
16
16
|
lr_method :speak, ->(string) { puts string }
|
data/lib/lazy_record/relation.rb
CHANGED
@@ -28,11 +28,16 @@ module LazyRecord
|
|
28
28
|
"\#<#{model}Relation [#{all.map(&:inspect).join(', ')}]>"
|
29
29
|
end
|
30
30
|
|
31
|
-
def where(condition)
|
31
|
+
def where(condition = nil)
|
32
32
|
result = all.select do |x|
|
33
33
|
if condition.is_a? Hash
|
34
|
-
condition.all?
|
35
|
-
|
34
|
+
condition.all? do |key, val|
|
35
|
+
val = val.call if val.is_a? Proc
|
36
|
+
x.send(key) == val
|
37
|
+
end
|
38
|
+
elsif block_given?
|
39
|
+
yield x
|
40
|
+
elsif condition
|
36
41
|
eval "x.#{condition}"
|
37
42
|
end
|
38
43
|
end
|
data/lib/lazy_record/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- M. Simon Borg
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-04-
|
11
|
+
date: 2017-04-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|