nazar 0.0.9 → 0.1.1

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: 7204b0e6f3c925d89b8d8ac8806b938fa88d73c21a8c9d7cd71236578968960a
4
- data.tar.gz: 40f2762c75425f5b0be957b60ba0812f65dcb4e2fce1fbb05e4d9f4c8314c12f
3
+ metadata.gz: f3ea71c523b3f82b6882bdcb11b842e53b4f2283bc95d1ab5e8c0e17e2b1c9b6
4
+ data.tar.gz: 97017a09da4032dd35b90e34cd49cf92e96473f7c1f001a8f9ffab1c93ae6d2f
5
5
  SHA512:
6
- metadata.gz: 040ab5047820966aef1db0f65478ffc581849d1d83a9f54ae39c463908c71d016f847391378b259f3098ffddc87c14ab673f1d40e5b3b5b887e162685798eff1
7
- data.tar.gz: 163d9c68f8f6f0c27d2a6d4eff9f9f89924d706a69e6d2acf37285afe1599f30d35151e0728b43dbec4253b8aaab8dba5d5de716de18d62b56c032f53da90212
6
+ metadata.gz: cb511b109dc83d28057e6454a5d438e799cfbb0a6eb64d2f0c55ae53dd8e92c19f0c78b4a361452307e41999738972260e2b5b4b6dc347c05ddaf81e49b452bd
7
+ data.tar.gz: 5c0f4e67fea1a380606dd75bac52de072ac9851f70b844ee8ba1464c73592e49cde3f0449fcb208cdb912b938e067fce4a8af0a7d5f7076e244e6b78d2cfa336
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -1,9 +1,5 @@
1
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
- Nazar improves defvault inspect output for console applications (supports IRB and Pry).
4
-
5
- **Nazar is under heavy development now. Expect bumpy ride or wait until the API will stabilise a bit ;)**
6
-
7
3
  Turn this:
8
4
  ```ruby
9
5
  >> User.all
@@ -67,7 +63,8 @@ Then, you have to call `Nazar.enable!`. If you're using Rails, you might want to
67
63
  console do
68
64
  require 'nazar'
69
65
 
70
- Nazar.enable! # see configuration section for more options
66
+ Nazar.enable! # See configuration section for more options and
67
+ # Opt-in setup section if you don't want to enable it for every item
71
68
  end
72
69
  ```
73
70
  Otherwise, call it in `bin/console` or any other script that launches your REPL.
@@ -104,11 +101,45 @@ end
104
101
  Nazar.enable! if defined?(Nazar)
105
102
  ```
106
103
 
104
+ ## Shorthand method
105
+
106
+ Nazar defines top level `__(data)` method, that can be useful for two things:
107
+
108
+ ### Inspecting (array of) Hashes/Structs
109
+
110
+ Nazar by design won't enhance output for any collection that does not consist of supported (eg. ActiveRecord/Sequel) items. If you have a data that is structurally compatible (ie: all items consists of the same keys), and want to enahance the output, you can simply use `#__` method:
111
+
112
+ ```ruby
113
+ __ [{foo: :bar, test: 123},{foo: :baz, test: 456}]
114
+ ┏━━━━━━━┯━━━━━━┓
115
+ ┃ foo │ test ┃
116
+ ┣═══════╪══════┫
117
+ ┃ :bar │ 123 ┃
118
+ ┃ :baz │ 456 ┃
119
+ ┠───────┼──────┨
120
+ ┃ Total │ 2 ┃
121
+ ┗━━━━━━━┷━━━━━━┛
122
+ ```
123
+
124
+ It works with Structs and Hashes (or, more specifically - with any object that reponds to `#keys` and `#values`)
125
+
126
+ ### Opt-in setup
127
+
128
+ If you use `Nazar.load!` instead of `Nazar.enable!`, it would not enhance output for every element in the console. Instead, you have to call `#__` for each item that you want to enhance output
129
+
130
+ ```ruby
131
+
132
+ Nazar.load!
133
+ User.all # Returns default output
134
+ __ User.all # Returns output enhanced by Nazar
135
+ ```
136
+
137
+
107
138
  ## Configuration
108
139
 
109
140
  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
141
 
111
- `Nazar.enable!(extensions: [:active_record, :csv, :sequel])` will enhance outout for <a href="https://github.com/jeremyevans/sequel/">Sequel</a> as well.
142
+ `Nazar.enable!(extensions: [:active_record, :csv, :sequel])` will enhance output for <a href="https://github.com/jeremyevans/sequel/">Sequel</a> as well.
112
143
 
113
144
  You can also configure behaviour of Nazar:
114
145
 
@@ -117,7 +148,8 @@ You can also configure behaviour of Nazar:
117
148
  | `Nazar.config.colors.enabled` | true | Determines whether the output should be colorised or not |
118
149
  | `Nazar.config.formatter.nil` | `∅` | Sets character printed if the value was nil |
119
150
  | `Nazar.config.formatter.boolean` | ['✓', '✗'] | First item in array is a character for `true`, second for `false` |
120
-
151
+ | `Nazar.config.enable_shorthand_method` | true | Determines if shorthand method should be defined. See <a href="#opt-in-setup">Opt-in setup</a> for more details |
152
+
121
153
  ## Contributing
