nazar 0.0.4 → 0.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc538e2fec48e0daf00bd670d1c5aa204f9caa6a045397cfcf32e92d433b379f
4
- data.tar.gz: 17658ef498b4c1f9f14a8ba91e3ad53aa3de347a95302042647b8e30decd6d91
3
+ metadata.gz: 7204b0e6f3c925d89b8d8ac8806b938fa88d73c21a8c9d7cd71236578968960a
4
+ data.tar.gz: 40f2762c75425f5b0be957b60ba0812f65dcb4e2fce1fbb05e4d9f4c8314c12f
5
5
  SHA512:
6
- metadata.gz: bdd620ad635fdfde0337d339c28b570e412bbd7d7d1e6701f76cf9ad028eb92c22a77e013ad7ef7e460dd1114e2e9ffb66039ca18276455a255445ce156ccc1c
7
- data.tar.gz: 9737cb971cc7a46118640e352e90745915101f409ba5297b432e046343715e53261f17c4bad80f332ec1878ff2e8911742be5f6bdefe42e50fa280f3674187fd
6
+ metadata.gz: 040ab5047820966aef1db0f65478ffc581849d1d83a9f54ae39c463908c71d016f847391378b259f3098ffddc87c14ab673f1d40e5b3b5b887e162685798eff1
7
+ data.tar.gz: 163d9c68f8f6f0c27d2a6d4eff9f9f89924d706a69e6d2acf37285afe1599f30d35151e0728b43dbec4253b8aaab8dba5d5de716de18d62b56c032f53da90212
checksums.yaml.gz.sig CHANGED
Binary file
@@ -35,4 +35,8 @@ jobs:
35
35
  ruby-version: 2.7
36
36
  bundler-cache: true
37
37
  - name: Specs
38
- run: bundle exec rspec
38
+ uses: paambaati/codeclimate-action@v2.7.5
39
+ env:
40
+ CC_TEST_REPORTER_ID: ${{secrets.CC_TEST_REPORTER_ID}}
41
+ with:
42
+ coverageCommand: bundle exec rspec
data/README.md CHANGED
@@ -1,11 +1,123 @@
1
- # Nazar [![Build Status](https://github.com/krzyzak/nazar/actions/workflows/main.yml/badge.svg)](https://github.com/krzyzak/nazar/actions) [![Code Climate](https://codeclimate.com/github/krzyzak/nazar/badges/gpa.svg)](https://codeclimate.com/github/krzyzak/nazar)
1
+ # Nazar [![Build Status](https://github.com/krzyzak/nazar/actions/workflows/main.yml/badge.svg)](https://github.com/krzyzak/nazar/actions) [![Gem Version](https://badge.fury.io/rb/nazar.svg)](https://badge.fury.io/rb/nazar) [![Code Climate](https://codeclimate.com/github/krzyzak/nazar/badges/gpa.svg)](https://codeclimate.com/github/krzyzak/nazar) [![Test Coverage](https://api.codeclimate.com/v1/badges/f89b8bfdf557900e1f9f/test_coverage)](https://codeclimate.com/github/krzyzak/nazar/test_coverage)
2
2
 
3
3
  Nazar improves defvault inspect output for console applications (supports IRB and Pry).
4
4
 
5
5
  **Nazar is under heavy development now. Expect bumpy ride or wait until the API will stabilise a bit ;)**
6
6
 
7
- [![asciicast](https://asciinema.org/a/436814.png)](https://asciinema.org/a/436814)
7
+ Turn this:
8
+ ```ruby
9
+ >> User.all
10
+ User Load (0.4ms) SELECT "users".* FROM "users" /* loading for inspect */ LIMIT ? [["LIMIT", 11]]
11
+ => #<ActiveRecord::Relation [#<User id: 1, email: "delma@goldner.co", name: "Mrs. Charis Brown", description: "Quasi atque ea. Beatae corporis quia. Eveniet veli...", created_at: "2021-09-01 05:42:24.341344000 +0000", updated_at: "2021-09-20 09:36:29.289078000 +0000", active: false, last_login_at: nil>, #<User id: 2, email: "markpfeffer@wunschbraun.info", name: "Ms. Sherice Champlin", description: "Omnis ut illum. Non velit mollitia. Expedita facil...", created_at: "2021-09-10 07:27:00.182298000 +0000", updated_at: "2021-09-20 09:36:29.292065000 +0000", active: true, last_login_at: "2021-09-19 16:25:32.094418000 +0000">, #<User id: 3, email: "nathansimonis@kunde.org", name: "Florentino Gleason", description: "Non recusandae eos. Et voluptates iusto. Commodi e...", created_at: "2021-09-07 18:24:45.608164000 +0000", updated_at: "2021-09-20 09:36:29.293572000 +0000", active: false, last_login_at: nil>, #<User id: 4, email: "edwardowilkinson@walkerschiller.info", name: "Judie Boehm", description: "Ut ea eum. Tempore voluptates praesentium. Animi s...", created_at: "2021-09-05 02:11:36.564693000 +0000", updated_at: "2021-09-20 09:36:29.295763000 +0000", active: true, last_login_at: "2021-09-19 20:17:24.074515000 +0000">, #<User id: 5, email: "isaiahebert@brauncarroll.net", name: "Mari Ullrich", description: "Aperiam quas voluptas. Autem alias quia. Aut persp...", created_at: "2021-09-09 05:23:03.229860000 +0000", updated_at: "2021-09-20 09:36:29.296928000 +0000", active: nil, last_login_at: nil>, #<User id: 6, email: "alexander@anderson.io", name: "Sam Larson DDS", description: "Sed velit consectetur. Quas cupiditate enim. Neque...", created_at: "2021-09-03 02:52:14.055696000 +0000", updated_at: "2021-09-20 09:36:29.299157000 +0000", active: true, last_login_at: "2021-09-20 05:01:39.019563000 +0000">, #<User id: 7, email: "chirunolfon@keler.io", name: "Ying Kub I", description: "Cupiditate ut consequatur. Eos ab laboriosam. Ipsa...", created_at: "2021-09-10 05:52:32.005877000 +0000", updated_at: "2021-09-20 09:36:29.301469000 +0000", active: true, last_login_at: "2021-09-19 21:30:48.854010000 +0000">, #<User id: 8, email: "johnny@anderson.net", name: "Johnie Corwin II", description: "Suscipit itaque adipisci. Assumenda ad error. Quas...", created_at: "2021-09-12 13:05:49.845361000 +0000", updated_at: "2021-09-20 09:36:29.302657000 +0000", active: nil, last_login_at: nil>, #<User id: 9, email: "yahairakuhn@schneider.com", name: "Sharie Weber", description: "Sapiente voluptatum mollitia. Dolores voluptatum a...", created_at: "2021-09-19 03:03:39.360526000 +0000", updated_at: "2021-09-20 09:36:29.304929000 +0000", active: true, last_login_at: "2021-09-20 06:46:05.846685000 +0000">, #<User id: 10, email: "tommye@schuppe.biz", name: "Yoshiko Mohr", description: "Similique laborum totam. Non cum atque. Placeat qu...", created_at: "2021-09-08 05:41:36.588541000 +0000", updated_at: "2021-09-20 09:36:29.306120000 +0000", active: nil, last_login_at: nil>, ...]>
12
+ ```
13
+ Into this:
8
14
 
15
+ ```ruby
16
+ >> User.all
17
+ (0.6ms) SELECT sqlite_version(*)
18
+ User Load (0.8ms) SELECT "users".* FROM "users"
19
+ ┏━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┓
20
+ ┃ id │ email │ name │ description │ created_at │ updated_at │ active │ last_login_at ┃
21
+ ┣═══════╪══════════════════════════════════════╪═══════════════════════╪═════════════════════════════════════════════════════════════════════════════════════════╪═════════════════════════╪═════════════════════════╪════════╪═════════════════════════┫
22
+ ┃ 1 │ delma@goldner.co │ Mrs. Charis Brown │ Quasi atque ea. Beatae corporis quia. Eveniet velit ad. │ 2021-09-01 05:42:24 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
23
+ ┃ 2 │ markpfeffer@wunschbraun.info │ Ms. Sherice Champlin │ Omnis ut illum. Non velit mollitia. Expedita facilis dignissimos. │ 2021-09-10 07:27:00 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-19 16:25:32 UTC ┃
24
+ ┃ 3 │ nathansimonis@kunde.org │ Florentino Gleason │ Non recusandae eos. Et voluptates iusto. Commodi est in. │ 2021-09-07 18:24:45 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
25
+ ┃ 4 │ edwardowilkinson@walkerschiller.info │ Judie Boehm │ Ut ea eum. Tempore voluptates praesentium. Animi sapiente distinctio. │ 2021-09-05 02:11:36 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-19 20:17:24 UTC ┃
26
+ ┃ 5 │ isaiahebert@brauncarroll.net │ Mari Ullrich │ Aperiam quas voluptas. Autem alias quia. Aut perspiciatis eos. │ 2021-09-09 05:23:03 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
27
+ ┃ 6 │ alexander@anderson.io │ Sam Larson DDS │ Sed velit consectetur. Quas cupiditate enim. Neque facere harum. │ 2021-09-03 02:52:14 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-20 05:01:39 UTC ┃
28
+ ┃ 7 │ chirunolfon@keler.io │ Ying Kub I │ Cupiditate ut consequatur. Eos ab laboriosam. Ipsam ut veritatis. │ 2021-09-10 05:52:32 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-19 21:30:48 UTC ┃
29
+ ┃ 8 │ johnny@anderson.net │ Johnie Corwin II │ Suscipit itaque adipisci. Assumenda ad error. Quas enim beatae. │ 2021-09-12 13:05:49 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
30
+ ┃ 9 │ yahairakuhn@schneider.com │ Sharie Weber │ Sapiente voluptatum mollitia. Dolores voluptatum accusamus. Veniam voluptatem deserunt. │ 2021-09-19 03:03:39 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-20 06:46:05 UTC ┃
31
+ ┃ 10 │ tommye@schuppe.biz │ Yoshiko Mohr │ Similique laborum totam. Non cum atque. Placeat quia velit. │ 2021-09-08 05:41:36 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
32
+ ┃ 11 │ derekbeier@hermankuhlman.co │ Lavern Feil │ Itaque molestias et. Iure tempora minus. Dolore autem omnis. │ 2021-09-05 04:04:51 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
33
+ ┃ 12 │ lavernwillms@runolfsdottirbogan.org │ Marcy Gottlieb Sr. │ Mollitia quia dolor. Libero laborum aperiam. Velit est eum. │ 2021-09-08 19:22:49 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
34
+ ┃ 13 │ earlhills@mclaughlingreenholt.info │ Adina Luettgen │ In qui rem. Illum itaque dignissimos. Enim vitae tenetur. │ 2021-09-08 10:16:12 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
35
+ ┃ 14 │ larry@rice.com │ Carolina Kuhlman II │ Ipsum ut fuga. Sunt id a. Rerum nulla sint. │ 2021-09-03 23:11:42 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
36
+ ┃ 15 │ randy@kuhicfritsch.name │ Harvey Runolfsdottir │ Dolorum officiis sed. Ut recusandae culpa. Odit aut nostrum. │ 2021-09-08 04:28:38 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
37
+ ┃ 16 │ lashaun@mooreleannon.co │ Lucio Raynor │ Et expedita rerum. Autem nesciunt labore. Suscipit aut eum. │ 2021-09-06 20:47:51 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
38
+ ┃ 17 │ omeryost@becker.com │ Rocky Hettinger │ Ut deleniti voluptas. Explicabo voluptas rerum. Illum sapiente quo. │ 2021-09-14 23:05:33 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-20 09:35:14 UTC ┃
39
+ ┃ 18 │ nilsa@corwinkub.name │ Serena Brakus │ Qui non provident. Omnis excepturi quasi. Ratione suscipit dolor. │ 2021-09-14 01:06:02 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
40
+ ┃ 19 │ berniebednar@spinkamiller.co │ Merlin Predovic │ Omnis et minus. Tempore in laboriosam. Molestiae aut eveniet. │ 2021-09-16 18:34:20 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-19 12:01:59 UTC ┃
41
+ ┃ 20 │ claude@smithamhaley.io │ Claudette Thompson MD │ Aut consectetur soluta. Laborum quia qui. Vel incidunt minima. │ 2021-09-10 03:53:24 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
42
+ ┃ 21 │ matilde@cormierstroman.name │ Dalton Witting │ Voluptatum provident earum. Ad esse odit. Quasi non ut. │ 2021-09-10 04:51:53 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
43
+ ┃ 22 │ randistrosin@littel.io │ Miss Lucio Kub │ Ea quia sapiente. Reprehenderit esse ut. Veritatis magnam quia. │ 2021-08-31 18:33:02 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
44
+ ┃ 23 │ noah@kuphal.co │ Alonzo Stroman │ Officia fugit eos. Qui quo et. Ducimus harum dignissimos. │ 2021-09-13 14:36:31 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
45
+ ┃ 24 │ cristinaschneider@cole.biz │ Dr. Newton Ritchie │ Corporis suscipit recusandae. Voluptas voluptas quo. Non perferendis fugiat. │ 2021-09-02 20:54:38 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-20 02:35:39 UTC ┃
46
+ ┃ 25 │ mirtha@spinkabayer.co │ Kory Cummings │ Aut ut aut. Quis voluptas sit. Consequatur quia voluptatibus. │ 2021-09-17 08:45:02 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
47
+ ┠───────┼──────────────────────────────────────┴───────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────┴─────────────────────────┴────────┴─────────────────────────┨
48
+ ┃ Total │ 25 ┃
49
+ ┗━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
50
+ ```
51
+
52
+ ## Installation
53
+
54
+ ### Per-project installation
55
+
56
+ While nothing stops you from adding Nazar to each project separately, I find it cumbersome to add it to every project I'm working on - it also alters REPL behaviour for other teammates, which is another reason why personally I don't recommend that approach. Anyway, here's quick how-to:
57
+
58
+ Add `nazar` to Gemfile:
59
+ ```ruby
60
+ group :development, :test do
61
+ gem 'nazar', require: false
62
+ end
63
+ ```
64
+ Then, you have to call `Nazar.enable!`. If you're using Rails, you might want to add this snippet to `config/application.rb`:
65
+
66
+ ```ruby
67
+ console do
68
+ require 'nazar'
69
+
70
+ Nazar.enable! # see configuration section for more options
71
+ end
72
+ ```
73
+ Otherwise, call it in `bin/console` or any other script that launches your REPL.
74
+
75
+ ### Global installation
76
+
77
+ This is my recommended way to install this gem - that way it works automatically in all your projects, even in standalone `irb` or `pry` sessions.
78
+
79
+ - **Create global gems path**
80
+
81
+ `mkdir -p ~/.gem/ruby/global` - You're welcome to use any other path for your global gemset
82
+ - **Install nazar in your global gemset**
83
+
84
+ `gem install nazar -i ~/.gem/ruby/global`
85
+ - **Load Nazar in your REPL rc file**
86
+ Add following snippet to your `.irbrc` or `.pryrc`:
87
+
88
+ ```ruby
89
+ global_gemset_path = File.expand_path('~/.gem/ruby/global/gems')
90
+
91
+ if Dir.exist?(global_gemset_path)
92
+ global_gems_path = Dir.glob("#{global_gemset_path}/*/lib")
93
+
94
+ $LOAD_PATH.unshift(*global_gems_path)
95
+ end
96
+
97
+ %w[nazar].each do |gem|
98
+ begin
99
+ require gem
100
+ rescue LoadError
101
+ end
102
+ end
103
+
104
+ Nazar.enable! if defined?(Nazar)
105
+ ```
106
+
107
+ ## Configuration
108
+
109
+ By default, Nazar improves output for ActiveRecord (both collections and a single item) and for CSV Tables. You can configure that by calling `#enable!` with optional argument:
110
+
111
+ `Nazar.enable!(extensions: [:active_record, :csv, :sequel])` will enhance outout for <a href="https://github.com/jeremyevans/sequel/">Sequel</a> as well.
112
+
113
+ You can also configure behaviour of Nazar:
114
+
115
+ | Config | Default | Description |
116
+ | ------ | ------- | ----------- |
117
+ | `Nazar.config.colors.enabled` | true | Determines whether the output should be colorised or not |
118
+ | `Nazar.config.formatter.nil` | `∅` | Sets character printed if the value was nil |
119
+ | `Nazar.config.formatter.boolean` | ['✓', '✗'] | First item in array is a character for `true`, second for `false` |
120
+
9
121
  ## Contributing
10
122
 
11
123
  Bug reports and pull requests are welcome on GitHub at https://github.com/krzyzak/nazar.
@@ -15,6 +15,12 @@ module Nazar
15
15
  @klass = collection.first&.class
16
16
  end
17
17
 
18
+ def self.valid?(data)
19
+ data.is_a?(ActiveRecord::Associations::CollectionProxy) ||
20
+ data.is_a?(ActiveRecord::Relation) ||
21
+ (data.is_a?(Array) && data.first.is_a?(ActiveRecord::Base))
22
+ end
23
+
18
24
  def summary
19
25
  collection.size
20
26
  end
@@ -13,6 +13,10 @@ module Nazar
13
13
  @klass = item.class
14
14
  end
15
15
 
16
+ def self.valid?(data)
17
+ data.is_a?(ActiveRecord::Base)
18
+ end
19
+
16
20
  def summary
17
21
  false
18
22
  end
@@ -9,8 +9,12 @@ module Nazar
9
9
  @collection = collection
10
10
  end
11
11
 
12
+ def self.valid?(data)
13
+ data.is_a?(CSV::Table)
14
+ end
15
+
12
16
  def valid?
13
- collection.present?
17
+ !!collection && !collection.empty?
14
18
  end
15
19
 
16
20
  def headers
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nazar/formatter/sequel_interface'
4
+
5
+ module Nazar
6
+ module Formatter
7
+ class SequelCollection
8
+ include SequelInterface
9
+
10
+ def initialize(collection)
11
+ @collection = collection
12
+ @attributes = collection.first&.values
13
+ end
14
+
15
+ def self.valid?(data)
16
+ data.is_a?(Array) && data.first.is_a?(Sequel::Model)
17
+ end
18
+
19
+ def summary
20
+ collection.size
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nazar
4
+ module Formatter
5
+ module SequelInterface
6
+ attr_reader :collection, :klass, :attributes
7
+
8
+ def valid?
9
+ !!attributes
10
+ end
11
+
12
+ def headers
13
+ HeadersFormatter.new(attributes.keys).format
14
+ end
15
+
16
+ def cells
17
+ @cells ||= begin
18
+ schema = collection.first.db_schema
19
+
20
+ collection.map do |item|
21
+ item.values.map do |column, value|
22
+ CellFormatter.new(value, type: schema.dig(column, :type)).format
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def summary
29
+ collection.size
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'nazar/formatter/sequel_interface'
4
+
5
+ module Nazar
6
+ module Formatter
7
+ class SequelItem
8
+ include SequelInterface
9
+
10
+ def initialize(item)
11
+ @collection = [item]
12
+ @attributes = item.values
13
+ @klass = item.class
14
+ end
15
+
16
+ def self.valid?(data)
17
+ data.is_a?(Sequel::Model)
18
+ end
19
+
20
+ def summary
21
+ false
22
+ end
23
+ end
24
+ end
25
+ end
@@ -7,7 +7,12 @@ module Nazar
7
7
  end
8
8
 
9
9
  def render
10
- pager.page(view.render) if view.supported_data?
10
+ pager.page(view.render)
11
+ nil
12
+ end
13
+
14
+ def valid?
15
+ view.supported_data?
11
16
  end
12
17
 
13
18
  def pager
data/lib/nazar/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nazar
4
- VERSION = '0.0.4'
4
+ VERSION = '0.0.9'
5
5
  end
data/lib/nazar/view.rb CHANGED
@@ -27,42 +27,13 @@ module Nazar
27
27
  attr_reader :data
28
28
 
29
29
  def formatter_klass
30
- @formatter_klass ||= case data
31
- when acttive_record_collection?
32
- Formatter::ActiveRecordCollection
33
- when active_record_item?
34
- Formatter::ActiveRecordItem
35
- when csv_table?
36
- Formatter::CSVTable
37
- end
30
+ @formatter_klass ||= Nazar.formatters.find { |klass| klass.valid?(data) }
38
31
  end
39
32
 
40
33
  def formatter
41
34
  @formatter ||= formatter_klass.new(data)
42
35
  end
43
36
 
44
- def acttive_record_collection?
45
- return false unless Nazar.extensions.include?(:active_record)
46
-
47
- proc do
48
- data.is_a?(ActiveRecord::Associations::CollectionProxy) ||
49
- data.is_a?(ActiveRecord::Relation) ||
50
- (data.is_a?(Array) && data.first.is_a?(ActiveRecord::Base))
51
- end
52
- end
53
-
54
- def active_record_item?
55
- return false unless Nazar.extensions.include?(:active_record)
56
-
57
- proc { data.is_a?(ActiveRecord::Base) }
58
- end
59
-
60
- def csv_table?
61
- return false unless Nazar.extensions.include?(:csv)
62
-
63
- proc { data.is_a?(CSV::Table) }
64
- end
65
-
66
37
  def add_summary
67
38
  table.add_separator
68
39
  table.add_row [pastel.bold('Total'), { value: summary, colspan: headers.size - 1 }]
data/lib/nazar.rb CHANGED
@@ -25,14 +25,16 @@ module Nazar
25
25
  end
26
26
 
27
27
  class << self
28
- def extensions
29
- @extensions ||= Set.new
28
+ def formatters
29
+ @formatters ||= Set.new
30
30
  end
31
31
 
32
32
  def enable!(extensions: [:active_record, :csv])
33
33
  return if @enabled
34
34
 
35
- load_active_record! if extensions.include?(:active_record) || defined?(ActiveRecord)
35
+ load_active_record! if extensions.include?(:active_record)
36
+ load_csv! if extensions.include?(:csv)
37
+ load_sequel! if extensions.include?(:sequel)
36
38
 
37
39
  enable_for_irb! if defined?(IRB)
38
40
  enable_for_pry! if defined?(Pry)
@@ -41,33 +43,37 @@ module Nazar
41
43
  end
42
44
 
43
45
  def load_csv!
44
- extensions << :csv
45
-
46
46
  require 'csv'
47
- require 'nazar/formatter/csv_table'
47
+
48
+ register_formatter!('CSVTable', 'nazar/formatter/csv_table')
48
49
  end
49
50
 
50
51
  def load_active_record!
51
- extensions << :active_record
52
-
53
52
  require 'active_record'
54
- require 'nazar/formatter/active_record_collection'
55
- require 'nazar/formatter/active_record_item'
53
+
54
+ register_formatter!('ActiveRecordCollection', 'nazar/formatter/active_record_collection')
55
+ register_formatter!('ActiveRecordItem', 'nazar/formatter/active_record_item')
56
56
  end
57
57
 
58
- def enable_for_irb!
59
- ::IRB::Irb.class_eval do
60
- alias_method :__original_output_value__, :output_value
61
- def output_value(omit = false) # rubocop:disable Style/OptionalBooleanParameter
62
- ::Nazar::Renderer.new(@context.last_value).render || __original_output_value__(omit)
63
- end
64
- end
58
+ def load_sequel!
59
+ require 'sequel'
60
+
61
+ register_formatter!('SequelCollection', 'nazar/formatter/sequel_collection')
62
+ register_formatter!('SequelItem', 'nazar/formatter/sequel_item')
65
63
  end
66
64
 
67
- def enable_for_pry!
68
- @__original_pry_print = Pry.config.print
69
- Pry.config.print = proc do |output, value, instance|
70
- output.puts Nazar::Renderer.new(value).render || @__original_pry_print.call(output, value, instance)
65
+ def register_formatter!(klass_name, path)
66
+ require path
67
+
68
+ formatters << Nazar::Formatter.const_get(klass_name)
69
+ end
70
+
71
+ def pry_proc
72
+ return unless defined?(Pry)
73
+
74
+ proc do |output, value, instance|
75
+ renderer = Nazar::Renderer.new(value)
76
+ renderer.valid? ? renderer.render : @__original_pry_print.call(output, value, instance)
71
77
  end
72
78
  end
73
79
 
@@ -75,16 +81,43 @@ module Nazar
75
81
  return unless @enabled
76
82
 
77
83
  disable_for_irb! if defined?(IRB)
84
+ disable_for_pry! if defined?(Pry)
78
85
 
79
86
  @enabled = false
80
87
  end
81
88
 
89
+ private
90
+
91
+ def enable_for_irb!
92
+ ::IRB::Irb.class_eval do
93
+ alias_method :__original_output_value__, :output_value
94
+ def output_value(omit = false) # rubocop:disable Style/OptionalBooleanParameter
95
+ renderer = Nazar::Renderer.new(@context.last_value)
96
+ renderer.valid? ? renderer.render : __original_output_value__(omit)
97
+ end
98
+ end
99
+ end
100
+
101
+ def enable_for_pry!
102
+ @__original_pry_print ||= Pry.config.print
103
+
104
+ if Pry.toplevel_binding.local_variable_defined?(:pry_instance)
105
+ Pry.toplevel_binding.eval('pry_instance.config.print = Nazar.pry_proc', __FILE__, __LINE__)
106
+ else
107
+ Pry.config.print = Nazar.pry_proc
108
+ end
109
+ end
110
+
82
111
  def disable_for_irb!
83
112
  ::IRB::Irb.send(:alias_method, :output_value, :__original_output_value__)
84
113
  end
85
114
 
86
115
  def disable_for_pry!
87
- nil
116
+ Pry.toplevel_binding.eval(
117
+ 'pry_instance.config.print = Nazar.instance_variable_get(:@__original_pry_print)',
118
+ __FILE__,
119
+ __LINE__
120
+ )
88
121
  end
89
122
  end
90
123
  end
data/nazar.gemspec CHANGED
@@ -28,7 +28,9 @@ Gem::Specification.new do |spec|
28
28
 
29
29
  spec.add_development_dependency 'activerecord', '>= 3.0', '< 6.2'
30
30
  spec.add_development_dependency 'rubocop', '~> 1.21'
31
- spec.add_development_dependency 'sqlite3'
31
+ spec.add_development_dependency 'sequel', '~> 5.0'
32
+ spec.add_development_dependency 'simplecov', '~> 0.20'
33
+ spec.add_development_dependency 'sqlite3', '~> 1.0'
32
34
 
33
35
  spec.add_runtime_dependency 'dry-configurable', '~> 0.12'
34
36
  spec.add_runtime_dependency 'pastel', '~> 0.8'
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nazar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michał Krzyżanowski
@@ -36,7 +36,7 @@ cert_chain:
36
36
  FlwFUDGjWAe8lcsjyGp1dox0FK91TAHdZW1op8LYnOcO2DM8Mgzu4Gp7mibATEnx
37
37
  ooN2pwmH
38
38
  -----END CERTIFICATE-----
39
- date: 2021-09-21 00:00:00.000000000 Z
39
+ date: 2021-09-24 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
@@ -72,20 +72,48 @@ dependencies:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
74
  version: '1.21'
75
+ - !ruby/object:Gem::Dependency
76
+ name: sequel
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: simplecov
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '0.20'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '0.20'
75
103
  - !ruby/object:Gem::Dependency
76
104
  name: sqlite3
77
105
  requirement: !ruby/object:Gem::Requirement
78
106
  requirements:
79
- - - ">="
107
+ - - "~>"
80
108
  - !ruby/object:Gem::Version
81
- version: '0'
109
+ version: '1.0'
82
110
  type: :development
83
111
  prerelease: false
84
112
  version_requirements: !ruby/object:Gem::Requirement
85
113
  requirements:
86
- - - ">="
114
+ - - "~>"
87
115
  - !ruby/object:Gem::Version
88
- version: '0'
116
+ version: '1.0'
89
117
  - !ruby/object:Gem::Dependency
90
118
  name: dry-configurable
91
119
  requirement: !ruby/object:Gem::Requirement
@@ -167,6 +195,9 @@ files:
167
195
  - lib/nazar/formatter/active_record_interface.rb
168
196
  - lib/nazar/formatter/active_record_item.rb
169
197
  - lib/nazar/formatter/csv_table.rb
198
+ - lib/nazar/formatter/sequel_collection.rb
199
+ - lib/nazar/formatter/sequel_interface.rb
200
+ - lib/nazar/formatter/sequel_item.rb
170
201
  - lib/nazar/headers_formatter.rb
171
202
  - lib/nazar/renderer.rb
172
203
  - lib/nazar/version.rb
metadata.gz.sig CHANGED
Binary file