pikelet 1.1.1 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 22fadd28f1e37b036e1f2cf7c5b2afee9a0bee74
4
- data.tar.gz: 51c14989ef2bbd19eb3c04107d45b87a1e5396fb
3
+ metadata.gz: 92134160fa79ab2d8985325efe9593aab5380451
4
+ data.tar.gz: 48510ce920f6fd195814d84688584b0d5c079da2
5
5
  SHA512:
6
- metadata.gz: 5ecea7731c540fd6626f96cbf9ce6058adad82f616f06066255858f0ba53906c04b844e06747774636990114bc350bc6df5ead169d80b99fc328d3de46672fc7
7
- data.tar.gz: a8c75d1f053b7f732a61d50da20b538735c67b97777103d8f43101e82c17512cb2233eac37e4e6fa5c7d84e59c72373581b8f3fa65bfdaf87d65e77be4cb161b
6
+ metadata.gz: ebfab61d3e579d5316e86c2495eba1ad7d57795c3fbed235c4f969ae9dc5892ee26f24622809a34c3e75f1a292dd5affb53365cf4e5e118f9fa756640c081e6d
7
+ data.tar.gz: fa71ad390ba4ece29732b2917b7f0de1ae4b4e6121755b7390241aa9859753a17a696397296c83a28ab007a32064a43fda10c2b14b17b8b9e32d38f012e40f26
@@ -2,9 +2,10 @@ module Pikelet
2
2
  class FieldDefinition
3
3
  attr_reader :index, :parser
4
4
 
5
- def initialize(index, &parser)
5
+ def initialize(index, parse: nil, &parser)
6
6
  @index = index
7
- @parser = parser || :strip.to_proc
7
+ @parser = parser || parse || :strip
8
+ @parser = @parser.to_proc unless @parser.respond_to? :call
8
9
  end
9
10
 
10
11
  def parse(text)
@@ -14,16 +14,16 @@ module Pikelet
14
14
  definition
15
15
  end
16
16
 
17
- def field(name, index, &block)
18
- definition.field(name, index, &block)
17
+ def field(name, index, **options, &block)
18
+ definition.field(name, index, **options, &block)
19
19
  end
20
20
 
21
21
  def record(type_signature, &block)
22
22
  file_definition.record(type_signature, base_definition: definition, &block)
23
23
  end
24
24
 
25
- def method_missing(method, *args, &block)
26
- field(method, *args, &block)
25
+ def method_missing(method, *args, **options, &block)
26
+ field(method, *args, **options, &block)
27
27
  end
28
28
  end
29
29
  end
@@ -9,9 +9,9 @@ module Pikelet
9
9
  @field_definitions = base_definition && base_definition.field_definitions.dup || {}
10
10
  end
11
11
 
12
- def field(name, index, &block)
12
+ def field(name, index, **options, &block)
13
13
  @record_class = nil
14
- field_definitions[name] = Pikelet::FieldDefinition.new(index, &block)
14
+ field_definitions[name] = Pikelet::FieldDefinition.new(index, **options, &block)
15
15
  end
16
16
 
17
17
  def parse(data)
@@ -1,3 +1,3 @@
1
1
  module Pikelet
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
@@ -3,63 +3,90 @@ require "pikelet"
3
3
  require "csv"
4
4
 
5
5
  describe Pikelet::FieldDefinition do
6
- let(:data) { "The quick brown fox" }
7
- let(:definition) { Pikelet::FieldDefinition.new(index) }
6
+ let(:data) { "The quick brown fox" }
7
+ let(:definition) { Pikelet::FieldDefinition.new(index) }
8
8
 
9
- subject(:value) { definition.parse(data) }
9
+ subject(:result) { definition.parse(data) }
10
10
 
11
- describe "for a fixed-width field" do
11
+ context "for a fixed-width field" do
12
12
  let(:index) { 4...9 }
13
13
 
14
14
  it "extracts the field content from the data" do
15
- expect(value).to eq "quick"
15
+ expect(result).to eq "quick"
16
16
  end
17
17
  end
18
18
 
19
- describe "given whitespace" do
19
+ context "given whitespace" do
20
20
  let(:index) { 3...16 }
21
21
 
22
22
  it "strips leading and trailing whitespace" do
23
- expect(value).to eq "quick brown"
23
+ expect(result).to eq "quick brown"
24
24
  end
25
25
  end
26
26
 
27
- describe "given a CSV row" do
27
+ context "given a CSV row" do
28
28
  let(:data) { CSV.parse("The,quick,brown,fox").first }
29
29
  let(:index) { 2 }
30
30
 
31
31
  it "extracts the field" do
32
- expect(value).to eq "brown"
32
+ expect(result).to eq "brown"
33
33
  end
34
34
  end
35
35
 
36
- describe "given a parser block" do
37
- let(:index) { 4...9 }
38
- let(:definition) do
39
- Pikelet::FieldDefinition.new(index) { |value| value.reverse }
36
+ context "given a custom parser" do
37
+ let(:parser) { ->(value) { value } }
38
+
39
+ before do
40
+ allow(parser).to receive(:call)
41
+ result
42
+ end
43
+
44
+ context "as a block" do
45
+ let(:index) { 4...9 }
46
+ let(:definition) { Pikelet::FieldDefinition.new(index, &parser) }
47
+
48
+ it "yields the value to the parser" do
49
+ expect(parser).to have_received(:call).with("quick")
50
+ end
40
51
  end
41
52
 
42
- it "yields the value to the parser" do
43
- expect(value).to eq "kciuq"
53
+ context "as a parse option" do
54
+ let(:index) { 10...15 }
55
+ let(:definition) { Pikelet::FieldDefinition.new(index, parse: parser) }
56
+
57
+ it "yields the value to the parser" do
58
+ expect(parser).to have_received(:call).with("brown")
59
+ end
44
60
  end
45
61
  end
46
62
 
47
- describe "given a symbol for the parser block" do
48
- let(:index) { 4...9 }
49
- let(:definition) do
50
- Pikelet::FieldDefinition.new(index, &:upcase)
63
+ context "given a shorthand parser" do
64
+ let(:parser) { :upcase }
65
+
66
+ context "as a block" do
67
+ let(:index) { 10...15 }
68
+ let(:definition) { Pikelet::FieldDefinition.new(index, &parser) }
69
+
70
+ it "invokes the named method on the value" do
71
+ expect(result).to eq "BROWN"
72
+ end
51
73
  end
52
74
 
53
- it "invokes the named method on the value" do
54
- expect(value).to eq "QUICK"
75
+ context "as a parse option" do
76
+ let(:index) { 4...9 }
77
+ let(:definition) { Pikelet::FieldDefinition.new(index, parse: parser) }
78
+
79
+ it "invokes the named method on the value" do
80
+ expect(result).to eq "QUICK"
81
+ end
55
82
  end
56
83
  end
57
84
 
58
- describe "given an index not covered in the data" do
85
+ context "given an index not covered in the data" do
59
86
  let(:index) { 999..999 }
60
87
 
61
88
  it "parses as nil" do
62
- expect(value).to be_nil
89
+ expect(result).to be_nil
63
90
  end
64
91
  end
65
92
  end
data/spec/pikelet_spec.rb CHANGED
@@ -132,6 +132,42 @@ describe Pikelet do
132
132
  its(:value) { is_expected.to eq 5637 }
133
133
  end
134
134
 
135
+ describe "given a parse option" do
136
+ let(:definition) do
137
+ Pikelet.define do
138
+ value 0...4, parse: ->(value) { value.to_i }
139
+ end
140
+ end
141
+
142
+ let(:data) do
143
+ <<-FILE.gsub(/^\s*/, "").split(/[\r\n]+/)
144
+ 5637
145
+ FILE
146
+ end
147
+
148
+ subject { records.first }
149
+
150
+ its(:value) { is_expected.to eq 5637 }
151
+ end
152
+
153
+ describe "given a shorthand parse option" do
154
+ let(:definition) do
155
+ Pikelet.define do
156
+ value 0...4, parse: :to_i
157
+ end
158
+ end
159
+
160
+ let(:data) do
161
+ <<-FILE.gsub(/^\s*/, "").split(/[\r\n]+/)
162
+ 5637
163
+ FILE
164
+ end
165
+
166
+ subject { records.first }
167
+
168
+ its(:value) { is_expected.to eq 5637 }
169
+ end
170
+
135
171
  describe "given a block when parsing" do
136
172
  let(:collected_records) { [] }
137
173
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pikelet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Carney
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-29 00:00:00.000000000 Z
11
+ date: 2015-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -156,7 +156,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
156
156
  version: '0'
157
157
  requirements: []
158
158
  rubyforge_project:
159
- rubygems_version: 2.2.2
159
+ rubygems_version: 2.4.3
160
160
  signing_key:
161
161
  specification_version: 4
162
162
  summary: A simple flat-file database parser.