Skip to content

Commit 0b7ec85

Browse files
author
Thomas Statter
committedMay 16, 2018
Start sorting out configuration mess
1 parent c8fc2f7 commit 0b7ec85

13 files changed

+61
-18
lines changed
 

‎datashift.thor

+5
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ module Datashift
7575
exit(-1)
7676
end
7777

78+
if options[:bump] && options[:bump] !~ /^(\d+\.)?(\d+\.)?(\*|\d+)$/
79+
puts 'ERROR: bump should be a valid numeric version in form x.x.x'
80+
exit(-1)
81+
end
82+
7883
version = options[:bump] || DataShift::VERSION
7984

8085
# Bump the VERSION file in library

‎lib/datashift/column_packer.rb

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ def record_to_column(record, json = false)
4343

4444
return '' if record.nil? || (record.respond_to?(:each) && record.empty?)
4545

46+
# :only and :except options can be used to limit the attributes included
47+
4648
return record.to_json if json # packs associations into single column
4749

4850
data = []
@@ -62,6 +64,7 @@ def record_to_column(record, json = false)
6264
"#{attribute_list_start}#{data.join(multi_value_delim)}#{attribute_list_end}"
6365
end
6466

67+
6568
end
6669

6770
# Convert an AR instance to a set of CSV columns

‎lib/datashift/configuration.rb

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ def self.parse_yaml( yaml_file )
1919
end
2020

2121

22+
# @param [Boolean] Export/Import of data expected in JSON format
23+
# @return [Boolean]
24+
#
25+
attr_accessor :json
26+
27+
def json?
28+
!!@json
29+
end
30+
2231
# List of association +TYPES+ to INCLUDE [:assignment, :enum, :belongs_to, :has_one, :has_many, :method]
2332
# Defaults to [:assignment, :enum]
2433
#

‎lib/datashift/exporters/configuration.rb

-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class Configuration < DataShift::Configuration
2424
#
2525
attr_accessor :csv_delimiter
2626

27-
# @param [Boolean] Export association data in single column in JSON format
28-
# @return [Boolean]
29-
#
30-
attr_accessor :json
31-
3227
# @param [String] Name for worksheet, otherwise uses Class name
3328
# @return [String]
3429
#

‎lib/datashift/exporters/excel_exporter.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ def prepare_data_flow_schema(klass)
9696
#
9797
# See - lib/exporters/configuration.rb
9898
#
99-
def export_with_associations(file_name, klass, records, options = {})
99+
# To export associations as JSON:
100+
#
101+
# DataShift::Exporters::Configuration.configure { |config| config.json = true }
102+
#
103+
def export_with_associations(file_name, klass, export_records, options = {})
104+
105+
records = [*export_records]
100106

101107
state = DataShift::Configuration.call.with
102108

@@ -132,7 +138,7 @@ def export_with_associations(file_name, klass, records, options = {})
132138
# pack association instances into single column
133139
if model_method.association_type?
134140
logger.info("Processing #{model_method.inspect} associations")
135-
excel[row, column] = record_to_column( obj.send( model_method.operator ), configuration.json )
141+
excel[row, column] = record_to_column( obj.send( model_method.operator ), configuration.json? )
136142
else
137143
excel[row, column] = obj.send( model_method.operator )
138144
end

‎lib/datashift/exporters/exporter_base.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ module DataShift
99

1010
class ExporterBase
1111

12-
attr_accessor :configuration
1312
attr_accessor :file_name
1413

1514
def initialize
16-
@configuration = DataShift::Exporters::Configuration.call
15+
end
16+
17+
def configuration
18+
DataShift::Configuration.call
1719
end
1820

1921
end

‎lib/datashift/header.rb

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ def initialize(source:, presentation: nil)
1818
@presentation = presentation || source
1919
end
2020

21+
def ==(rhs)
22+
puts "== Called with #{rhs}"
23+
@source == rhs
24+
end
25+
2126
def to_s
2227
presentation
2328
end

‎lib/datashift/headers.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ class Headers
2626

2727
def_delegators :@headers, *Array.delegated_methods_for_fwdable
2828

29-
def initialize(source, idx = 0, headers = [])
29+
def initialize(source, header_row_index = 0, headers = [])
3030
@source = source
31-
@idx = idx
31+
@idx = header_row_index
3232
@headers = headers
3333
end
3434

35+
# Check for either string or symbol version of a header
36+
def header?( header)
37+
h = header.is_a?(ModelMethod) ? header.operator : header
38+
(sources & [h, h.to_sym]).present?
39+
end
40+
41+
def index(header)
42+
h = header.is_a?(ModelMethod) ? header.operator : header
43+
sources.index(h) || sources.index(h.to_sym)
44+
end
45+
46+
3547
# Factory for dealing with Active Record models and collections
3648
# Catalogs the supplied Klass and builds set of expected/valid Headers for Klass
3749
# See config options
@@ -79,6 +91,7 @@ def class_source_to_operators
7991
if(mm.association_type?)
8092
association_to_headers(mm)
8193
else
94+
# TODO - can/shoudl we standardise to always store symbols - this is currently String
8295
add mm.operator
8396
end
8497
end

‎lib/datashift/mapping/data_flow_schema.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@ def prepare_from_klass( klass, doc_context = nil )
8282

8383
@nodes = create_node_collections(klass, doc_context: doc_context)
8484

85-
klass_to_model_methods( klass ).each_with_index do |mm, i|
86-
@nodes.headers.add(mm.operator) # for a class, the header names, default to the operators (methods)
85+
@nodes.headers = Headers.klass_to_headers(klass)
8786

88-
binding = MethodBinding.new(mm.operator, mm, idx: i)
87+
klass_to_model_methods( klass ).each do |mm|
88+
# headers define what nodes are actually in scope
89+
next unless @nodes.headers.header?(mm)
90+
91+
i = @nodes.headers.index(mm)
92+
93+
binding = MethodBinding.new(mm.operator, mm, idx: i) # TOFIX - why need to pass in both mm.operator AND mm ??
8994

9095
# TODO: - do we really need to pass in the doc context when parent nodes already has it ?
9196
@nodes << DataShift::NodeContext.new(@nodes.doc_context, binding, i, nil)

‎lib/datashift/node_collection.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class NodeCollection
1818

1919
def_delegators :@nodes, *Array.delegated_methods_for_fwdable
2020

21-
def_delegators :@doc_context, :headers, :klass
21+
def_delegators :@doc_context, :headers, :'headers=', :klass
2222

2323
def initialize(doc_context: nil)
2424
@nodes = []

‎lib/datashift/transformation/remove.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def remove_list
1818

1919
def association?(mm)
2020
return false unless(mm.association_type?)
21-
DataShift::Configuration.call.exclude_associations.include?(mm.operator)
21+
(DataShift::Configuration.call.exclude_associations & [mm.operator, mm.operator.to_sym]).present?
2222
end
2323

2424
# Specify columns to remove via DataShift::Configuration

‎spec/datashift/exporters/csv_exporter_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ module DataShift
221221

222222
expected = result_file('project_and_assoc_in_json_export.csv')
223223

224-
DataShift::Exporters::Configuration.configure do |config|
224+
DataShift::Configuration.configure do |config|
225225
config.json = true
226226
end
227227

‎spec/datashift/exporters/excel_exporter_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ module DataShift
166166

167167
expected = result_file('project_and_assoc_in_json_export.xls')
168168

169-
DataShift::Exporters::Configuration.configure do |config|
169+
DataShift::Configuration.configure do |config|
170170
config.json = true
171171
end
172172

0 commit comments

Comments
 (0)
Please sign in to comment.