lazy_record 0.4.4 → 0.5.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: 86fc7c8a4fc3d5cb68c30d8967dccc32e41a300a
4
- data.tar.gz: 4a3d473eef29dda64e62689394103992a46a1750
3
+ metadata.gz: 6e001b11d0607098306b11994a55436e0f1ee6a9
4
+ data.tar.gz: a38d5dc0d58a33665aaea2b3bedd7c7b22018f8a
5
5
  SHA512:
6
- metadata.gz: 2192b9b391256c80b27c2bbc0e8dabe0f617793c8ad502d092c8e88f8fcd75dabbab0313c442386469c4378cead5509b4bb9b4c62b7e09a2b341792b975ebc87
7
- data.tar.gz: 121c6dc8b06a5a11b7c6bc8895e380156d332c98385ddc0c8685041ed3d2c60e43f9de807ff2fadc6b6befd95e38408497665102fc2f893e46862fe97a4880ed
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 id: 1>
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 id: 1>
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 id: 1, stuff: "stuff", junk: "junk", hmm: nil>
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 id: nil, stuff: nil, junk: "junk">
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 id: 1>
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 id: 1, stuff: "stuff", junk: nil>
120
+ # => #<Thing stuff: "stuff", junk: nil>
121
121
 
122
122
  thing.whatevers
123
- # => #<WhateverRelation [#<Whatever id: 1>]>
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 id: 1, stuff: "stuff", junk: nil>
149
+ # => #<Thing stuff: "stuff", junk: nil>
150
150
 
151
151
  thing.whatevers.big_party
152
- # => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>, #<Whatever id: 2, party_value: 13, sleepy_value: 3>]>
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 id: 2, party_value: 13, sleepy_value: 3>, #<Whatever id: 4, party_value: 3, sleepy_value: 5>]>
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 id: 2, party_value: 13, sleepy_value: 3>]>
158
+ # => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>]>
159
159
 
160
160
  Whatever.low_sleepy
161
- # => #<WhateverRelation [#<Whatever id: 2, party_value: 13, sleepy_value: 3>, #<Whatever id: 4, party_value: 3, sleepy_value: 5>]>
161
+ # => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>, #<Whatever id: 4, party_value: 3, sleepy_value: 5>]>
162
162
 
163
- Whatever.where('id == 1')
164
- # => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>
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 id: 1, party_value: 12, sleepy_value: 12>, #<Whatever id: 3, party_value: 4, sleepy_value: 11>]>
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 id: 1, party_value: 12, sleepy_value: 12>]>
171
+ # => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>]>
172
172
  ```
173
- When using hash syntax can be a value, an expression, or a Proc object.
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 id: 1, party_value: 12, sleepy_value: 12>]>
176
+ # => #<WhateverRelation [#<Whatever party_value: 12, sleepy_value: 12>]>
177
177
  Whatever.where party_value: 7 + 6, sleepy_value: 3
178
- # => #<WhateverRelation [#<Whatever id: 2, party_value: 13, sleepy_value: 3>]>
178
+ # => #<WhateverRelation [#<Whatever party_value: 13, sleepy_value: 3>]>
179
179
  num = 6
180
180
  Whatever.where party_value: -> { num * 2 }
181
- # => #<WhateverRelation [#<Whatever id: 1, party_value: 12, sleepy_value: 12>]>
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 id: 1, party_value: nil, sleepy_value: nil, right: true>]
206
+ # => [#<Whatever party_value: nil, sleepy_value: nil, right: true>]
207
207
 
208
208
  thing.whatevers
209
- # => #<WhateverRelation [#<Whatever id: 1, party_value: nil, sleepy_value: nil, right: true>]>
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 id: %s%s%s%s>',
47
+ format('#<%s%s>',
50
48
  self.class,
51
- display_id_even_if_nil,
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 id
62
- @id.freeze
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 :id=,
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
- add_id(instance)
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'lazy_record/nesting'
2
4
 
3
5
  module LazyRecord
@@ -88,4 +90,4 @@ module LazyRecord
88
90
  end
89
91
  end
90
92
  end
91
- end
93
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module LazyRecord
2
4
  # Apply the same namespace nesting as self to another object.
3
5
  module Nesting
@@ -5,4 +7,4 @@ module LazyRecord
5
7
  "#{to_s.split('::')[0..-3].join('::')}::#{class_name}"
6
8
  end
7
9
  end
8
- end
10
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LazyRecord
4
- VERSION = '0.4.4'
4
+ VERSION = '0.5.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.4.4
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-17 00:00:00.000000000 Z
11
+ date: 2017-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport