lazy_record 0.4.4 → 0.5.0
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 +4 -4
- data/README.md +22 -22
- data/lib/lazy_record/base_module.rb +11 -16
- data/lib/lazy_record/callbacks.rb +1 -11
- data/lib/lazy_record/collections.rb +3 -1
- data/lib/lazy_record/nesting.rb +3 -1
- 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: 6e001b11d0607098306b11994a55436e0f1ee6a9
|
4
|
+
data.tar.gz: a38d5dc0d58a33665aaea2b3bedd7c7b22018f8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aeadbadcb0ffe05bd58a86996734aebfe9f9e0cccdc89e428874857e1d2b51e83c3e9de4813956e69ae8886ca9bdb95427b87f5a9a3683f8dde8366f43e8cf23
|
7
|
+
data.tar.gz: 262e1db0468746deb5c76300f5297159cca1aeea6e98b54d0e6a680e155ef6a49b768835a770e71fe2296d1059c4c62909210fa086af59e21e4ddae299a92577
|
data/README.md
CHANGED
@@ -29,7 +29,7 @@ end
|
|
29
29
|
|
30
30
|
thing = Thing.new { |t| puts t.class.superclass }
|
31
31
|
# LazyRecord::Base
|
32
|
-
# => #<Thing
|
32
|
+
# => #<Thing>
|
33
33
|
```
|
34
34
|
|
35
35
|
Alternatively, if you want to inherit from another class, you can mix in the `LazyRecord::BaseModule` and get all the same results.
|
@@ -41,7 +41,7 @@ end
|
|
41
41
|
|
42
42
|
thing = Thing.new { |t| puts t.class.superclass }
|
43
43
|
# Object
|
44
|
-
# => #<Thing
|
44
|
+
# => #<Thing>
|
45
45
|
```
|
46
46
|
Every LazyRecord object is assigned an auto-incrementing ID after initialization. IDs reset when the program is terminated.
|
47
47
|
|
@@ -60,7 +60,7 @@ end
|
|
60
60
|
thing = Thing.new stuff: 'stuff' do |t|
|
61
61
|
t.junk = 'junk'
|
62
62
|
end
|
63
|
-
# => #<Thing
|
63
|
+
# => #<Thing stuff: "stuff", junk: "junk", hmm: nil>
|
64
64
|
thing.something
|
65
65
|
# => "something"
|
66
66
|
```
|
@@ -95,7 +95,7 @@ end
|
|
95
95
|
thing = Thing.new junk: 'junk'
|
96
96
|
ArgumentError
|
97
97
|
stuff must be given
|
98
|
-
#<Thing
|
98
|
+
#<Thing stuff: nil, junk: "junk">
|
99
99
|
# => false
|
100
100
|
```
|
101
101
|
Use `lr_has_many` to set up associated collections of another class. `lr_belongs_to` will be added in a future update.
|
@@ -111,16 +111,16 @@ class Thing < LazyRecord::Base
|
|
111
111
|
end
|
112
112
|
|
113
113
|
whatever = Whatever.new
|
114
|
-
# => #<Whatever
|
114
|
+
# => #<Whatever>
|
115
115
|
|
116
116
|
thing = Thing.new do |t|
|
117
117
|
t.stuff = 'stuff'
|
118
118
|
t.whatevers << whatever
|
119
119
|
end
|
120
|
-
# => #<Thing
|
120
|
+
# => #<Thing stuff: "stuff", junk: nil>
|
121
121
|
|
122
122
|
thing.whatevers
|
123
|
-
# => #<WhateverRelation [#<Whatever
|
123
|
+
# => #<WhateverRelation [#<Whatever>]>
|
124
124
|
```
|
125
125
|
|
126
126
|
Use `lr_scope` and `#where` to create class scope methods and query objects. Works just like ActiveRecord scopes, including scope chaining. Only since it is all Ruby and no SQL, use `==` as the comparison operator.
|
@@ -146,39 +146,39 @@ thing = Thing.new do |t|
|
|
146
146
|
t.stuff = 'stuff'
|
147
147
|
t.whatevers = Whatever.all
|
148
148
|
end
|
149
|
-
# => #<Thing
|
149
|
+
# => #<Thing stuff: "stuff", junk: nil>
|
150
150
|
|
151
151
|
thing.whatevers.big_party
|
152
|
-
# => #<WhateverRelation [#<Whatever
|
152
|
+
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>, #<Whatever party_value: 13, sleepy_value: 3>]>
|
153
153
|
|
154
154
|
thing.whatevers.low_sleepy
|
155
|
-
# => #<WhateverRelation [#<Whatever
|
155
|
+
# => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>, #<Whatever party_value: 3, sleepy_value: 5>]>
|
156
156
|
|
157
157
|
thing.whatevers.big_party.low_sleepy
|
158
|
-
# => #<WhateverRelation [#<Whatever
|
158
|
+
# => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>]>
|
159
159
|
|
160
160
|
Whatever.low_sleepy
|
161
|
-
# => #<WhateverRelation [#<Whatever
|
161
|
+
# => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>, #<Whatever id: 4, party_value: 3, sleepy_value: 5>]>
|
162
162
|
|
163
|
-
Whatever.where('
|
164
|
-
# => #<WhateverRelation [#<Whatever
|
163
|
+
Whatever.where('party_value == 12')
|
164
|
+
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>
|
165
165
|
```
|
166
166
|
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.
|
167
167
|
```ruby
|
168
168
|
Whatever.where { |w| w.sleepy_value > 5 }
|
169
|
-
# => #<WhateverRelation [#<Whatever
|
169
|
+
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>, #<Whatever id: 3, party_value: 4, sleepy_value: 11>]>
|
170
170
|
Whatever.where { |w| w.sleepy_value == w.party_value }
|
171
|
-
# => #<WhateverRelation [#<Whatever
|
171
|
+
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>]>
|
172
172
|
```
|
173
|
-
When using hash syntax can be
|
173
|
+
When using hash syntax the value can be an object, an expression, or a Proc.
|
174
174
|
```ruby
|
175
175
|
Whatever.where party_value: 12
|
176
|
-
# => #<WhateverRelation [#<Whatever
|
176
|
+
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>]>
|
177
177
|
Whatever.where party_value: 7 + 6, sleepy_value: 3
|
178
|
-
# => #<WhateverRelation [#<Whatever
|
178
|
+
# => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>]>
|
179
179
|
num = 6
|
180
180
|
Whatever.where party_value: -> { num * 2 }
|
181
|
-
# => #<WhateverRelation [#<Whatever
|
181
|
+
# => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>]>
|
182
182
|
```
|
183
183
|
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.
|
184
184
|
|
@@ -203,10 +203,10 @@ thing.speak "I'm a thing"
|
|
203
203
|
# => nil
|
204
204
|
|
205
205
|
thing.add_whatever(true)
|
206
|
-
# => [#<Whatever
|
206
|
+
# => [#<Whatever party_value: nil, sleepy_value: nil, right: true>]
|
207
207
|
|
208
208
|
thing.whatevers
|
209
|
-
# => #<WhateverRelation [#<Whatever
|
209
|
+
# => #<WhateverRelation [#<Whatever party_value: nil, sleepy_value: nil, right: true>]>
|
210
210
|
```
|
211
211
|
|
212
212
|
## Development
|
@@ -18,8 +18,6 @@ module LazyRecord
|
|
18
18
|
base.extend DynamicModules
|
19
19
|
end
|
20
20
|
|
21
|
-
attr_writer :id
|
22
|
-
|
23
21
|
# Use options hash to set attributes, and/or operate on object in a block.
|
24
22
|
# Checks each options key for a matching attribute setter method.
|
25
23
|
def initialize(opts = {})
|
@@ -46,20 +44,19 @@ module LazyRecord
|
|
46
44
|
end
|
47
45
|
|
48
46
|
def inspect
|
49
|
-
format('#<%s
|
47
|
+
format('#<%s%s>',
|
50
48
|
self.class,
|
51
|
-
|
52
|
-
public_attr_readers_to_s.dup.unshift('').join(', '),
|
53
|
-
associations_to_s.unshift('').join(', '),
|
54
|
-
collection_counts_to_s.unshift('').join(', '))
|
55
|
-
end
|
56
|
-
|
57
|
-
def display_id_even_if_nil
|
58
|
-
id ? id.to_s : 'nil'
|
49
|
+
displayable_attributes.join(', '))
|
59
50
|
end
|
60
51
|
|
61
|
-
def
|
62
|
-
|
52
|
+
def displayable_attributes
|
53
|
+
attributes = [
|
54
|
+
public_attr_readers_to_s.dup,
|
55
|
+
associations_to_s.dup,
|
56
|
+
collection_counts_to_s.dup
|
57
|
+
].flatten
|
58
|
+
return attributes if attributes.empty?
|
59
|
+
attributes.unshift(' ' + attributes.shift)
|
63
60
|
end
|
64
61
|
|
65
62
|
def stringify_value(value)
|
@@ -72,9 +69,7 @@ module LazyRecord
|
|
72
69
|
end
|
73
70
|
end
|
74
71
|
|
75
|
-
private :
|
76
|
-
:display_id_even_if_nil,
|
77
|
-
:stringify_value,
|
72
|
+
private :stringify_value,
|
78
73
|
:public_attr_readers_to_s,
|
79
74
|
:collection_counts_to_s
|
80
75
|
|
@@ -11,17 +11,7 @@ module LazyRecord
|
|
11
11
|
if instance.respond_to?(:validation)
|
12
12
|
instance = instance.validation(*@validations)
|
13
13
|
end
|
14
|
-
|
14
|
+
instance.tap { |inst| all << inst if inst }
|
15
15
|
end
|
16
|
-
|
17
|
-
def add_id(instance)
|
18
|
-
if instance
|
19
|
-
all << instance
|
20
|
-
instance.send(:id=, count)
|
21
|
-
end
|
22
|
-
instance
|
23
|
-
end
|
24
|
-
|
25
|
-
private :add_id
|
26
16
|
end
|
27
17
|
end
|
data/lib/lazy_record/nesting.rb
CHANGED
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.5.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-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|