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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98460eb0778d6a59b63f876bb77db1eb5459b050
4
- data.tar.gz: de124a0a25a87792a7d8b993a39d3b9c193d35e8
3
+ metadata.gz: 02e7026ed898c508dbf1d414caa9a49d46fc768b
4
+ data.tar.gz: d47a262f3619021a1bb2fc0441fe3ece573bca78
5
5
  SHA512:
6
- metadata.gz: 51f6e7ca2e8bbd54aa2b7a2526a85aa28b1cd6fa64fe1595f0a70cf9453ec2b85edef0d30c4cf39af0d50e5e1af36a720b4eacc3ddbc0a7b5ff38c216e53f832
7
- data.tar.gz: bb8d95aa73395c01ead3ad6285e3c86f9ef416e5b84fd77c4166e8a78f1a082daddf6b7d699929d267ef00cf99a3bd17d0f4406702ad00abd5f086804388b454
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('age > 30') }
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 }
@@ -80,8 +80,8 @@ module LazyRecord
80
80
  @all.last
81
81
  end
82
82
 
83
- def where(condition)
84
- @all.where(condition)
83
+ def where(condition = nil, &block)
84
+ @all.where(condition, &block)
85
85
  end
86
86
 
87
87
  def destroy_all
@@ -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? { |key, val| x.send(key) == val }
35
- else
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.1.9'
4
+ VERSION = '0.2.0'
5
5
  end
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.1.9
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-05 00:00:00.000000000 Z
11
+ date: 2017-04-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport