Skip to content

Commit 75a162b

Browse files
authored
Add support for options to Daru module (#506)
* Add support for options to Daru module #476 #502 * Use the max_rows option on the terminal * fix typo in table formatter
1 parent ba5992c commit 75a162b

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

lib/daru.rb

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ def jruby?
55
# :nocov:
66

77
module Daru
8+
89
DAYS_OF_WEEK = {
910
'SUN' => 0,
1011
'MON' => 1,
@@ -102,6 +103,7 @@ def error msg
102103
require 'daru/index/categorical_index.rb'
103104

104105
require 'daru/helpers/array.rb'
106+
require 'daru/configuration.rb'
105107
require 'daru/vector.rb'
106108
require 'daru/dataframe.rb'
107109
require 'daru/monkeys.rb'

lib/daru/configuration.rb

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Daru
2+
# Defines constants and methods related to configuration
3+
module Configuration
4+
5+
INSPECT_OPTIONS_KEYS = [
6+
:max_rows,
7+
8+
# Terminal
9+
:spacing,
10+
]
11+
12+
# Jupyter
13+
DEFAULT_MAX_ROWS = 30
14+
15+
# Terminal
16+
DEFAULT_SPACING = 10
17+
18+
attr_accessor(*INSPECT_OPTIONS_KEYS)
19+
20+
def configure
21+
yield self
22+
end
23+
24+
def self.extended(base)
25+
base.reset_options
26+
end
27+
28+
def reset_options
29+
self.max_rows = DEFAULT_MAX_ROWS
30+
31+
self.spacing = DEFAULT_SPACING
32+
end
33+
end
34+
35+
extend Configuration
36+
end

lib/daru/dataframe.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class DataFrame # rubocop:disable Metrics/ClassLength
1212
# TODO: Remove this line but its causing erros due to unkown reason
1313
Daru.has_nyaplot?
1414

15+
attr_accessor(*Configuration::INSPECT_OPTIONS_KEYS)
16+
1517
extend Gem::Deprecate
1618

1719
class << self
@@ -2120,7 +2122,7 @@ def to_h
21202122
end
21212123

21222124
# Convert to html for IRuby.
2123-
def to_html(threshold=30)
2125+
def to_html(threshold = Daru.max_rows)
21242126
table_thead = to_html_thead
21252127
table_tbody = to_html_tbody(threshold)
21262128
path = if index.is_a?(MultiIndex)
@@ -2141,7 +2143,8 @@ def to_html_thead
21412143
ERB.new(File.read(table_thead_path).strip).result(binding)
21422144
end
21432145

2144-
def to_html_tbody(threshold=30)
2146+
def to_html_tbody(threshold = Daru.max_rows)
2147+
threshold ||= @size
21452148
table_tbody_path =
21462149
if index.is_a?(MultiIndex)
21472150
File.expand_path('../iruby/templates/dataframe_mi_tbody.html.erb', __FILE__)
@@ -2258,7 +2261,7 @@ def transpose
22582261
end
22592262

22602263
# Pretty print in a nice table format for the command line (irb/pry/iruby)
2261-
def inspect spacing=10, threshold=15
2264+
def inspect spacing=Daru.spacing, threshold=Daru.max_rows
22622265
name_part = @name ? ": #{@name} " : ''
22632266

22642267
"#<#{self.class}#{name_part}(#{nrows}x#{ncols})>\n" +

lib/daru/formatters/table.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,10 @@ def initialize(data, headers, row_headers)
1313
@row_headers = [''] * @data.to_a.size if @row_headers.empty?
1414
end
1515

16-
DEFAULT_SPACING = 10
17-
DEFAULT_THRESHOLD = 15
18-
1916
def format threshold=nil, spacing=nil
20-
rows = build_rows(threshold || DEFAULT_THRESHOLD)
17+
rows = build_rows(threshold || Daru.max_rows)
2118

22-
formatter = construct_formatter rows, spacing || DEFAULT_SPACING
19+
formatter = construct_formatter rows, spacing || Daru.spacing
2320

2421
rows.map { |r| formatter % r }.join("\n")
2522
end

0 commit comments

Comments
 (0)