lite-decorator 1.3.0 → 2.0.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
  SHA256:
3
- metadata.gz: 1fbe8cde8843bd22d6181ad4aeaebd5fe3e74eab12a970c3862a6615e4e761a1
4
- data.tar.gz: 1304f8031585e3500a704bdbb4091816a8966177325c094e24dfa1f02f7654ed
3
+ metadata.gz: 79964d52a1fbc21e1b05b9fa7b7ba819a49e0dcfa1ad82999fa4ac5c3361cfd3
4
+ data.tar.gz: 2320e385b9dae23e2b3834a6fc47990780c74eca64db25a59859c1c61eb9ca29
5
5
  SHA512:
6
- metadata.gz: 9207d3e6a931bc694d25030ae219d88efb8ec99798623a6b0c36ae20f00131034515be2102075b216c2077bee890772cbd00cb46bfe3c556efab3d393c497c4e
7
- data.tar.gz: d43cfa5cb11fd7987476229ec6c95b332c75f4c288ff1343cb2ade16e2214e93b91f914d5c790ca7c5c6510f28b4534b32b99bc7c784532c70a71a51f8c205ad
6
+ metadata.gz: c2c3b1b9994591964fcf233883e635ab7ce649c3ce8e2dac46cd8e37d5010d27818117ee9a38ec038bef76a40975566f0cf085420675b295c66919cad178dcd0
7
+ data.tar.gz: 6ff5619a096ea3ea9333713fb4ba59936c407670ab71ad9856b177ddda2dbf766caaa82b91b0a5c022d5b3b3e1acc5c2d5fd43f02fcc564d47372ec7d31872c9
data/CHANGELOG.md CHANGED
@@ -6,6 +6,10 @@ 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
+
9
13
  ## [1.3.0] - 2024-09-20
10
14
  ### Added
11
15
  - Record lazy load module
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lite-decorator (1.3.0)
4
+ lite-decorator (2.0.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -59,7 +59,7 @@ end
59
59
  ## Usage
60
60
 
61
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.
62
+ PORO's and ActiveRecord objects can be decorated with this method.
63
63
 
64
64
  #### Instance
65
65
  ```ruby
@@ -75,16 +75,35 @@ collection = UserDecorator.new(users)
75
75
  collection.map(&:full_name) #=> ["John Doe", "Jane Poe"]
76
76
  ```
77
77
 
78
- ## ActiveRecord
78
+ ## Infered decoration
79
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.
80
+ Including the `Infered` module will setup a `decorator_class` and `decorator` method
81
+ that you can access via your PORO and ActiveRecord objects.
82
+
83
+ ```ruby
84
+ class User < ActiveRecord::Base
85
+ include Lite::Decorator::Infered
86
+ end
87
+
88
+ # Usage
89
+ user = User.first
90
+ user.decorator.full_name
91
+ ```
92
+
93
+ ## Record decoration
94
+
95
+ Including the `Record` module will use method missing to delegate decorator methods as
96
+ if it lived on the ActiveRecord object itself. If neither the class instance and the
97
+ decorator contain the method, a `NoMethodError` just like normal.
83
98
 
84
99
  ```ruby
85
100
  class User < ActiveRecord::Base
86
101
  include Lite::Decorator::Record
87
102
  end
103
+
104
+ # Usage
105
+ user = User.first
106
+ user.full_name
88
107
  ```
89
108
 
90
109
  ## Development
@@ -6,14 +6,10 @@ module Lite
6
6
  module Decorator
7
7
  class Base < SimpleDelegator
8
8
 
9
- class << self
10
-
11
- def decorate(object)
12
- return new(object) unless object.respond_to?(:each)
13
-
14
- object.map { |obj| new(obj) }
15
- end
9
+ def self.decorate(object)
10
+ return new(object) unless object.respond_to?(:each)
16
11
 
12
+ object.map { |obj| new(obj) }
17
13
  end
18
14
 
19
15
  def class
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Decorator
5
+ module Infered
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
+ end
18
+ end
19
+ end
@@ -4,12 +4,8 @@ module Lite
4
4
  module Decorator
5
5
  module Record
6
6
 
7
- def decorator_class
8
- "#{self.class.name}Decorator".safe_constantize
9
- end
10
-
11
- def decorator
12
- @decorator ||= decorator_class&.new(self)
7
+ def self.included(base)
8
+ base.include Infered
13
9
  end
14
10
 
15
11
  private
@@ -3,7 +3,7 @@
3
3
  module Lite
4
4
  module Decorator
5
5
 
6
- VERSION = "1.3.0"
6
+ VERSION = "2.0.0"
7
7
 
8
8
  end
9
9
  end
@@ -4,4 +4,5 @@ require "generators/rails/decorator_generator" if defined?(Rails::Generators)
4
4
 
5
5
  require "lite/decorator/version"
6
6
  require "lite/decorator/base"
7
+ require "lite/decorator/infered"
7
8
  require "lite/decorator/record"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lite-decorator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juan Gomez
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-21 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -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/infered.rb
207
208
  - lib/lite/decorator/record.rb
208
209
  - lib/lite/decorator/version.rb
209
210
  - lite-decorator.gemspec