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 4 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
4 changes: 2 additions & 2 deletions lib/netsuite/records/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Customer
:estimated_budget, :fax, :fax_transactions, :first_name, :first_visit, :give_access, :global_subscription_status,
:group_pricing_list, :home_phone, :image, :is_budget_approved, :is_inactive, :is_person, :item_pricing_list, :keywords,
:language, :last_modified_date, :last_name, :last_page_visited, :last_visit, :middle_name, :mobile_phone,
:opening_balance, :opening_balance_account, :opening_balance_date, :parent, :partners_list,
:opening_balance, :opening_balance_account, :opening_balance_date, :partners_list,
: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_rep, :sales_team_list, :salutation, :send_email, :ship_complete, :shipping_item,
Expand All @@ -34,7 +34,7 @@ class Customer
read_only_fields :balance, :consol_balance, :deposit_balance, :consol_deposit_balance, :overdue_balance,
:consol_overdue_balance, :unbilled_orders, :consol_unbilled_orders

record_refs :custom_form, :entity_status, :partner, :category, :lead_source,
record_refs :custom_form, :entity_status, :parent, :partner, :category, :lead_source,
:price_level, :subsidiary

attr_reader :internal_id
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
4 changes: 2 additions & 2 deletions spec/netsuite/records/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
:estimated_budget, :fax, :fax_transactions, :first_name, :first_visit, :give_access, :global_subscription_status,
:group_pricing_list, :home_phone, :image, :is_budget_approved, :is_inactive, :is_person, :item_pricing_list, :keywords,
:language, :last_modified_date, :last_name, :last_page_visited, :last_visit, :middle_name, :mobile_phone,
:opening_balance, :opening_balance_account, :opening_balance_date, :overdue_balance, :parent, :partners_list,
:opening_balance, :opening_balance_account, :opening_balance_date, :overdue_balance, :partners_list,
: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_rep, :sales_team_list, :salutation, :send_email, :ship_complete, :shipping_item,
Expand All @@ -28,7 +28,7 @@

it 'has the right record_refs' do
[
:custom_form, :entity_status, :partner
:custom_form, :entity_status, :parent, :partner
].each do |record_ref|
expect(customer).to have_record_ref(record_ref)
end
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