lite-decorator 1.2.1 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +14 -0
- data/lib/lite/decorator/record.rb +43 -0
- data/lib/lite/decorator/version.rb +1 -1
- data/lib/lite/decorator.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83f15b0a52139c1ac7ec23fe37dfe4aa0c43eee2ad38ca114ebb05a12598b5cb
|
4
|
+
data.tar.gz: e9e225627211d78ebfe11dff2ed7846a600916aca3176c6ece041206e3ab4d44
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 21f72f88662e5bb8a7fe44b588d5779a18288c362ee25d39b7dda33ab69f1692f6f2f09d6d8d59245e8b711262c4add05a86e1e1a694cf45cc4b408e73239de3
|
7
|
+
data.tar.gz: afc4d9dafc6883d65a4703c08c2be6d507371a1e90411fd56179f863e01dba0969ab4fbe192b17b5f2b5f12216cfae545c03cd046e2977e3383409251f6f9d7c
|
data/CHANGELOG.md
CHANGED
@@ -6,6 +6,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
6
6
|
|
7
7
|
## [Unreleased]
|
8
8
|
|
9
|
+
## [1.3.1] - 2024-09-20
|
10
|
+
### Changed
|
11
|
+
- Memoize record decorator class
|
12
|
+
|
13
|
+
## [1.3.0] - 2024-09-20
|
14
|
+
### Added
|
15
|
+
- Record lazy load module
|
16
|
+
|
9
17
|
## [1.2.1] - 2024-09-20
|
10
18
|
### Changed
|
11
19
|
- Bump dependencies
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -26,6 +26,7 @@ Or install it yourself as:
|
|
26
26
|
|
27
27
|
* [Setup](#setup)
|
28
28
|
* [Usage](#usage)
|
29
|
+
* [ActiveRecord](#active_record)
|
29
30
|
|
30
31
|
## Setup
|
31
32
|
|
@@ -58,6 +59,7 @@ end
|
|
58
59
|
## Usage
|
59
60
|
|
60
61
|
To access the decorator you need to pass the object to the decorator class.
|
62
|
+
PORO's just like active record objects can be decorated as well in this method.
|
61
63
|
|
62
64
|
#### Instance
|
63
65
|
```ruby
|
@@ -73,6 +75,18 @@ collection = UserDecorator.new(users)
|
|
73
75
|
collection.map(&:full_name) #=> ["John Doe", "Jane Poe"]
|
74
76
|
```
|
75
77
|
|
78
|
+
## ActiveRecord
|
79
|
+
|
80
|
+
Including the record module will automatically lazy delegate methods to decorators
|
81
|
+
if available. If neither the class instance and the decorator contain the method,
|
82
|
+
a `NoMethodError` just like normal.
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class User < ActiveRecord::Base
|
86
|
+
include Lite::Decorator::Record
|
87
|
+
end
|
88
|
+
```
|
89
|
+
|
76
90
|
## Development
|
77
91
|
|
78
92
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lite
|
4
|
+
module Decorator
|
5
|
+
module Record
|
6
|
+
|
7
|
+
def decorator_class
|
8
|
+
return @decorator_class if defined?(@decorator_class)
|
9
|
+
|
10
|
+
@decorator_class = "#{self.class.name}Decorator".safe_constantize
|
11
|
+
end
|
12
|
+
|
13
|
+
def decorator
|
14
|
+
@decorator ||= decorator_class&.new(self)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def method_missing(method_name, *args, **kwargs, &)
|
20
|
+
if respond_to_method?(method_name)
|
21
|
+
decorator.send(method_name, *args, **kwargs, &)
|
22
|
+
else
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def respond_to_method_cache
|
28
|
+
@respond_to_method_cache ||= {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def respond_to_method?(method_name)
|
32
|
+
return respond_to_method_cache[method_name] if respond_to_method_cache.key?(method_name)
|
33
|
+
|
34
|
+
respond_to_method_cache[method_name] = decorator.present? && decorator.public_methods.include?(method_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
def respond_to_missing?(method_name, include_private = false)
|
38
|
+
respond_to_method?(method_name) || super
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/lite/decorator.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lite-decorator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Gomez
|
@@ -204,6 +204,7 @@ files:
|
|
204
204
|
- lib/generators/rails/templates/decorator.rb.tt
|
205
205
|
- lib/lite/decorator.rb
|
206
206
|
- lib/lite/decorator/base.rb
|
207
|
+
- lib/lite/decorator/record.rb
|
207
208
|
- lib/lite/decorator/version.rb
|
208
209
|
- lite-decorator.gemspec
|
209
210
|
homepage: http://drexed.github.io/lite-decorator
|