table_structure 0.3.4 → 0.3.5

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: 7ba1e6952eaa875b8f55c7b2af3f18fad6030c4cbbd8ae1caaec81ae20a1be3c
4
- data.tar.gz: 53b87d0b36b8eada62c881e3e286396b4dc82afae9a713feccab2da1258dfbf2
3
+ metadata.gz: 0044f8cb21e559ae23eed1f0eb6f28c1b0bc9dab9ed804088d17ff85f4040a4f
4
+ data.tar.gz: 97ef79ee4a4d71d1b6c20e56841a5e450f116777050e285e17ba11c2ddd43f1b
5
5
  SHA512:
6
- metadata.gz: 3c07d1f99a48b990fad463b4ab87a82f6ece8ad4e58eab86499a32266bc3580e6bbf9d83444956244f4c89e561998ae84c27441d7063efd46fd8b9daadcb6990
7
- data.tar.gz: 8003a4609435158f876752ad1b42a7b3d00eab319a17f765234fafc105c7ccae9888eccc75a2382a80d641f0aa837bf950e45bb665aa35bf9a30e87d87ec73fd
6
+ metadata.gz: 8cff983c441e44cfb7d6d0fc525aeca0b210ea50806421b607d3b53c4c0947a04eca0030f6da6cfdb91e05975dc14cb8805d08b314d48b73ee3013b944d99ed0
7
+ data.tar.gz: 3310d0d8446fc608a2956d45fb6bea2e514a774f1f83e19b828cde4d11f2fc69d5ad9ff75546f811f3d5992f2e81bccf6dd2dfc4208b078095750162bc68d0ad
data/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ # 0.3.5
2
+ Changes:
3
+ - `TableStructure::Schema`
4
+ - Add following options:
5
+ - `:name_prefix`
6
+ - `:name_suffix`
7
+ - DSL
8
+ - `column_converter`
9
+ - Add `:header` and `:row` options.
10
+ - If `header: false`, the converter is not applied to header values.
11
+ - If `row: false`, the converter is not applied to row values.
12
+ - Both options default to true.
13
+
1
14
  # 0.3.4
2
15
  Changes:
3
16
  - `TableStructure::Writer`
@@ -30,11 +43,13 @@ Changes:
30
43
  # 0.3.0
31
44
  Changes:
32
45
  - `TableStructure::Schema`
33
- - Add `:omitted` key for `column(s)` DSL.
34
- - Support nested schema.
35
- - Add following options for schema initialization:
46
+ - Add following options:
36
47
  - `:key_prefix`
37
48
  - `:key_suffix`
49
+ - DSL
50
+ - `column(s)`
51
+ - Add `:omitted` option.
52
+ - Support nested schema.
38
53
 
39
54
  # 0.2.0
40
55
  Changes:
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.4)
4
+ table_structure (0.3.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -237,8 +237,12 @@ class SampleTableSchema
237
237
  value: ->(row, *) { row[:name] }
238
238
 
239
239
  columns ->(table) { PetsSchema.new(context: table) }
240
+ ## Same as above code
241
+ # columns PetsSchema
240
242
 
241
243
  columns ->(table) { QuestionsSchema.new(context: table) }
244
+ ## Same as above code.
245
+ # columns QuestionsSchema
242
246
 
243
247
  column_converter :to_s, ->(val, *) { val.to_s }
244
248
  end
@@ -9,7 +9,7 @@ module TableStructure
9
9
  }
10
10
  }.freeze
11
11
 
12
- attr_reader :columns, :options
12
+ attr_reader :options
13
13
 
14
14
  def initialize(
15
15
  name,
@@ -25,8 +25,10 @@ module TableStructure
25
25
 
26
26
  @name = name
27
27
  @columns = create_columns(name, column_definitions, context, options)
28
- @context_builders = context_builders
29
- @column_converters = column_converters
28
+ @header_context_builder = context_builders[:header]
29
+ @row_context_builder = context_builders[:row]
30
+ @header_converters = select_column_converters(:header, column_converters)
31
+ @row_converters = select_column_converters(:row, column_converters)
30
32
  @result_builders = result_builders
31
33
  @context = context
32
34
  @options = options
@@ -34,6 +36,10 @@ module TableStructure
34
36
 
35
37
  def create_table(result_type: :array, **options)
36
38
  options = @options.merge(options)
39
+
40
+ header_converters =
41
+ optional_header_converters(options).merge(@header_converters)
42
+
37
43
  result_builders =
38
44
  RESULT_BUILDERS
39
45
  .select { |k, _v| k == result_type }
@@ -41,8 +47,10 @@ module TableStructure
41
47
 
42
48
  Table.new(
43
49
  @columns,
44
- @context_builders,
45
- @column_converters,
50
+ @header_context_builder,
51
+ @row_context_builder,
52
+ header_converters,
53
+ @row_converters,
46
54
  result_builders,
47
55
  @context,
48
56
  options
@@ -57,6 +65,29 @@ module TableStructure
57
65
  .compile(context)
58
66
  .map { |definition| Column.create(definition, options) }
59
67
  end
68
+
69
+ def select_column_converters(type, column_converters)
70
+ column_converters
71
+ .select { |_k, v| v[:options][type] }
72
+ .map { |k, v| [k, v[:callable]] }
73
+ .to_h
74
+ end
75
+
76
+ def optional_header_converters(options)
77
+ converters = {}
78
+ if options[:name_prefix]
79
+ converters[:_prepend_prefix] = lambda { |val, *|
80
+ val.nil? ? val : "#{options[:name_prefix]}#{val}"
81
+ }
82
+ end
83
+ if options[:name_suffix]
84
+ converters[:_append_suffix] = lambda { |val, *|
85
+ val.nil? ? val : "#{val}#{options[:name_suffix]}"
86
+ }
87
+ end
88
+
89
+ converters
90
+ end
60
91
  end
61
92
  end
62
93
  end
@@ -4,8 +4,17 @@ module TableStructure
4
4
  module Schema
5
5
  module DSL
6
6
  module ColumnConverter
7
- def column_converter(name, callable)
8
- column_converters[name] = callable
7
+ DEFAULT_OPTIONS = {
8
+ header: true,
9
+ row: true
10
+ }.freeze
11
+
12
+ def column_converter(name, callable, **options)
13
+ options = DEFAULT_OPTIONS.merge(options)
14
+ column_converters[name] = {
15
+ callable: callable,
16
+ options: options
17
+ }
9
18
  nil
10
19
  end
11
20
 
@@ -3,20 +3,23 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  class Table
6
- attr_reader :column_converters, :result_builders
6
+ attr_reader :header_converters, :row_converters, :result_builders
7
7
 
8
8
  def initialize(
9
9
  columns,
10
- context_builders,
11
- column_converters,
10
+ header_context_builder,
11
+ row_context_builder,
12
+ header_converters,
13
+ row_converters,
12
14
  result_builders,
13
15
  context,
14
16
  options
15
17
  )
16
18
  @columns = columns
17
- @header_context_builder = context_builders[:header]
18
- @row_context_builder = context_builders[:row]
19
- @column_converters = column_converters
19
+ @header_context_builder = header_context_builder
20
+ @row_context_builder = row_context_builder
21
+ @header_converters = header_converters
22
+ @row_converters = row_converters
20
23
  @result_builders = result_builders
21
24
  @context = context
22
25
  @options = options
@@ -26,12 +29,12 @@ module TableStructure
26
29
  if @header_context_builder
27
30
  context = @header_context_builder.call(context)
28
31
  end
29
- values(:name, context)
32
+ values(:name, context, @header_converters)
30
33
  end
31
34
 
32
35
  def row(context: nil)
33
36
  context = @row_context_builder.call(context) if @row_context_builder
34
- values(:value, context)
37
+ values(:value, context, @row_converters)
35
38
  end
36
39
 
37
40
  private
@@ -63,14 +66,14 @@ module TableStructure
63
66
  @size ||= @columns.map(&:size).reduce(0) { |memo, size| memo + size }
64
67
  end
65
68
 
66
- def values(method, context)
69
+ def values(method, context, converters)
67
70
  columns =
68
71
  @columns
69
72
  .map { |column| column.send(method, context, @context) }
70
73
  .flatten
71
74
  .map do |val|
72
- @column_converters.reduce(val) do |val, (_, column_converter)|
73
- column_converter.call(val, context, @context)
75
+ converters.reduce(val) do |val, (_, converter)|
76
+ converter.call(val, context, @context)
74
77
  end
75
78
  end
76
79
 
@@ -11,6 +11,8 @@ module TableStructure
11
11
  end
12
12
 
13
13
  DEFAULT_OPTIONS = {
14
+ name_prefix: nil,
15
+ name_suffix: nil,
14
16
  key_prefix: nil,
15
17
  key_suffix: nil
16
18
  }.freeze
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.4'
4
+ VERSION = '0.3.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_structure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-09-08 00:00:00.000000000 Z
11
+ date: 2019-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler