Skip to content

Commit 9e74b12

Browse files
committed
Define respond_to? on RecordRef to match method_missing behavior
`RecordRef`s often include a name, which allows access via `method_missing` by calling `record_ref.name`, however if no referenced record is assigned, you'll still get a `RecordRef` instance, it just wont have any fields, so `record_ref.name` raised a `NoMethodError`, but if you checked for the field first via `record_ref.respond_to?(:name)`, it'd be falsey regardless of whether the field existed or not. Defining `respond_to?` in good practice alongside `method_missing`. Ideally, `respond_to_missing?` would be used instead, as that enables access to the method like `record_ref.method(:name)`, however I kept with `respond_to?` to match `CustomFieldList`. `CustomRecordRef` also uses `method_missing`, however there's no specs and I'm not familiar with that record, so I left it alone.
1 parent dd693a3 commit 9e74b12

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixed
88
* Revert recent proxy changes which breaks proxy usage by @andrewdicken-stripe in https://github.com/NetSweet/netsuite/pull/579
9+
* Define `respond_to?` on `RecordRef` to match `method_missing` behavior (#608)
910

1011
### Breaking Changes
1112

lib/netsuite/records/record_ref.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def method_missing(m, *args, &block)
3333
end
3434
end
3535

36+
def respond_to?(m, include_private = false)
37+
return true if attributes.keys.map(&:to_sym).include?(m.to_sym)
38+
super
39+
end
40+
3641
end
3742
end
3843
end

spec/netsuite/records/record_ref_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@
4040

4141
context 'readers' do
4242
it 'can take on arbitrary attributes into itself on initialization' do
43+
expect(record_ref).to respond_to(:name)
44+
expect(record_ref).to respond_to('name')
4345
expect(record_ref.name).to eql('This is a record_ref')
46+
47+
expect(record_ref).to respond_to(:banana)
48+
expect(record_ref).to respond_to('banana')
4449
expect(record_ref.banana).to eql('for monkeys')
50+
51+
expect(record_ref).to_not respond_to(:non_existant_field)
52+
expect(record_ref).to_not respond_to('non_existant_field')
4553
end
4654
end
4755
end

0 commit comments

Comments
 (0)