Skip to content

Commit 9135607

Browse files
authored
Merge pull request #566 from mevo-limited/instance-level-dynamic-accessors
Instance level dynamic accessors
2 parents 85a7707 + 03c86f7 commit 9135607

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,13 @@ intercom = Intercom::Client.new(token: ENV['AT'], handle_rate_limit: true)
736736

737737
```bash
738738
# all tests
739-
bundle exec spec
739+
bundle exec rake spec
740740

741741
# unit tests
742-
bundle exec spec:unit
742+
bundle exec rake spec:unit
743743

744744
# integration tests
745-
bundle exec spec:integration
745+
bundle exec rake spec:integration
746746

747747
# single test file
748748
bundle exec m spec/unit/intercom/job_spec.rb

lib/intercom/lib/dynamic_accessors.rb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ module DynamicAccessors
55
class << self
66

77
def define_accessors(attribute, value, object)
8-
klass = object.class
98
if attribute.to_s.end_with?('_at') && attribute.to_s != 'update_last_request_at'
10-
define_date_based_accessors(attribute, value, klass)
9+
define_date_based_accessors(attribute, value, object)
1110
elsif object.flat_store_attribute?(attribute)
12-
define_flat_store_based_accessors(attribute, value, klass)
11+
define_flat_store_based_accessors(attribute, value, object)
1312
else
14-
define_standard_accessors(attribute, value, klass)
13+
define_standard_accessors(attribute, value, object)
1514
end
1615
end
1716

1817
private
1918

20-
def define_flat_store_based_accessors(attribute, value, klass)
21-
klass.class_eval %Q"
19+
def define_flat_store_based_accessors(attribute, value, object)
20+
object.instance_eval %Q"
2221
def #{attribute}=(value)
2322
mark_field_as_changed!(:#{attribute})
2423
@#{attribute} = Intercom::Lib::FlatStore.new(value)
@@ -29,8 +28,8 @@ def #{attribute}
2928
"
3029
end
3130

32-
def define_date_based_accessors(attribute, value, klass)
33-
klass.class_eval %Q"
31+
def define_date_based_accessors(attribute, value, object)
32+
object.instance_eval %Q"
3433
def #{attribute}=(value)
3534
mark_field_as_changed!(:#{attribute})
3635
@#{attribute} = value.nil? ? nil : value.to_i
@@ -41,8 +40,8 @@ def #{attribute}
4140
"
4241
end
4342

44-
def define_standard_accessors(attribute, value, klass)
45-
klass.class_eval %Q"
43+
def define_standard_accessors(attribute, value, object)
44+
object.instance_eval %Q"
4645
def #{attribute}=(value)
4746
mark_field_as_changed!(:#{attribute})
4847
@#{attribute} = value

spec/unit/intercom/contact_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@
274274

275275
describe 'nested resources' do
276276
let(:contact) { Intercom::Contact.new(id: '1', client: client) }
277+
let(:contact_no_tags) { Intercom::Contact.new(id: '2', client: client, tags: []) }
277278
let(:company) { Intercom::Company.new(id: '1') }
278279
let(:tag) { Intercom::Tag.new(id: '1') }
279280
let(:note) { Intercom::Note.new(body: "<p>Text for the note</p>") }
@@ -299,6 +300,27 @@
299300
_(proxy.resource_class).must_equal Intercom::Tag
300301
end
301302

303+
it 'returns correct tags from differring contacts' do
304+
client.expects(:get).with('/contacts/1/tags', {}).returns({
305+
'type' => 'tag.list',
306+
'tags' => [
307+
{
308+
'type' => 'tag',
309+
'id' => '1',
310+
'name' => 'VIP Customer'
311+
},
312+
{
313+
'type' => 'tag',
314+
'id' => '2',
315+
'name' => 'Test tag'
316+
}
317+
]
318+
})
319+
320+
_(contact_no_tags.tags.map{ |t| t.id }).must_equal []
321+
_(contact.tags.map{ |t| t.id }).must_equal ['1', '2']
322+
end
323+
302324
it 'returns a collection proxy for listing companies' do
303325
proxy = contact.companies
304326
_(proxy.resource_name).must_equal 'companies'

0 commit comments

Comments
 (0)