pikelet 1.0.1 → 1.1.0

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: 49ef5854cd526cdc7bd4491974e5ae7dcb013582
4
- data.tar.gz: 52b908b3bee6e565b53b5e0b17b9c2723af24a42
3
+ metadata.gz: 0f85a72791e86e65cc0074bbb3912015240b280f
4
+ data.tar.gz: 76b4aab6edad8e711a2df279b07d616e33de878d
5
5
  SHA512:
6
- metadata.gz: c5e64237c90b7eeea7e8c934df11f195948d1142c307951345f4454c7a9a6cee7e35a18962e518a860ef02eae128066a8f9c159f0960917f081fe5c383ccb8fc
7
- data.tar.gz: 2c92d249c1e3fb2e12de71d91fdabf809af6e2510ac95a90a024fd0e54d96a5e25fa23752754ec97fe00e1a7feeab8dd4e06b3acced5c2f419dbaa5bf8ac9e5f
6
+ metadata.gz: acc91a2a79c79bfb32f361ee41a0085cbf80c1196202e250335f7666625d153675de3ea163884c8ff3b5634f3b9a78c4498f243a19fd54f554da894cbbb13805
7
+ data.tar.gz: ce8f7386d344c99f65548b124dc6f0d3909296c04773ed744a33c8fdd77daf89b7595d7df66950f483c7783739b96e8d3280e6baa8e9107f68fe75a9d278d6a1
@@ -3,12 +3,12 @@ module Pikelet
3
3
  attr_reader :base_record_definition
4
4
 
5
5
  def initialize(&block)
6
- @base_record_definition = RecordDefinition.new(self, &block)
6
+ @base_record_definition = RecordDefiner.new(self).define(&block)
7
7
  end
8
8
 
9
9
  def record(type_signature, base_definition: nil, &block)
10
10
  base_definition ||= base_record_definition
11
- record_definitions[type_signature] = RecordDefinition.new(self, base_definition: base_definition, &block)
11
+ record_definitions[type_signature] = RecordDefiner.new(self, base_definition: base_definition).define(&block)
12
12
  end
13
13
 
14
14
  def record_definitions
@@ -0,0 +1,29 @@
1
+ module Pikelet
2
+ class RecordDefiner
3
+ attr_reader :file_definition, :definition
4
+
5
+ def initialize(file_definition, base_definition: nil)
6
+ @file_definition = file_definition
7
+ @definition = RecordDefinition.new(file_definition, base_definition: base_definition)
8
+ end
9
+
10
+ def define(&block)
11
+ if block_given?
12
+ instance_eval(&block)
13
+ end
14
+ definition
15
+ end
16
+
17
+ def field(name, index, &block)
18
+ definition.field(name, index, &block)
19
+ end
20
+
21
+ def record(type_signature, &block)
22
+ file_definition.record(type_signature, base_definition: definition, &block)
23
+ end
24
+
25
+ def method_missing(method, *args, &block)
26
+ field(method, *args, &block)
27
+ end
28
+ end
29
+ end
@@ -1,13 +1,12 @@
1
+ require "pikelet/record_definer"
2
+
1
3
  module Pikelet
2
4
  class RecordDefinition
3
5
  attr_reader :file_definition, :field_definitions
4
6
 
5
- def initialize(file_definition, base_definition: nil, &block)
7
+ def initialize(file_definition, base_definition:)
6
8
  @file_definition = file_definition
7
9
  @field_definitions = base_definition && base_definition.field_definitions.dup || {}
8
- if block_given?
9
- instance_eval(&block)
10
- end
11
10
  end
12
11
 
13
12
  def field(name, index, &block)
@@ -15,10 +14,6 @@ module Pikelet
15
14
  field_definitions[name] = Pikelet::FieldDefinition.new(index, &block)
16
15
  end
17
16
 
18
- def record(type_signature, &block)
19
- file_definition.record(type_signature, base_definition: self, &block)
20
- end
21
-
22
17
  def parse(data)
23
18
  record_class.new(*field_definitions.values.map { |field| field.parse(data) })
24
19
  end
@@ -27,10 +22,6 @@ module Pikelet
27
22
  record_class.new(*hash.values_at(*field_definitions.keys))
28
23
  end
29
24
 
30
- def method_missing(method, *args, &block)
31
- field(method, *args, &block)
32
- end
33
-
34
25
  def record_class
35
26
  @record_class ||= Struct.new(*field_definitions.keys.map(&:to_sym))
36
27
  end
@@ -1,3 +1,3 @@
1
1
  module Pikelet
2
- VERSION = "1.0.1"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -4,7 +4,7 @@ require "pikelet"
4
4
  describe Pikelet::RecordDefinition do
5
5
  let(:data) { "Hello world" }
6
6
  let(:definition) do
7
- Pikelet::RecordDefinition.new(nil) do
7
+ Pikelet::RecordDefiner.new(nil, base_definition: nil).define do
8
8
  hello 0...5
9
9
  world 6..-1
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pikelet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Carney
@@ -128,6 +128,7 @@ files:
128
128
  - lib/pikelet.rb
129
129
  - lib/pikelet/field_definition.rb
130
130
  - lib/pikelet/file_definition.rb
131
+ - lib/pikelet/record_definer.rb
131
132
  - lib/pikelet/record_definition.rb
132
133
  - lib/pikelet/version.rb
133
134
  - pikelet.gemspec