pikelet 2.0.0.beta.4 → 2.0.0.beta.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
  SHA1:
3
- metadata.gz: 70a0fcf61a81ceefc60f5553225d9e9c8aef3038
4
- data.tar.gz: fb8276527757550144e73f69b939bdeb6038a430
3
+ metadata.gz: f1dcc5a3b20d8e3277715f1841f88ea4dc22839c
4
+ data.tar.gz: a7526777b26bc21e7966fc7e1386083fe3630aac
5
5
  SHA512:
6
- metadata.gz: 3532873fe18e685b72fe211270f08d55b343e4e5cf87d094d0ec191b21c255e57c1e5eec609f2d5d789791ecfea7b9d00652ef23c6cdf034a256866ed4631c48
7
- data.tar.gz: e274f942d76718d17a135affafff8152299edb84a06a66aaa70d9be561fd4e2d6f7087503fe11f9c3395990c3cf5b7ebf2ea8f2084e10656117bfe136dff8a26
6
+ metadata.gz: 8f7957af9756008020b857e9ad2eb7e5e6c3474315900c55536babe8c685f6c596d2d798c1af2c597f5c30d34172b267aa4f68649dfed21c2b928f73805fc281
7
+ data.tar.gz: 275f4b314132e74d5cd74a73c32f78264d34f13cad6f286dc7a929d299f55fac5affeb040103d796ae98dde138e7d56672889203787fd88e52db8fb592f31d4d
@@ -1,15 +1,16 @@
1
1
  module Pikelet
2
2
  class FileDefinition
3
- attr_reader :base_record_definition
3
+ attr_reader :base, :signature_field
4
4
 
5
- def initialize(record_class: nil, &block)
6
- @base_record_definition = RecordDefiner.new(self, record_class: record_class).define(&block)
5
+ def initialize(signature_field: nil, record_class: nil, &block)
6
+ @signature_field = signature_field
7
+ definer = RecordDefiner.new(self, record_class: record_class)
8
+ @base = definer.define(&block)
7
9
  end
8
10
 
9
- def record(type_signature, record_class: nil, base_definition: nil, &block)
10
- base_definition ||= base_record_definition
11
- definer = RecordDefiner.new(self, record_class: record_class, base_definition: base_definition)
12
- record_definitions[type_signature] = definer.define(&block)
11
+ def record(signature, record_class: nil, base: nil, &block)
12
+ definer = RecordDefiner.new(self, record_class: record_class, base: base || self.base)
13
+ record_definitions[signature] = definer.define(&block)
13
14
  end
14
15
 
15
16
  def record_definitions
@@ -30,8 +31,12 @@ module Pikelet
30
31
 
31
32
  private
32
33
 
34
+ def all_record_definitions
35
+ [ base, *record_definitions.values ]
36
+ end
37
+
33
38
  def records_with_type_signatures
34
- [ base_record_definition, *record_definitions.values ].select(&:type_signature)
39
+ all_record_definitions.select(&:type_signature)
35
40
  end
36
41
 
37
42
  def type_signatures
@@ -39,12 +44,17 @@ module Pikelet
39
44
  end
40
45
 
41
46
  def best_definition(signatures)
42
- signatures.map { |sig| record_definitions[sig] }.detect { |d| d } || base_record_definition
47
+ signatures.map { |sig| record_definitions[sig] }.detect { |d| d } || base
48
+ end
49
+
50
+ def record_signature(record)
51
+ field = signature_field || (all_record_definitions.detect(&:signature_field) && :type_signature)
52
+ record.send(field) if record.respond_to?(field)
43
53
  end
44
54
 
45
55
  def format_record(record, width:)
46
- signatures = type_signatures.lazy.select(&record.method(:respond_to?)).map(&record.method(:send))
47
- best_definition(signatures).format(record, width: width)
56
+ definition = record_definitions[record_signature(record)] || base
57
+ definition.format(record, width: width)
48
58
  end
49
59
 
50
60
  def parse_records(data, &block)
@@ -56,12 +66,13 @@ module Pikelet
56
66
  end
57
67
 
58
68
  def signature_fields
59
- records_with_type_signatures.map(&:signature_field).uniq
69
+ all_record_definitions.map(&:signature_field).compact.uniq
60
70
  end
61
71
 
62
72
  def parse_record(data)
63
73
  signatures = signature_fields.lazy.map { |field| field.parse(data) }
64
- best_definition(signatures).parse(data)
74
+ definition = signatures.map { |sig| record_definitions[sig] }.detect { |defn| defn } || base
75
+ definition.parse(data)
65
76
  end
66
77
  end
67
78
  end
@@ -2,9 +2,12 @@ module Pikelet
2
2
  class RecordDefiner
3
3
  attr_reader :file_definition, :definition
4
4
 
5
- def initialize(file_definition, record_class: nil, base_definition: nil)
5
+ def initialize(file_definition, record_class: nil, base: nil)
6
6
  @file_definition = file_definition
7
- @definition = RecordDefinition.new(file_definition, record_class: record_class, base_definition: base_definition)
7
+ @definition = RecordDefinition.new(file_definition,
8
+ record_class: record_class,
9
+ base: base
10
+ )
8
11
  end
9
12
 
10
13
  def define(&block)
@@ -18,17 +21,8 @@ module Pikelet
18
21
  definition.field(name, index, **options, &block)
19
22
  end
20
23
 
21
- def type_signature(field_or_index, **options, &block)
22
- if field_or_index.is_a? Range
23
- field(:type_signature, field_or_index, **options, &block)
24
- definition.type_signature = :type_signature
25
- else
26
- definition.type_signature = field_or_index
27
- end
28
- end
29
-
30
- def record(type_signature, record_class: nil, &block)
31
- file_definition.record(type_signature, record_class: record_class, base_definition: definition, &block)
24
+ def record(signature, record_class: nil, &block)
25
+ file_definition.record(signature, record_class: record_class, base: definition, &block)
32
26
  end
33
27
 
34
28
  def method_missing(method, *args, **options, &block)
@@ -3,11 +3,10 @@ require "pikelet/record_definer"
3
3
  module Pikelet
4
4
  class RecordDefinition
5
5
  attr_reader :file_definition, :field_definitions
6
- attr_writer :type_signature
7
6
 
8
- def initialize(file_definition, record_class: nil, base_definition:)
7
+ def initialize(file_definition, record_class: nil, base:)
9
8
  @file_definition = file_definition
10
- @field_definitions = base_definition && base_definition.field_definitions.dup || {}
9
+ @field_definitions = base && base.field_definitions.dup || {}
11
10
  @record_class = record_class
12
11
  end
13
12
 
@@ -15,17 +14,6 @@ module Pikelet
15
14
  field_definitions[name] = Pikelet::FieldDefinition.new(index, **options, &block)
16
15
  end
17
16
 
18
- def type_signature
19
- unless defined? @type_signature
20
- @type_signature = :type_signature if field_definitions.key? :type_signature
21
- end
22
- @type_signature
23
- end
24
-
25
- def signature_field
26
- type_signature && field_definitions[type_signature]
27
- end
28
-
29
17
  def parse(data)
30
18
  record_class.new(parse_fields(data))
31
19
  end
@@ -44,7 +32,6 @@ module Pikelet
44
32
  def default_record_class
45
33
  Struct.new(*field_names) do
46
34
  def initialize(**attributes)
47
- # FIXME: Do we need to worry about missing attributes?
48
35
  super(*attributes.values_at(*self.class.members))
49
36
  end
50
37
  end
@@ -54,6 +41,10 @@ module Pikelet
54
41
  field_definitions.values.map { |d| d.index.max }.max + 1
55
42
  end
56
43
 
44
+ def signature_field
45
+ field_definitions[file_definition.signature_field || :type_signature]
46
+ end
47
+
57
48
  private
58
49
 
59
50
  def field_names
@@ -1,3 +1,3 @@
1
1
  module Pikelet
2
- VERSION = "2.0.0.beta.4"
2
+ VERSION = "2.0.0.beta.5"
3
3
  end
data/lib/pikelet.rb CHANGED
@@ -4,7 +4,7 @@ require "pikelet/record_definition"
4
4
  require "pikelet/field_definition"
5
5
 
6
6
  module Pikelet
7
- def self.define(record_class: nil, &block)
8
- Pikelet::FileDefinition.new(record_class: record_class, &block)
7
+ def self.define(signature_field: nil, record_class: nil, &block)
8
+ Pikelet::FileDefinition.new(signature_field: signature_field, record_class: record_class, &block)
9
9
  end
10
10
  end
@@ -4,34 +4,5 @@ require "pikelet"
4
4
  describe Pikelet::RecordDefiner do
5
5
  let(:definer) { described_class.new(nil) }
6
6
  let(:definition) { definer.definition }
7
-
8
- describe "#type_signature" do
9
- context "given a field name" do
10
- before do
11
- allow(definition).to receive(:type_signature=).and_call_original
12
- definer.type_signature :type
13
- end
14
-
15
- it "sets the type signature field on the record definition" do
16
- expect(definition).to have_received(:type_signature=).with(:type)
17
- end
18
- end
19
-
20
- context "legacy usage" do
21
- before do
22
- allow(definition).to receive(:type_signature=).and_call_original
23
- allow(definer).to receive(:field).and_call_original
24
- definer.type_signature 0...4, align: :right, pad: "0", &:to_i
25
- end
26
-
27
- it "creates the field" do
28
- expect(definer).to have_received(:field).with(:type_signature, 0...4, align: :right, pad: "0", &:to_i)
29
- end
30
-
31
- it "sets the type signature field on the record definition" do
32
- expect(definition).to have_received(:type_signature=).with(:type_signature)
33
- end
34
- end
35
- end
36
7
  end
37
8
 
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
  require "pikelet"
3
3
 
4
4
  describe Pikelet::RecordDefinition do
5
- let(:definer) { Pikelet::RecordDefiner.new(nil, base_definition: nil) }
5
+ let(:definer) { Pikelet::RecordDefiner.new(nil, base: nil) }
6
6
 
7
7
  let(:data) { "Hello world" }
8
8
 
@@ -21,32 +21,6 @@ describe Pikelet::RecordDefinition do
21
21
  it { is_expected.to eq "Hello world" }
22
22
  end
23
23
 
24
- describe "#type_signature" do
25
- let(:definition) { described_class.new(nil, base_definition: nil) }
26
-
27
- subject { definition }
28
-
29
- context "with no fields and no signature defined" do
30
- its(:type_signature) { is_expected.to be_nil }
31
- end
32
-
33
- context "with signature defined" do
34
- before do
35
- definition.type_signature = :type
36
- end
37
-
38
- its(:type_signature) { is_expected.to eq :type }
39
- end
40
-
41
- context "with a field named :type_signature" do
42
- before do
43
- definition.field(:type_signature, 0...3)
44
- end
45
-
46
- its(:type_signature) { is_expected.to eq :type_signature }
47
- end
48
- end
49
-
50
24
  describe "#width" do
51
25
  subject(:width) { definition.width }
52
26
 
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: 2.0.0.beta.4
4
+ version: 2.0.0.beta.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Carney
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-19 00:00:00.000000000 Z
11
+ date: 2015-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler