lhs 21.1.3 → 21.1.4

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
  SHA256:
3
- metadata.gz: b5d0f511517652dc99ad8e693f0f35b78bcc0580e24ee198fddbe681abee879e
4
- data.tar.gz: b117fb8348082ef4e55991945f6d8bdc91ecd18225a6e46ad1ae148e742174be
3
+ metadata.gz: e2612560f58416abc9a3ecd2583d5c53bf4ab14866735058e606a69e872c1263
4
+ data.tar.gz: f423637d06798525364da4332b2edc502257e44d42713373077beec856890db8
5
5
  SHA512:
6
- metadata.gz: 3767f16729e8df72d87bdb4cecc8052228e80f7bb02259663035a6491ca8671e7ffeb89e0e75969bfc63ee5066ec09859232c70b20282dc6dea112b00a896623
7
- data.tar.gz: 632c012c1a49d99e6b65d6ddec27ec02c2d7b94330b9aa42e0bdec72aea3b5739463b2fa10b74cab64f0c8f0c0cb49ee42fab3f354a6482b5bb3205decaa1856
6
+ metadata.gz: bb34fca037f6338abee76f32decb1188bdefc6a982b160ded012251bcf758796b4e47e69603933f3a0f057493ea62b85c4a14cd5acff65260a118bfebcdf8c5d
7
+ data.tar.gz: 8a28967ed6a25915f2d02ebbd5b64ec6ceb4a403d675ef6846769773c2de06c0d7c99326dce06e2262ad01c38b5e69ddb827465cd3182d86efba5fc2fc10516e
data/README.md CHANGED
@@ -2004,6 +2004,38 @@ record = Record.new(ratings: { quality: 3 })
2004
2004
  record.ratings # [{ :name=>:quality, :value=>3 }]
2005
2005
  ```
2006
2006
 
2007
+ Setting attributes with other names:
2008
+
2009
+ ```ruby
2010
+ # app/models/booking.rb
2011
+
2012
+ class Booking < LHS::Record
2013
+
2014
+ def appointments_attributes=(values)
2015
+ self.appointments = values.map { |appointment| appointment[:id] }
2016
+ end
2017
+ end
2018
+ ```
2019
+
2020
+ or
2021
+
2022
+ ```ruby
2023
+ # app/models/booking.rb
2024
+
2025
+ class Booking < LHS::Record
2026
+
2027
+ def appointments_attributes=(values)
2028
+ self[:appointments] = values.map { |appointment| appointment[:id] }
2029
+ end
2030
+ end
2031
+ ```
2032
+
2033
+ ```ruby
2034
+ # app/controllers/some_controller.rb
2035
+
2036
+ booking.update(params)
2037
+ ```
2038
+
2007
2039
  #### Record getters
2008
2040
 
2009
2041
  If you implement accompanying getter methods, the whole data conversion would be internal only:
@@ -18,8 +18,15 @@ class LHS::Proxy
18
18
 
19
19
  private
20
20
 
21
- def set(name, value)
21
+ def set(name, args)
22
22
  clear_cache!
23
+ return set_attribute_directly(name, args.try(:first)) if name != :[]=
24
+ key = args[0]
25
+ value = args[1]
26
+ _data._raw[key.to_sym] = value
27
+ end
28
+
29
+ def set_attribute_directly(name, value)
23
30
  key = name.to_s.gsub(/=$/, '')
24
31
  _data._raw[key.to_sym] = value
25
32
  end
@@ -41,7 +41,7 @@ class LHS::Item < LHS::Proxy
41
41
  protected
42
42
 
43
43
  def method_missing(name, *args, &_block)
44
- return set(name, args.try(&:first)) if name.to_s[/=$/]
44
+ return set(name, args) if name.to_s[/=$/]
45
45
  get(name, *args)
46
46
  end
47
47
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '21.1.3'
4
+ VERSION = '21.1.4'
5
5
  end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHS::Record do
6
+ context 'custom setters' do
7
+
8
+ context 'assigning values directly to other attributes' do
9
+
10
+ before do
11
+ Object.send(:remove_const, :Booking) # make sure there is no other booking from previous tests around
12
+ class Booking < LHS::Record
13
+ endpoint 'https://bookings'
14
+
15
+ def appointment_attributes=(params)
16
+ self.appointments = params.map { |item| item[:id] }
17
+ end
18
+ end
19
+ end
20
+
21
+ it 'allows to change raw in custom setters' do
22
+ booking = Booking.new(appointment_attributes: [{ id: 1 }])
23
+ expect(booking.appointments.to_a).to eq [1]
24
+ end
25
+ end
26
+
27
+ context 'assign values directly by using square brackets' do
28
+
29
+ before do
30
+ class Booking < LHS::Record
31
+ endpoint 'https://bookings'
32
+
33
+ def appointment_attributes=(params)
34
+ self[:appointments] = params.map { |item| item[:id] }
35
+ end
36
+ end
37
+ end
38
+
39
+ it 'allows to change raw in custom setters' do
40
+ booking = Booking.new(appointment_attributes: [{ id: 1 }])
41
+ expect(booking.appointments.to_a).to eq [1]
42
+ end
43
+ end
44
+ end
45
+ end
@@ -4,11 +4,10 @@ require "rails_helper"
4
4
  require 'capybara/rspec'
5
5
 
6
6
  describe 'form_for helper' do
7
- class Feedback < LHS::Record
8
- endpoint '{+datastore}/v2/feedbacks'
9
- end
10
-
11
7
  before do
8
+ class Feedback < LHS::Record
9
+ endpoint '{+datastore}/v2/feedbacks'
10
+ end
12
11
  assign(:instance, Feedback.new)
13
12
  end
14
13
 
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: 21.1.3
4
+ version: 21.1.4
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: 2020-02-19 00:00:00.000000000 Z
11
+ date: 2020-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -459,6 +459,7 @@ files:
459
459
  - spec/record/cast_nested_data_spec.rb
460
460
  - spec/record/create_spec.rb
461
461
  - spec/record/creation_failed_spec.rb
462
+ - spec/record/custom_setters_spec.rb
462
463
  - spec/record/definitions_spec.rb
463
464
  - spec/record/destroy_spec.rb
464
465
  - spec/record/dig_configuration_spec.rb
@@ -550,7 +551,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
550
551
  version: '0'
551
552
  requirements:
552
553
  - Ruby >= 2.3.0
553
- rubygems_version: 3.0.3
554
+ rubygems_version: 3.0.6
554
555
  signing_key:
555
556
  specification_version: 4
556
557
  summary: 'REST services accelerator: Rails gem providing an easy, active-record-like
@@ -675,6 +676,7 @@ test_files:
675
676
  - spec/record/cast_nested_data_spec.rb
676
677
  - spec/record/create_spec.rb
677
678
  - spec/record/creation_failed_spec.rb
679
+ - spec/record/custom_setters_spec.rb
678
680
  - spec/record/definitions_spec.rb
679
681
  - spec/record/destroy_spec.rb
680
682
  - spec/record/dig_configuration_spec.rb