hal-interpretation 1.1.1 → 1.2.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: 96131b9c5640d5b01861aac5c092f4cbc009bbe8
4
- data.tar.gz: 7ce4b73b5077ba8b72a63e3f09067375b8ddde89
3
+ metadata.gz: 52c231b40d2b58c3c0b1a5c3256b1b3b4a9d2588
4
+ data.tar.gz: ae6b29f0cb4de045685d0934d37e84c555314ab7
5
5
  SHA512:
6
- metadata.gz: d44a3aa8c1e3bc7f6298e006ab6f5bac0cb2403e217928f9e7c95660add3158ffa64ab98241eef9d3c485b5e49bf9301c0d3446450b57772ec2662fd123d43db
7
- data.tar.gz: 6179f01b1730d7adb295b5453d33492ac9db1c440b5fba19cc3bfdb0a19b3d0400aaeb328378ebbc144253f976b6cb69ef8fb75982c3367a569f9893f210f755
6
+ metadata.gz: 4d8c1cf2fc169bc09f4847b41ef4a25101cbe0ded25b5f0a7d3f25d06503508ced8a4cda4b21a0db80f9f15a5fe5a6e3d8acaacfe7937b7c9f4beff7e4311e71
7
+ data.tar.gz: 44f5146b86da19be5961c16c5ec4aed8ffc7ae0de5a95c576951e6805285a12b91bccdf95058fe8ac96d992efb98f432caf3618ceea8a6a9a60d38283f8629d4
data/README.md CHANGED
@@ -19,6 +19,18 @@ class UserHalInterpreter
19
19
 
20
20
  extract :name
21
21
  extract :address_line, from: "address/line1"
22
+ extract :seq, with: ->(_hal_repr) { next_seq_num }
23
+ extract :birthday, coercion: ->(date_str) { Date.iso8601(date_str) }
24
+
25
+ def initialize
26
+ @cur_seq_num = 0
27
+ end
28
+
29
+ protected
30
+
31
+ def next_seq_num
32
+ @cur_seq_num += 1
33
+ end
22
34
  end
23
35
  ```
24
36
 
@@ -24,14 +24,22 @@ module HalInterpretation
24
24
 
25
25
  # opts - named args
26
26
  # :from - The HalRepresentation from which to extract attribute.
27
+ #
27
28
  # :to - The model that we are extracting
28
29
  #
30
+ # :context - The context(usually a HalInterpreter) in which to
31
+ # execute the extraction
32
+ #
29
33
  # Returns any problems encountered.
30
34
  def extract(opts)
31
35
  from = opts.fetch(:from) { fail ArgumentError, "from is required" }
32
36
  to = opts.fetch(:to) { fail ArgumentError, "to is required" }
37
+ context = opts.fetch(:context, self)
38
+
39
+ raw_val = context.instance_exec from, &fetcher
40
+ val = context.instance_exec raw_val, &value_coercion
33
41
 
34
- to.send "#{attr}=", value_coercion.call(fetcher.call(from))
42
+ to.public_send "#{attr}=", val
35
43
 
36
44
  []
37
45
  rescue => err
@@ -40,7 +40,7 @@ module HalInterpretation
40
40
  new_item = item_class.new do |it|
41
41
  e = extractors
42
42
  e.each do |an_extractor|
43
- @problems += an_extractor.extract(from: repr, to: it)
43
+ @problems += an_extractor.extract(from: repr, to: it, context: interpreter)
44
44
  .map {|msg| "#{json_path_for an_extractor.attr} #{msg}" }
45
45
  end
46
46
  end
@@ -1,3 +1,3 @@
1
1
  module HalInterpretation
2
- VERSION = "1.1.1"
2
+ VERSION = "1.2.0"
3
3
  end
@@ -37,6 +37,25 @@ describe HalInterpretation::Extractor do
37
37
  end
38
38
  end
39
39
 
40
+ context "context dependent lambda based" do
41
+ subject(:extractor) { described_class
42
+ .new(attr: "seq", location: "/seq", extraction_proc:
43
+ ->(hal_repr) { self.count_so_far }) }
44
+
45
+ specify { expect{
46
+ extractor.extract(from: source, to: target, context: interpreter)
47
+ }.not_to raise_error }
48
+
49
+
50
+ context "after extraction" do
51
+ before do extractor.extract(from: source, to: target, context: interpreter) end
52
+
53
+ specify { expect(target.seq).to eq 42 }
54
+ end
55
+
56
+ let(:interpreter) { double(:interpreter, count_so_far: 42) }
57
+ end
58
+
40
59
  context "coercion" do
41
60
  subject(:extractor) { described_class.new(attr: "bday",
42
61
  coercion: ->(val){ Time.parse(val) } ) }
@@ -46,7 +65,7 @@ describe HalInterpretation::Extractor do
46
65
  end
47
66
 
48
67
 
49
- let(:target) { Struct.new(:first_name, :bday, :parent).new }
68
+ let(:target) { Struct.new(:first_name, :bday, :parent, :seq).new }
50
69
  let(:source) { HalClient::Representation.new(parsed_json: {
51
70
  "firstName" => "Alice",
52
71
  "bday" => "2013-10-10T12:13:14Z",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hal-interpretation
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-07 00:00:00.000000000 Z
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hal-client