exposant 0.1.0 → 0.1.2

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: a32157d1c5d5bc482d278ea27afc87bea199f1a70459f5158e17a4cb27a35c2b
4
- data.tar.gz: 4a31ae775b219ded8bce8961ee321ab57334af8603412997b6622a5e9e918868
3
+ metadata.gz: e71f97dbf32080d25a520091a6e33d480ad18e6a3fff6bcd6e450b80c0cd5414
4
+ data.tar.gz: 499db0ccd9fd33e879dcc035f4decf21ea7c8874042ddc113bc4862b3bf726e6
5
5
  SHA512:
6
- metadata.gz: b30952a3a17b5f5bd718d2a1e54f7fb7e186a60cd2a2a0cf667e8f9716eb42fc70d0b80cf1303544c96ee2a39893336bfe4d3ce29e21f70c73f390781a983524
7
- data.tar.gz: e0ae7a0c8501b34037038621b79ab9bb78b596c6c3216974bf74970b3f3f8ee0bf328b18e703926bf451dd5dd87797ee3099178ace2d29d2cfb0033560f793b6
6
+ metadata.gz: d446462784d85f9b13426e79fb69a484a827c8bb347d117c286fa3da0a841aace9c51ea265a5d23256a53da36112c6b3b6c28be0a6502a28cb1c7414bd80a2e7
7
+ data.tar.gz: 4f0642a4b7001c14b149deaf738738ae13e8541cb54c3987405e383ef5656c1ade8fe84c1b8c31756659c7816869c10b0a83023f18500ac73c81d2560d733ed6
data/Gemfile.lock CHANGED
@@ -1,22 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- exposant (0.1.0)
4
+ exposant (0.1.2)
5
5
  activemodel (~> 7.0)
6
6
  activesupport (~> 7.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (7.0.4)
12
- activesupport (= 7.0.4)
13
- activesupport (7.0.4)
11
+ activemodel (7.0.4.2)
12
+ activesupport (= 7.0.4.2)
13
+ activesupport (7.0.4.2)
14
14
  concurrent-ruby (~> 1.0, >= 1.0.2)
15
15
  i18n (>= 1.6, < 2)
16
16
  minitest (>= 5.1)
17
17
  tzinfo (~> 2.0)
18
18
  ast (2.4.2)
19
- concurrent-ruby (1.1.10)
19
+ concurrent-ruby (1.2.0)
20
20
  i18n (1.12.0)
21
21
  concurrent-ruby (~> 1.0)
22
22
  json (2.6.1)
@@ -41,7 +41,7 @@ GEM
41
41
  rubocop-ast (1.21.0)
42
42
  parser (>= 3.1.1.0)
43
43
  ruby-progressbar (1.11.0)
44
- tzinfo (2.0.5)
44
+ tzinfo (2.0.6)
45
45
  concurrent-ruby (~> 1.0)
46
46
  unicode-display_width (2.2.0)
47
47
 
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Exposant
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/exposant`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ In a Rails application, it is often required to fill a gap between Models and
4
+ Views. There are of course, many differents ways to fill this gap, one of these
5
+ is exhibitors (or improved decorators).
4
6
 
5
- TODO: Delete this and the text above, and describe your gem
7
+ This gem provide an easy way to create Ruby exhibitors or decorators.
6
8
 
7
9
  ## Installation
8
10
 
@@ -22,7 +24,55 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ There are two kinds of exhibitors refering to model or collection.
28
+
29
+ A collection exhibitor is meant to encapsulate an enumerable object (like
30
+ ActiveRecord Relation or just Array). It overrides `each` method to ensure
31
+ encapsulation of resulting objects.
32
+
33
+ A model exhibitor improve it's associated object, like adding non-database
34
+ related methods to an ActiveRecord object for example.
35
+
36
+ To use this gem in a Rails application, create a folder `app/exhibitors`.
37
+ Create pluralized exhibitors for collections and singularized exhibitors for
38
+ models.
39
+
40
+ ### Example:
41
+
42
+ Consider having a User model with `first_name` and `last_name`
43
+
44
+ ```ruby
45
+ # app/models/application_record.rb
46
+ class ApplicationRecord < ActiveRecord::Base
47
+ include Exposant::Exposable
48
+ end
49
+
50
+ # app/exhibitors/user_exhibitor.rb
51
+ class UserExhibitor < Exposant::ModelExhibitor
52
+ def full_name
53
+ "#{first_name} #{last_name}"
54
+ end
55
+ end
56
+
57
+ # app/exhibitors/users_exhibitor.rb
58
+ class UsersExhibitor < Exposant::CollectionExhibitor
59
+ # You can add methods for collections too if necessary
60
+ end
61
+ ```
62
+
63
+ Then you may want to use your brand new exhibitor in your controller
64
+ ```ruby
65
+ # app/controllers/users_controller.rb
66
+ class UsersController < DefaultController
67
+ def index
68
+ @users = User.exhibitor(User.all)
69
+ end
70
+
71
+ def show
72
+ @user = User.find(...).exhibitor
73
+ end
74
+ end
75
+ ```
26
76
 
27
77
  ## Development
28
78
 
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ require 'rake/testtask'
6
6
  Rake::TestTask.new(:test) do |t|
7
7
  t.libs << 'test'
8
8
  t.libs << 'lib'
9
- t.test_files = FileList['test/**/test_*.rb']
9
+ t.test_files = FileList['test/**/*_test.rb']
10
10
  end
11
11
 
12
12
  require 'rubocop/rake_task'
@@ -11,7 +11,12 @@ module Exposant
11
11
  def each
12
12
  return enum_for(:each) unless block_given?
13
13
 
14
- __getobj__.each { |o| yield o.exhibitor(self.class.exhibitor_variant) }
14
+ __getobj__.each do |o|
15
+ exh = o&.exhibitor(self.class.exhibitor_variant)
16
+ exh.contextualize(context) if exh.present? && contextualized?
17
+
18
+ yield exh
19
+ end
15
20
  end
16
21
 
17
22
  def to_model
@@ -1,5 +1,6 @@
1
1
  module Exhibitor
2
2
  extend ActiveSupport::Concern
3
+ attr_accessor :context
3
4
 
4
5
  def obj
5
6
  __getobj__
@@ -9,7 +10,16 @@ module Exhibitor
9
10
  self.class.exhibitor_for(obj)
10
11
  end
11
12
 
13
+ def contextualize(context)
14
+ self.context = context
15
+ end
16
+
17
+ def contextualized?
18
+ context.present?
19
+ end
20
+
12
21
  module ClassMethods
22
+
13
23
  def exhibitor_for_super(method, klass = nil)
14
24
  define_method(method) do |*args|
15
25
  klass ||= self.class
@@ -1,5 +1,5 @@
1
1
  module Exposant
2
- module Presentable
2
+ module Exposable
3
3
  module Model
4
4
  extend ActiveSupport::Concern
5
5
 
@@ -9,7 +9,7 @@ module Exposant
9
9
 
10
10
  module ClassMethods
11
11
  def exhibitor(obj, variant = nil)
12
- obj.extend(Presentable::Collection)
12
+ obj.extend(Exposable::Collection)
13
13
  obj.model_klass = self
14
14
 
15
15
  obj.exhibitor(variant)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Exposant
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
data/lib/exposant.rb CHANGED
@@ -4,8 +4,8 @@ require 'delegate'
4
4
  require 'active_support'
5
5
  require 'active_model'
6
6
 
7
- require_relative 'exposant/concerns/presentable'
8
7
  require_relative 'exposant/concerns/exhibitor'
8
+ require_relative 'exposant/concerns/exposable'
9
9
  require_relative 'exposant/collection_exhibitor'
10
10
  require_relative 'exposant/model_exhibitor'
11
11
  require_relative 'exposant/version'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exposant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Kienlen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-27 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -56,7 +56,7 @@ files:
56
56
  - lib/exposant.rb
57
57
  - lib/exposant/collection_exhibitor.rb
58
58
  - lib/exposant/concerns/exhibitor.rb
59
- - lib/exposant/concerns/presentable.rb
59
+ - lib/exposant/concerns/exposable.rb
60
60
  - lib/exposant/model_exhibitor.rb
61
61
  - lib/exposant/version.rb
62
62
  homepage: http://127.0.0.1:3000