Skip to content

Commit 97d76e4

Browse files
authored
Add configurable classes list with to_s representation (#98)
Adds configuration option to convert custom class types to their string representation. Will resolve #97
1 parent 23b8555 commit 97d76e4

File tree

8 files changed

+46
-10
lines changed

8 files changed

+46
-10
lines changed

lib/annotate_rb/model_annotator/column_annotation/attributes_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AttributesBuilder
88
NO_DEFAULT_COL_TYPES = %w[json jsonb hstore].freeze
99

1010
def initialize(column, options, is_primary_key, column_indices, column_defaults)
11-
@column = ColumnWrapper.new(column, column_defaults)
11+
@column = ColumnWrapper.new(column, column_defaults, options)
1212
@options = options
1313
@is_primary_key = is_primary_key
1414
@column_indices = column_indices

lib/annotate_rb/model_annotator/column_annotation/column_wrapper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ module AnnotateRb
44
module ModelAnnotator
55
module ColumnAnnotation
66
class ColumnWrapper
7-
def initialize(column, column_defaults)
7+
def initialize(column, column_defaults, options)
88
@column = column
99
@column_defaults = column_defaults
10+
@options = options
1011
end
1112

1213
def raw_default
@@ -88,7 +89,7 @@ def name
8889

8990
# Simple quoting for the default column value
9091
def quote(value)
91-
DefaultValueBuilder.new(value).build
92+
DefaultValueBuilder.new(value, @options).build
9293
end
9394
end
9495
end

lib/annotate_rb/model_annotator/column_annotation/default_value_builder.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ module AnnotateRb
44
module ModelAnnotator
55
module ColumnAnnotation
66
class DefaultValueBuilder
7-
def initialize(value)
7+
def initialize(value, options)
88
@value = value
9+
@options = options
910
end
1011

1112
# @return [String]
@@ -27,6 +28,8 @@ def build
2728
private
2829

2930
def quote(value)
31+
return value.to_s.inspect if @options[:classes_default_to_s]&.include?(value.class.name)
32+
3033
case value
3134
when NilClass then "NULL"
3235
when TrueClass then "TRUE"

lib/annotate_rb/model_annotator/column_annotation/type_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TypeBuilder
1010

1111
def initialize(column, options, column_defaults)
1212
# Passing `column_defaults` for posterity, don't actually need it here since it's not used
13-
@column = ColumnWrapper.new(column, column_defaults)
13+
@column = ColumnWrapper.new(column, column_defaults, options)
1414
@options = options
1515
end
1616

lib/annotate_rb/options.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ def from(options = {}, state = {})
7676
target_action: :do_annotations, # Core; Possible values: :do_annotations, :remove_annotations
7777
wrapper: nil, # ModelAnnotator, RouteAnnotator
7878
wrapper_close: nil, # ModelAnnotator, RouteAnnotator
79-
wrapper_open: nil # ModelAnnotator, RouteAnnotator
79+
wrapper_open: nil, # ModelAnnotator, RouteAnnotator,
80+
classes_default_to_s: [] # ModelAnnotator
8081
}.freeze
8182

8283
PATH_OPTIONS = {
@@ -135,7 +136,8 @@ def from(options = {}, state = {})
135136
:target_action,
136137
:wrapper,
137138
:wrapper_close,
138-
:wrapper_open
139+
:wrapper_open,
140+
:classes_default_to_s
139141
].freeze
140142

141143
PATH_OPTION_KEYS = [

lib/annotate_rb/parser.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def self.parse(args, existing_options)
99

1010
BANNER_STRING = <<~BANNER.freeze
1111
Usage: annotaterb [command] [options]
12-
12+
1313
Commands:
1414
models [options]
1515
routes [options]
@@ -248,6 +248,15 @@ def add_model_options_to_parser(option_parser)
248248
"exclude table comments in model annotations") do
249249
@options[:with_table_comments] = false
250250
end
251+
252+
option_parser.on("--classes-default-to-s class",
253+
"Custom classes to be represented with `to_s`, may be used multiple times") do |klass|
254+
@options[:classes_default_to_s] = if @options[:classes_default_to_s].present?
255+
[*@options[:classes_default_to_s], klass]
256+
else
257+
[klass]
258+
end
259+
end
251260
end
252261

253262
def add_route_options_to_parser(option_parser)

spec/lib/annotate_rb/model_annotator/column_annotation/column_wrapper_spec.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
include AnnotateTestHelpers
55

66
describe "#default_string" do
7-
subject { described_class.new(column, column_defaults).default_string }
7+
subject { described_class.new(column, column_defaults, options).default_string }
88
let(:column) { mock_column("field", nil, default: value) }
99
let(:column_defaults) { {"field" => value} }
10+
let(:options) { {} }
1011

1112
context "when the value is nil" do
1213
let(:value) { nil }

spec/lib/annotate_rb/model_annotator/column_annotation/default_value_builder_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
RSpec.describe AnnotateRb::ModelAnnotator::ColumnAnnotation::DefaultValueBuilder do
44
describe "#build" do
5-
subject { described_class.new(value).build }
5+
subject { described_class.new(value, config).build }
6+
let(:config) { {} }
67

78
context "when value is a String" do
89
let(:value) { "a random string" }
@@ -83,5 +84,24 @@
8384
it { is_expected.to eq("[TRUE, FALSE]") }
8485
end
8586
end
87+
88+
context "with specified `classes_default_to_s` config option" do
89+
let(:config) { {classes_default_to_s: ["Integer"]} }
90+
91+
context "when value is an Integer" do
92+
let(:value) { 42 }
93+
94+
it { is_expected.to eq("\"42\"") }
95+
end
96+
97+
context "when config option includes unloaded class" do
98+
let(:config) { {classes_default_to_s: ["Locale", "Float"]} }
99+
let(:value) { 42 }
100+
101+
it "does not fail" do
102+
expect(subject).to eq("42")
103+
end
104+
end
105+
end
86106
end
87107
end

0 commit comments

Comments
 (0)