lhs 6.5.0 → 6.6.0

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: 79a4e6c3e274aa01035b67be3b5eeff82c68ddbd
4
- data.tar.gz: 9c9362c9aeed7f7e2ed352eb22701b0bce023a22
3
+ metadata.gz: 03af8ee3ccf17c8a75304d5c3f3ce6e395ef51dc
4
+ data.tar.gz: 60292bdfe213362cc375f32ac399bc26945d28d2
5
5
  SHA512:
6
- metadata.gz: 7cebde70543152187087c61cdfd89eb996ea93421393e3abd6062e63da99ba30a4f4ebc4abbd32474ed7f2ea9a3e7afc19c486881af22600d639b0184f8e17a3
7
- data.tar.gz: b066b19b120054d863ab7c18f134796c70f7ba0ae84ec1d7a60280188ea2aefd2e48f91326a3c9f847c938a806c1da1e8dec6c0625503e7160da4d9166cb32dd
6
+ metadata.gz: 47a6488f9d1a9dcd99b90e4cc5ef81bc16fdeb1559cf589ecd36fdacdf0d00c173d20f62743bc28b7e114ed64463a694e0709532a4c08d7fca01f8ae41124774
7
+ data.tar.gz: 88559585cc42b9f679038c2b8cf9005ce2d7ae3ffe2ab9997028d77105d37153f78647952555a0dfd7b4cb476c4a29a74d49da07b800c746e41974a7c19d38ad
data/README.md CHANGED
@@ -534,6 +534,13 @@ class User < LHS::Record
534
534
  end
535
535
  ```
536
536
 
537
+ ## Custom validation errors
538
+
539
+ In case you want to add custom validation errors to an instance of LHS::Record:
540
+
541
+ ```ruby
542
+ user.errors.add(:name, 'The name you provided is not valid.')
543
+ ```
537
544
 
538
545
  ## Pagination
539
546
 
data/lib/lhs/errors.rb CHANGED
@@ -4,10 +4,10 @@ class LHS::Errors
4
4
 
5
5
  attr_reader :messages, :message, :raw
6
6
 
7
- def initialize(response)
7
+ def initialize(response = nil)
8
8
  @messages = messages_from_response(response)
9
9
  @message = message_from_response(response)
10
- @raw = response.body
10
+ @raw = response.body if response
11
11
  rescue JSON::ParserError # rubocop:disable Lint/HandleExceptions
12
12
  end
13
13
 
@@ -17,6 +17,11 @@ class LHS::Errors
17
17
  alias has_key? include?
18
18
  alias key? include?
19
19
 
20
+ def add(attribute, message = :invalid, _options = {})
21
+ self[attribute]
22
+ messages[attribute] << message
23
+ end
24
+
20
25
  def get(key)
21
26
  messages[key]
22
27
  end
@@ -81,13 +86,14 @@ class LHS::Errors
81
86
  messages
82
87
  end
83
88
 
84
- def messages_from_response(response)
85
- return {} if !response.body.is_a?(String) || response.body.length.zero?
89
+ def messages_from_response(response = nil)
90
+ return {} if !response || !response.body.is_a?(String) || response.body.length.zero?
86
91
  json = JSON.parse(response.body)
87
92
  parse_messages(json)
88
93
  end
89
94
 
90
- def message_from_response(response)
95
+ def message_from_response(response = nil)
96
+ return unless response
91
97
  json = JSON.parse(response.body)
92
98
  json['message']
93
99
  end
data/lib/lhs/item.rb CHANGED
@@ -16,6 +16,11 @@ class LHS::Item < LHS::Proxy
16
16
 
17
17
  delegate :_raw, to: :_data
18
18
 
19
+ def initialize(data)
20
+ self.errors = LHS::Errors.new
21
+ super(data)
22
+ end
23
+
19
24
  def item?
20
25
  true
21
26
  end
data/lib/lhs/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = "6.5.0"
2
+ VERSION = "6.6.0"
3
3
  end
@@ -0,0 +1,19 @@
1
+ require 'rails_helper'
2
+
3
+ describe LHS::Item do
4
+ before(:each) do
5
+ class Record < LHS::Record
6
+ end
7
+ end
8
+
9
+ subject { Record.new }
10
+
11
+ context 'add error' do
12
+ it 'allows to add validation errors to the instance itself' do
13
+ subject.errors.add(:name, 'This date is invalid')
14
+ expect(
15
+ subject.errors.first
16
+ ).to eq [:name, 'This date is invalid']
17
+ end
18
+ end
19
+ end
@@ -26,7 +26,8 @@ describe LHS::Record do
26
26
  record = Feedback.create(object)
27
27
  expect(record).to be_kind_of Feedback
28
28
  expect(record.recommended).to eq true
29
- expect(record.errors).to eq nil
29
+ expect(record.errors.messages).to eq({})
30
+ expect(record.errors.message).to eq nil
30
31
  end
31
32
 
32
33
  it 'uses proper endpoint when creating data' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lhs
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.5.0
4
+ version: 6.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - https://github.com/local-ch/lhs/graphs/contributors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-12 00:00:00.000000000 Z
11
+ date: 2016-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lhc
@@ -273,6 +273,7 @@ files:
273
273
  - spec/dummy/public/favicon.ico
274
274
  - spec/endpoint/for_url_spec.rb
275
275
  - spec/item/accessors_spec.rb
276
+ - spec/item/add_error_spec.rb
276
277
  - spec/item/delegate_spec.rb
277
278
  - spec/item/destroy_spec.rb
278
279
  - spec/item/errors_spec.rb
@@ -420,6 +421,7 @@ test_files:
420
421
  - spec/dummy/public/favicon.ico
421
422
  - spec/endpoint/for_url_spec.rb
422
423
  - spec/item/accessors_spec.rb
424
+ - spec/item/add_error_spec.rb
423
425
  - spec/item/delegate_spec.rb
424
426
  - spec/item/destroy_spec.rb
425
427
  - spec/item/errors_spec.rb