hal-interpretation 1.3.0 → 1.4.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: ddd4f4d59ce42087dfde97336e8ff4c2cd32a99f
4
- data.tar.gz: c922581f34ac914205b687a8bbabc7be87678377
3
+ metadata.gz: 7c73d5eadd5be34552f2a0b5d2677410b55da858
4
+ data.tar.gz: 8f612d0a43581eb0aa0293d1da2b91a0530d6db7
5
5
  SHA512:
6
- metadata.gz: a52862249331ca2ebce1be637e8c44cf6d1d2073f95922b857e99400b4666a3356751849fb3a7b45d8967163b941c9be6256f679aa79488b826486177fd68319
7
- data.tar.gz: 45c8213ca0346ea609bd3c57d767969b03590211c5396b44402ab3c09b241d6eb26db89900978768d13fec298dd7f1129e3453e5af8967f44ecc720fa75aeedd
6
+ metadata.gz: 257068790c8689c780904f9da8762bae1706e5adc32a2458c0873155b3d15feb32afeb63cbf5712c72ff5ae00ec1ffcc21227245b1be700a52493cc316e0ebaa
7
+ data.tar.gz: 991b7e1ff8582dc1341bbcdbf8821043518b3c9f3f4538f764917ed4087d56979c4bb8fc6546cb1f57d12e6e9549066ff804dad5fd2e444ca7876abcc5b54150
data/README.md CHANGED
@@ -34,6 +34,8 @@ class UserHalInterpreter
34
34
  end
35
35
  ```
36
36
 
37
+ #### Create
38
+
37
39
  To interpret a HAL document simply create a new interpreter from the
38
40
  JSON document to interpret and then call its `#items` method.
39
41
 
@@ -42,6 +44,8 @@ class Users < ApplicationController
42
44
  def create
43
45
  @users = UserHalInterpreter.new_from_json(request.raw_post).items
44
46
 
47
+ @users.each(&:save!)
48
+
45
49
  rescue HalInterpretation::InvalidRepresentationError => err
46
50
  render template: "shared/error", status: 422, locals: { problems: err.problems }
47
51
  end
@@ -50,6 +54,27 @@ end
50
54
 
51
55
  The `items` method returns an `Enumerable` of valid `item_class` objects.
52
56
 
57
+ #### Update
58
+
59
+ To update and existing record
60
+
61
+ ```ruby
62
+
63
+ class Users < ApplicationController
64
+ def update
65
+ existing_user = User.find(params[:id])
66
+
67
+ @user = UserHalInterpreter.new_from_json(request.raw_post).only_update(existing_user)
68
+ .item
69
+
70
+ @user.save!
71
+
72
+ rescue HalInterpretation::InvalidRepresentationError => err
73
+ render template: "shared/error", status: 422, locals: { problems: err.problems }
74
+ end
75
+ end
76
+ ```
77
+
53
78
  ### Errors
54
79
 
55
80
  If the JSON being interpreted is invalid or malformed
@@ -6,6 +6,11 @@ require "hal-client"
6
6
  # objects.
7
7
  module HalInterpretation
8
8
 
9
+ # Declares that this interpreter should only update `an_item`.
10
+ def only_update(an_item)
11
+ @item_to_update = an_item
12
+ end
13
+
9
14
  # Returns array of models created from the HAL representation we are
10
15
  # interpreting.
11
16
  #
@@ -14,7 +19,17 @@ module HalInterpretation
14
19
  def items
15
20
  (fail InvalidRepresentationError.new(problems)) if problems.any?
16
21
 
17
- @observations ||= interpreters.flat_map(&:items)
22
+ @items ||= interpreters.flat_map(&:items)
23
+ end
24
+
25
+ # Returns the single item interpreted.
26
+ #
27
+ # Raises InvalidRepresentationError if more than one item was found.
28
+ def item
29
+ (fail InvalidRepresentationError, "More than one representation found") if
30
+ items.size > 1
31
+
32
+ items.first
18
33
  end
19
34
 
20
35
  # Returns array of problems messages, or empty array if there are
@@ -27,6 +42,17 @@ module HalInterpretation
27
42
  extend Forwardable
28
43
  def_delegators "self.class", :extractors, :extractor_for
29
44
 
45
+ # Internal: builds and returns items that should be use interpreted
46
+ # into.
47
+ def new_item(&blk)
48
+ if item_to_update
49
+ yield item_to_update
50
+ item_to_update
51
+ else
52
+ item_class.new(&blk)
53
+ end
54
+ end
55
+
30
56
  protected