122
154
 
123
155
  Bug reports and pull requests are welcome on GitHub at https://github.com/krzyzak/nazar.
@@ -10,11 +10,11 @@ module Nazar
10
10
  def format
11
11
  case type
12
12
  when :boolean
13
- format_boolean(value)
13
+ format_boolean
14
14
  when :integer
15
- Pastel.new(enabled: Nazar.config.colors.enabled).bright_blue(value)
15
+ format_number
16
16
  else
17
- value.nil? ? format_nil : value.to_s
17
+ value.nil? ? format_nil : inteligent_format
18
18
  end
19
19
  end
20
20
 
@@ -22,7 +22,7 @@ module Nazar
22
22
 
23
23
  attr_reader :value, :type
24
24
 
25
- def format_boolean(value)
25
+ def format_boolean
26
26
  return format_nil if value.nil?
27
27
 
28
28
  true_value, false_value = Nazar.config.formatter.boolean
@@ -30,7 +30,26 @@ module Nazar
30
30
  end
31
31
 
32
32
  def format_nil
33
- Pastel.new(enabled: Nazar.config.colors.enabled).dim(Nazar.config.formatter.nil)
33
+ pastel.dim(Nazar.config.formatter.nil)
34
+ end
35
+
36
+ def format_number
37
+ pastel.bright_blue(value)
38
+ end
39
+
40
+ def inteligent_format
41
+ case value
42
+ when Symbol
43
+ pastel.magenta(":#{value}")
44
+ when Numeric
45
+ format_number
46
+ else
47
+ value.to_s
48
+ end
49
+ end
50
+
51
+ def pastel
52
+ @pastel ||= Pastel.new(enabled: Nazar.config.colors.enabled)
34
53
  end
35
54
  end
36
55
  end
@@ -18,7 +18,7 @@ module Nazar
18
18
  def self.valid?(data)
19
19
  data.is_a?(ActiveRecord::Associations::CollectionProxy) ||
20
20
  data.is_a?(ActiveRecord::Relation) ||
21
- (data.is_a?(Array) && data.first.is_a?(ActiveRecord::Base))
21
+ (data.is_a?(Enumerable) && data.first.is_a?(ActiveRecord::Base))
22
22
  end
23
23
 
24
24
  def summary
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nazar
4
+ module Formatter
5
+ class Generic
6
+ attr_reader :collection
7
+
8
+ def initialize(collection)
9
+ @collection = Array(collection)
10
+ @item = collection.first
11
+ end
12
+
13
+ def headers
14
+ HeadersFormatter.new(raw_headers).format
15
+ end
16
+
17
+ def cells
18
+ @cells ||= collection.map do |item|
19
+ item.values.map do |value|
20
+ CellFormatter.new(value).format
21
+ end
22
+ end
23
+ end
24
+
25
+ def summary
26
+ collection.size
27
+ end
28
+
29
+ def self.valid?(data)
30
+ item = data.first
31
+ compatible = item.respond_to?(:keys) && item.respond_to?(:values)
32
+
33
+ data.is_a?(Enumerable) && (item.is_a?(Struct) || compatible)
34
+ end
35
+
36
+ def valid?
37
+ !!item && !raw_headers.empty?
38
+ end
39
+
40
+ private
41
+
42
+ attr_reader :item
43
+
44
+ def raw_headers
45
+ @raw_headers ||= if item.is_a?(Struct)
46
+ item.members
47
+ elsif item.respond_to?(:keys)
48
+ item.keys
49
+ else
50
+ []
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -13,7 +13,7 @@ module Nazar
13
13
  end
14
14
 
15
15
  def self.valid?(data)
16
- data.is_a?(Array) && data.first.is_a?(Sequel::Model)
16
+ (data.is_a?(Enumerable) && data.first.is_a?(Sequel::Model)) || data.is_a?(Sequel::Dataset)
17
17
  end
18
18
 
19
19
  def summary
@@ -2,8 +2,9 @@
2
2
 
3
3
  module Nazar
4
4
  class Renderer
5
- def initialize(data)
5
+ def initialize(data, use_generic_formatter: false)
6
6
  @data = data
7
+ @use_generic_formatter = use_generic_formatter
7
8
  end
8
9
 
9
10
  def render
@@ -24,7 +25,7 @@ module Nazar
24
25
  attr_reader :data
25
26
 
26
27
  def view
27
- @view ||= View.new(data)
28
+ @view ||= View.new(data, use_generic_formatter: @use_generic_formatter)
28
29
  end
29
30
  end
30
31
  end
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.9'
4
+ VERSION = '0.1.1'
5
5
  end
data/lib/nazar/view.rb CHANGED
@@ -6,8 +6,9 @@ module Nazar
6
6
 
7
7
  def_delegators :formatter, :headers, :cells, :summary
8
8
 
9
- def initialize(data)
9
+ def initialize(data, use_generic_formatter: false)
10
10
  @data = data
11
+ @use_generic_formatter = use_generic_formatter
11
12
  end
12
13
 
13
14
  def render
@@ -24,10 +25,16 @@ module Nazar
24
25
 
25
26
  private
26
27
 
27
- attr_reader :data
28
+ attr_reader :data, :use_generic_formatter
29
+
30
+ def formatters
31
+ @formatters ||= Nazar.formatters.tap do |formatters|
32
+ formatters << Nazar::Formatter::Generic if use_generic_formatter
33
+ end
34
+ end
28
35
 
29
36
  def formatter_klass
30
- @formatter_klass ||= Nazar.formatters.find { |klass| klass.valid?(data) }
37
+ @formatter_klass ||= formatters.find { |klass| klass.valid?(data) }
31
38
  end
32
39
 
33
40
  def formatter
data/lib/nazar.rb CHANGED
@@ -10,9 +10,10 @@ require 'nazar/cell_formatter'
10
10
  require 'nazar/headers_formatter'
11
11
  require 'nazar/renderer'
12
12
  require 'nazar/formatter'
13
+ require 'nazar/formatter/generic'
13
14
  require 'nazar/view'
14
15
 
15
- module Nazar
16
+ module Nazar # rubocop:disable Metrics/ModuleLength
16
17
  extend Dry::Configurable
17
18
 
18
19
  setting :formatter do
@@ -24,6 +25,8 @@ module Nazar
24
25
  setting :enabled, default: ENV.fetch('ENABLE_TTY_COLORS') { TTY::Color.color? ? 'true' : 'false' } == 'true'
25
26
  end
26
27
 
28
+ setting :enable_shorthand_method, default: true
29
+
27
30
  class << self
28
31
  def formatters
29
32
  @formatters ||= Set.new
@@ -32,16 +35,21 @@ module Nazar
32
35
  def enable!(extensions: [:active_record, :csv])
33
36
  return if @enabled
34
37
 
35
- load_active_record! if extensions.include?(:active_record)
36
- load_csv! if extensions.include?(:csv)
37
- load_sequel! if extensions.include?(:sequel)
38
+ load_extensions!(extensions)
39
+ enable_repl!
38
40
 
39
- enable_for_irb! if defined?(IRB)
40
- enable_for_pry! if defined?(Pry)
41
+ enable_shorthand_method! if Nazar.config.enable_shorthand_method
41
42
 
42
43
  @enabled = true
43
44
  end
44
45
 
46
+ def load!(extensions: [:active_record, :csv])
47
+ load_extensions!(extensions)
48
+ enable_shorthand_method! if Nazar.config.enable_shorthand_method
49
+
50
+ true
51
+ end
52
+
45
53
  def load_csv!
46
54
  require 'csv'
47
55
 
@@ -78,6 +86,8 @@ module Nazar
78
86
  end
79
87
 
80
88
  def disable!
89
+ disable_shorthand_method! if @defined_shorthand_method
90
+
81
91
  return unless @enabled
82
92
 
83
93
  disable_for_irb! if defined?(IRB)
@@ -88,6 +98,17 @@ module Nazar
88
98
 
89
99
  private
90
100
 
101
+ def load_extensions!(extensions)
102
+ load_active_record! if extensions.include?(:active_record)
103
+ load_csv! if extensions.include?(:csv)
104
+ load_sequel! if extensions.include?(:sequel)
105
+ end
106
+
107
+ def enable_repl!
108
+ enable_for_irb! if defined?(IRB)
109
+ enable_for_pry! if defined?(Pry)
110
+ end
111
+
91
112
  def enable_for_irb!
92
113
  ::IRB::Irb.class_eval do
93
114
  alias_method :__original_output_value__, :output_value
@@ -108,6 +129,28 @@ module Nazar
108
129
  end
109
130
  end
110
131
 
132
+ def enable_shorthand_method!
133
+ if Object.respond_to?(:__)
134
+ return warn Pastel.new.red("Already defined Object#__() method, Nazar won't redefine it.")
135
+ end
136
+
137
+ @defined_shorthand_method = true
138
+
139
+ Object.class_eval do
140
+ def __(item)
141
+ Nazar::Renderer.new(item, use_generic_formatter: true).render
142
+ end
143
+ end
144
+ end
145
+
146
+ def disable_shorthand_method!
147
+ @defined_shorthand_method = nil
148
+
149
+ Object.class_eval do
150
+ undef_method :__
151
+ end
152
+ end
153
+
111
154
  def disable_for_irb!
112
155
  ::IRB::Irb.send(:alias_method, :output_value, :__original_output_value__)
113
156
  end
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.9
4
+ version: 0.1.1
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-24 00:00:00.000000000 Z
39
+ date: 2021-10-06 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
@@ -195,6 +195,7 @@ files:
195
195
  - lib/nazar/formatter/active_record_interface.rb
196
196
  - lib/nazar/formatter/active_record_item.rb
197
197
  - lib/nazar/formatter/csv_table.rb
198
+ - lib/nazar/formatter/generic.rb
198
199
  - lib/nazar/formatter/sequel_collection.rb
199
200
  - lib/nazar/formatter/sequel_interface.rb
200
201
  - lib/nazar/formatter/sequel_item.rb
metadata.gz.sig CHANGED
Binary file