Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Equality and better metaprogramming for RecordRef. #167

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/netsuite/records/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Customer
:password, :password2, :phone, :phonetic_name, :pref_cc_processor, :print_on_check_as,
:print_transactions, :referrer, :reminder_days, :representing_subsidiary, :require_pwd_change, :resale_number,
:sales_group, :sales_readiness, :sales_team_list, :salutation, :send_email, :ship_complete, :shipping_item,
:stage, :start_date, :subscriptions_list, :sync_partner_teams, :tax_exempt, :tax_item, :taxable,
:stage, :start_date, :subscriptions_list, :sync_partner_teams, :tax_exempt, :tax_item, :taxable,
:territory, :third_party_acct, :third_party_country, :third_party_zipcode, :title, :url,
:vat_reg_number, :visits, :web_lead

Expand Down
17 changes: 16 additions & 1 deletion lib/netsuite/records/record_ref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,25 @@ def initialize(attributes_or_record = {})
@type = attributes.delete(:type) || attributes.delete(:@type) || attributes.delete(:"@xsi:type")
@attributes = attributes
else
record = attributes_or_record
record = attributes_or_record
@internal_id = record.internal_id if record.respond_to?(:internal_id)
@external_id = record.external_id if record.respond_to?(:external_id)
@type = record.class.to_s.split('::').last.lower_camelcase
end
end

def ==(other)
other.class == self.class &&
other.internal_id != nil && self.internal_id != nil &&
other.internal_id.to_s == self.internal_id.to_s
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable to me


alias_method :eql?, :==

def hash
internal_id.to_s.hash
end

def method_missing(m, *args, &block)
if attributes.keys.map(&:to_sym).include?(m.to_sym)
attributes[m.to_sym]
Expand All @@ -33,6 +45,9 @@ def method_missing(m, *args, &block)
end
end

def respond_to_missing?(m, include_all=false)
attributes.keys.map(&:to_sym).include?(m.to_sym) || super
end
end
end
end
2 changes: 1 addition & 1 deletion spec/netsuite/records/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
:password, :password2, :phone, :phonetic_name, :pref_cc_processor,:print_on_check_as,
:print_transactions, :referrer, :reminder_days, :representing_subsidiary, :require_pwd_change, :resale_number,
:sales_group, :sales_readiness, :sales_team_list, :salutation, :send_email, :ship_complete, :shipping_item,
:stage, :start_date, :subscriptions_list, :sync_partner_teams, :tax_exempt, :tax_item, :taxable,
:stage, :start_date, :subscriptions_list, :sync_partner_teams, :tax_exempt, :tax_item, :taxable,
:territory, :third_party_acct, :third_party_country, :third_party_zipcode, :title, :unbilled_orders, :url,
:vat_reg_number, :visits, :web_lead
].each do |field|
Expand Down
50 changes: 47 additions & 3 deletions spec/netsuite/records/record_ref_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,51 @@
expect(record_ref.banana).to eql('for monkeys')
end
end

context 'responding to' do
it 'responds to the arbitrary attributes' do
expect(record_ref).to respond_to(:name)
expect(record_ref).to respond_to(:banana)
end
it 'continues to respond to other methods' do
expect(record_ref).to respond_to(:internal_id)
expect(record_ref).to respond_to(:to_s)
end
end
end

describe 'equality' do
context 'when internal id is nil' do
subject { NetSuite::Records::RecordRef.new(:name => 'name!') }
it { should_not eq(record_ref) }
it { should_not eq(NetSuite::Records::RecordRef.new(:name => 'name!')) }
it { should_not eq(NetSuite::Records::RecordRef.new(:name => 'other!')) }
end
context 'when internal id is not nil' do
subject { NetSuite::Records::RecordRef.new(:internal_id => '5', :name => 'name!') }
it { should_not eq(NetSuite::Records::RecordRef.new(:internal_id => '9', :name => 'name!')) }
it { should eq(NetSuite::Records::RecordRef.new(:internal_id => '5')) }
it { should eq(NetSuite::Records::RecordRef.new(:internal_id => 5)) }
it { should_not eq(NetSuite::Records::RecordRef.new(:name => 'name!')) }
it { should eq(NetSuite::Records::RecordRef.new(:internal_id => '5', :name => 'name!')) }
it { should eq(NetSuite::Records::RecordRef.new(:internal_id => '5', :name => 'other')) }
it { should_not eq(Struct.new(:internal_id).new('5')) }
end
end

describe '#hash' do
context 'when the internal id is a string' do
subject { NetSuite::Records::RecordRef.new(:internal_id => '5') }
it 'hashes the internal id' do
expect(subject.hash).to eq('5'.hash)
end
end
context 'when the internal id is an integer' do
subject { NetSuite::Records::RecordRef.new(:internal_id => 5) }
it 'hashes the internal id as a string' do
expect(subject.hash).to eq('5'.hash)
end
end
end

describe 'untouchables' do
Expand All @@ -60,7 +105,7 @@

describe 'initialize from record' do
it 'initializes a new ref with the proper attributes from the record' do
record = NetSuite::Records::Classification.new(:is_inactive => false, :name => 'Retail', :internal_id => '9')
record = NetSuite::Records::Classification.new(:is_inactive => false, :name => 'Retail', :internal_id => '9')
record_ref = NetSuite::Records::RecordRef.new(record)
expect(record_ref).to be_kind_of(NetSuite::Records::RecordRef)
expect(record_ref.internal_id).to eql('9')
Expand All @@ -71,7 +116,7 @@
describe '#to_record' do
it 'can represent itself as a SOAP record' do
record_ref = NetSuite::Records::RecordRef.new(:something => 'blah')
record = {
record = {
'platformCore:something' => 'blah'
}
expect(record_ref.to_record).to eql(record)
Expand All @@ -83,5 +128,4 @@
expect(record_ref.record_type).to eql('platformCore:RecordRef')
end
end

end