Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add struct formatter #10

Merged
merged 6 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Style/SymbolArray:
EnforcedStyle: brackets
Style/WordArray:
EnforcedStyle: brackets
Style/OpenStructUse:
Enabled: false
Metrics/BlockLength:
Exclude:
- 'spec/**/*_spec.rb'
Expand All @@ -15,3 +17,5 @@ Layout/EndAlignment:
EnforcedStyleAlignWith: variable
Layout/FirstArrayElementIndentation:
EnforcedStyle: consistent
Gemspec/DevelopmentDependencies:
Enabled: false
11 changes: 9 additions & 2 deletions lib/nazar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ module Nazar # rubocop:disable Metrics/ModuleLength

setting :enable_shorthand_method, default: true

class << self
class << self # rubocop:disable Metrics/ClassLength
def formatters
@formatters ||= Set.new
end

def enable!(extensions: [:active_record, :csv])
def enable!(extensions: [:active_record, :csv, :struct])
return if @enabled

load_extensions!(extensions)
Expand All @@ -67,6 +67,12 @@ def load_csv!
register_formatter!('CSVTable', 'nazar/formatter/csv_table')
end

def load_struct!
require 'ostruct'

register_formatter!('Struct', 'nazar/formatter/struct')
end

def load_active_record!
require 'active_record'

Expand Down Expand Up @@ -113,6 +119,7 @@ def disable!
def load_extensions!(extensions)
load_active_record! if extensions.include?(:active_record)
load_csv! if extensions.include?(:csv)
load_struct! if extensions.include?(:struct)
load_sequel! if extensions.include?(:sequel)
end

Expand Down
2 changes: 2 additions & 0 deletions lib/nazar/formatter/active_record_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def initialize(collection)
end

def self.valid?(data)
return false if data.is_a?(Struct) || (defined?(OpenStruct) && data.is_a?(OpenStruct))

data.is_a?(ActiveRecord::Associations::CollectionProxy) ||
data.is_a?(ActiveRecord::Relation) ||
(data.is_a?(Enumerable) && data.first.is_a?(ActiveRecord::Base))
Expand Down
6 changes: 3 additions & 3 deletions lib/nazar/formatter/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def summary
end

def self.valid?(data)
item = data&.first
compatible = item.respond_to?(:keys) && item.respond_to?(:values)
return false unless data.is_a?(Enumerable)

data.is_a?(Enumerable) && (item.is_a?(Struct) || compatible)
item = data&.first
item.respond_to?(:keys) && item.respond_to?(:values)
end

def valid?
Expand Down
33 changes: 33 additions & 0 deletions lib/nazar/formatter/struct.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

module Nazar
module Formatter
class Struct
def initialize(item)
@collection = Array(item)
@attributes = item.to_h.keys
@item = item
end

def self.valid?(data)
data.is_a?(::Struct) || data.is_a?(::OpenStruct)
end

def valid?
true
end

def headers
HeadersFormatter.new(attributes).format
end

def cells
@cells ||= @collection.map do |item|
item.each_pair do |_, value|
CellFormatter.new(value, type: nil).format
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/nazar/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Nazar
VERSION = '1.2.0'
VERSION = '1.3.0'
end
24 changes: 24 additions & 0 deletions spec/nazar/view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@
end
end
end

context 'with Struct' do
let(:data) { Struct.new(:id, :name).new(1, 'foo') }

context 'without loaded extension' do
it do
expect(subject).not_to be_supported_data
end
end

context 'with loaded extension' do
before do
Nazar.load_struct!
end

after do
unload_struct!
end

it do
expect(subject).to be_supported_data
end
end
end
end

describe '#render' do
Expand Down
4 changes: 2 additions & 2 deletions spec/nazar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

context 'when Pry is defined' do
it do
::Pry = double
::Pry = double # rubocop:disable Style/RedundantConstantBase

expect(Nazar).to receive(:enable_for_pry!)
expect(Nazar).not_to receive(:enable_for_irb!)
Expand All @@ -26,7 +26,7 @@

context 'when IRB is defined' do
it do
::IRB = double
::IRB = double # rubocop:disable Style/RedundantConstantBase

expect(Nazar).not_to receive(:enable_for_pry!)
expect(Nazar).to receive(:enable_for_irb!)
Expand Down
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ def unload_sequel!
Nazar.formatters.delete(Nazar::Formatter::SequelCollection)
Nazar.formatters.delete(Nazar::Formatter::SequelItem)
end

def unload_struct!
Nazar.formatters.delete(Nazar::Formatter::Struct)
end
end
end

Expand Down
Loading