31
57
 
32
58
  # opts
@@ -54,6 +80,8 @@ module HalInterpretation
54
80
  [ItemInterpreter.new(repr, location: location, interpreter: self)]
55
81
  end
56
82
  end
83
+ .tap {|is| raise(HalInterpretation::InvalidRepresentationError,
84
+ "Too many representations") if item_to_update && is.size > 1 }
57
85
  end
58
86
 
59
87
  # Back stop method to be overridden by individual interpreters.
@@ -61,6 +89,8 @@ module HalInterpretation
61
89
  fail NotImplementedError, "interpreter classes must call `item_class <model class>` in the class defintion"
62
90
  end
63
91
 
92
+ attr_reader :item_to_update
93
+
64
94
  module ClassMethods
65
95
  # Returns new interpreter for the provided JSON document.
66
96
  #
@@ -28,7 +28,7 @@ module HalInterpretation
28
28
 
29
29
  extend Forwardable
30
30
 
31
- def_delegators :interpreter, :extractors, :extractor_for, :item_class
31
+ def_delegators :interpreter, :extractors, :extractor_for
32
32
 
33
33
  attr_reader :repr, :location, :interpreter
34
34
 
@@ -37,7 +37,7 @@ module HalInterpretation
37
37
  end
38
38
 
39
39
  def interpret
40
- new_item = item_class.new do |it|
40
+ new_item = interpreter.new_item do |it|
41
41
  e = extractors
42
42
  e.each do |an_extractor|
43
43
  @problems += an_extractor.extract(from: repr, to: it, context: interpreter)
@@ -1,3 +1,3 @@
1
1
  module HalInterpretation
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -39,12 +39,29 @@ describe HalInterpretation do
39
39
  JSON
40
40
 
41
41
  specify { expect(interpreter.items).to have(1).item }
42
- specify { expect(interpreter.items.first.name).to eq "foo" }
43
- specify { expect(interpreter.items.first.latitude).to eq 39.1 }
44
- specify { expect(interpreter.items.first.up).to eq "/foo" }
45
- specify { expect(interpreter.items.first.bday).to eq Time.utc(2013,12,11,10,9,8) }
46
- specify { expect(interpreter.items.first.seq).to eq 1 }
42
+ specify { expect(interpreter.item).to be interpreter.items.first }
43
+ specify { expect(interpreter.item.name).to eq "foo" }
44
+ specify { expect(interpreter.item.latitude).to eq 39.1 }
45
+ specify { expect(interpreter.item.up).to eq "/foo" }
46
+ specify { expect(interpreter.item.bday).to eq Time.utc(2013,12,11,10,9,8) }
47
+ specify { expect(interpreter.item.seq).to eq 1 }
47
48
  specify { expect(interpreter.problems).to be_empty }
49
+
50
+ context "for update" do
51
+ let(:existing) { test_item_class.new do |it|
52
+ it.name = "foo"
53
+ it.latitude = 40
54
+ end }
55
+
56
+ before do interpreter.only_update(existing) end
57
+
58
+ specify { expect(interpreter.items).to have(1).item }
59
+ specify { expect(interpreter.item).to be interpreter.items.first }
60
+ specify { expect(interpreter.item).to eq existing }
61
+ specify { expect(interpreter.item.name).to eq "foo" }
62
+ specify { expect(interpreter.item.latitude).to eq 39.1 }
63
+ specify { expect(interpreter.item.bday).to eq Time.utc(2013,12,11,10,9,8) }
64
+ end
48
65
  end
49
66
 
50
67
  context "valid collection" do
@@ -75,6 +92,9 @@ describe HalInterpretation do
75
92
  specify { expect(interpreter.items[0].seq).to eq 1 }
76
93
  specify { expect(interpreter.items[1].seq).to eq 2 }
77
94
 
95
+ specify { expect{interpreter.item}
96
+ .to raise_error HalInterpretation::InvalidRepresentationError }
97
+
78
98
  matcher :item_named do |expected_name|
79
99
  match do |obj|
80
100
  obj.name == expected_name
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.3.0
4
+ version: 1.4.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-09-30 00:00:00.000000000 Z
11
+ date: 2014-12-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: hal-client
@@ -188,3 +188,4 @@ test_files:
188
188
  - spec/hal_interpretation/extractor_spec.rb
189
189
  - spec/hal_interpretation_spec.rb
190
190
  - spec/spec_helper.rb
191
+ has_rdoc: