table_structure 0.3.8 → 0.3.9

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: e177edf11a4350aba68061bd14e8f30a936f72baa8360df318633fe1197fcffe
4
- data.tar.gz: 277ad55b42c096f7f4cca0089dc97e250a336b3decdbedf051637288ef7a2352
3
+ metadata.gz: 15634b3115faa6adf71706af3ee278aa08b196408e4bcf895de38d1dd9401dff
4
+ data.tar.gz: 678ca7958dbb6edfea148639c4e9ec51e336226112f6e7f5d9ca65a511cadf41
5
5
  SHA512:
6
- metadata.gz: 6442ee4a8f9e372574a3f701f85d0252f22ee939b53af85d89ed042ef6c07675131d0a0a1df2ad0bf269f9076393bdc8f660fa2e8cdaa941f4d3017f9296e7ff
7
- data.tar.gz: 22975ca738449c98fdb23c190c919ac91dddaede9efbabdb9871632120c3369adc87166b1f94bb7f3aa0a0105b53df8c4222c565d9cb3d85b5deb57aee58af2a
6
+ metadata.gz: 605978ebfae00094056ad2dd53e4ff3c26a4caf3575b8efcf54545998ea36bcfc5cc93eb779c7bc43df277f562337cf6b22b381b392e2552d32636e7b65e0459
7
+ data.tar.gz: 99f37c9c2d3b3feaed7332c0c9a4e6d76c3eb3f3960ccced2b2db13abd973f3f68914ce26465f36df72da5a200005bec8a644bd6216f9ce5c1b3c629a92f1a4e
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.3.9
2
+ Changes:
3
+ - Add `TableStructure::CSV::Writer`. This is a wrapper for `TableStructure::Writer`.
4
+
1
5
  # 0.3.8
2
6
  Changes:
3
7
  - `TableStructure::Schema`
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- table_structure (0.3.8)
4
+ table_structure (0.3.9)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -80,7 +80,7 @@ writer = TableStructure::Writer.new(schema)
80
80
  # writer = TableStructure::Writer.new(schema, header_omitted: true)
81
81
  ```
82
82
 
83
- Writes the items converted by the schema to array
83
+ Writes the items converted by the schema to array:
84
84
  ```ruby
85
85
  items = [
86
86
  {
@@ -98,7 +98,9 @@ items = [
98
98
  ]
99
99
 
100
100
  ## When using `find_each` method of Rails
101
- # items = ->(y) { Records.find_each {|r| y << r } }
101
+ # items = Enumerator.new { |y| Item.find_each { |item| y << item } }
102
+ ## or
103
+ # items = ->(y) { Item.find_each { |item| y << item } }
102
104
 
103
105
  table = []
104
106
  writer.write(items, to: table)
@@ -127,6 +129,14 @@ end
127
129
  ```
128
130
  [Sample with docker](https://github.com/jsmmr/ruby_table_structure_sample)
129
131
 
132
+ You can also use `TableStructure::CSV::Writer` instead:
133
+ ```ruby
134
+ writer = TableStructure::CSV::Writer.new(schema)
135
+ File.open('sample.csv', 'w') do |f|
136
+ writer.write(items, to: f, bom: true)
137
+ end
138
+ ```
139
+
130
140
  #### TableStructure::Iterator
131
141
  Specifying `result_type: :hash` option works well.
132
142
  To use this option, define `column(s)` with `:key`.
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TableStructure
4
+ module CSV
5
+ class Writer
6
+ DEFAULT_OPTIONS = {
7
+ bom: false,
8
+ header_omitted: false,
9
+ header_context: nil
10
+ }.freeze
11
+
12
+ FIXED_OPTIONS = {
13
+ result_type: :array,
14
+ method: :<<
15
+ }.freeze
16
+
17
+ BOM = "\uFEFF"
18
+
19
+ def initialize(schema, **options)
20
+ require 'csv'
21
+ @options = DEFAULT_OPTIONS.merge(options).merge(FIXED_OPTIONS)
22
+ @writer = ::TableStructure::Writer.new(schema, **@options)
23
+ end
24
+
25
+ def write(items, to:, **options)
26
+ options = @options.merge(options).merge(FIXED_OPTIONS)
27
+ to.send(options[:method], BOM) if options[:bom]
28
+ @writer.write(items, to: ::CSV.new(to), **options)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -34,6 +34,7 @@ module TableStructure
34
34
  omitted = definition.delete(:omitted)
35
35
  next if Utils.evaluate_callable(omitted, context)
36
36
 
37
+ definition = evaluate_attrs(definition, context)
37
38
  validator.validate(definition)
38
39
  definition[:size] = determine_size(definition)
39
40
  definition
@@ -54,6 +55,11 @@ module TableStructure
54
55
 
55
56
  private
56
57
 
58
+ def evaluate_attrs(definition, context)
59
+ size = Utils.evaluate_callable(definition[:size], context)
60
+ definition.merge(size: size)
61
+ end
62
+
57
63
  def determine_size(name:, key:, size:, **)
58
64
  return size if size
59
65
 
@@ -23,7 +23,7 @@ module TableStructure
23
23
  raise Error.new('"size" must be positive.', @name, @index)
24
24
  end
25
25
  if key && size && [key].flatten.size < size
26
- raise Error.new('"key" size must be greater than or equal to specified "size".', @name, @index)
26
+ raise Error.new('"key" size must not be less than specified "size".', @name, @index)
27
27
  end
28
28
 
29
29
  true
@@ -3,6 +3,7 @@
3
3
  module TableStructure
4
4
  module Schema
5
5
  class Table
6
+ # TODO: Following `attr_reader` will be removed after version 1.4.0.
6
7
  attr_reader :header_column_converters, :row_column_converters, :result_builders
7
8
 
8
9
  def initialize(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TableStructure
4
- VERSION = '0.3.8'
4
+ VERSION = '0.3.9'
5
5
  end
@@ -25,5 +25,6 @@ module TableStructure
25
25
  require 'table_structure/schema/column/schema'
26
26
  require 'table_structure/schema/utils'
27
27
  require 'table_structure/writer'
28
+ require 'table_structure/csv/writer'
28
29
  require 'table_structure/iterator'
29
30
  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.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - jsmmr
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-16 00:00:00.000000000 Z
11
+ date: 2019-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -71,6 +71,7 @@ files:
71
71
  - bin/console
72
72
  - bin/setup
73
73
  - lib/table_structure.rb
74
+ - lib/table_structure/csv/writer.rb
74
75
  - lib/table_structure/iterator.rb
75
76
  - lib/table_structure/schema.rb
76
77
  - lib/table_structure/schema/column.rb