class NSConnector::Resource

This is a 'meta' class that all our useful NetSuite classes inherit from, overriding what they may need to. For example:

class Contact < Resource
        # The NetSuite internal id for the object.
        @type_id = 'contact'
end

Attributes

fields[R]
sublists[R]
type_id[R]

Provides accessibility to class instance variables

store[RW]

Public Class Methods

all() click to toggle source

Retrieve all records, will most likely become a chunked search due to size

# File lib/ns_connector/resource.rb, line 226
def all
        advanced_search([])
end
delete!(id) click to toggle source

Delete a single ID from NetSuite

Returns

Nothing useful

Raises

Relevant exceptions on failure

# File lib/ns_connector/resource.rb, line 191
def delete! id
        NSConnector::Restlet.execute!(
                :action => 'delete',
                :type_id => type_id,
                :data => {'id' => Integer(id)}
        )
end
find(id) click to toggle source

Retrieve a single resource from NetSuite with id

# File lib/ns_connector/resource.rb, line 200
def find id
        self.new(
                NSConnector::Restlet.execute!(
                        :action => 'retrieve',
                        :type_id => type_id,
                        :fields => fields,
                        :data => {'id' => Integer(id)}
                ),
                true
        )
end
find_by(field, value) click to toggle source

Return a single resource, by searching for the given field.

Returns

A single record

Raises

NSConnector::Errors::NotFound when nothing found

# File lib/ns_connector/resource.rb, line 215
def find_by(field, value)
        results = search_by(field, value)
        unless results.empty? then
                return results.first
        else
                raise NSConnector::Errors::NotFound
        end
end
new(upstream_store=nil, in_netsuite=false) click to toggle source
# File lib/ns_connector/resource.rb, line 25
def initialize upstream_store=nil, in_netsuite=false
        upstream_store.stringify_keys! if upstream_store

        @store = (upstream_store || {})
        @sublist_store = {}

        # This is set so that we can tell wether we need to create an
        # entirely new netstuite object, or we are modifying an
        # existing one.
        @in_netsuite = in_netsuite

        check_id10t_errors!
        create_store_accessors!
        create_sublist_accessors!
end
search_by(field, value) click to toggle source

Perform a search by field, with value matching exactly

# File lib/ns_connector/resource.rb, line 231
def search_by field, value
        advanced_search([[field, nil, 'is', value]])
end

Public Instance Methods

attach!(klass, ids, attributes=nil) click to toggle source

Attach ids on target klass to this record

Arguments
klass

Target class to attach to, e.g. Contact

ids

Array of ids to attach

attributes

Optional attributes for attach, e.g. {:role => -5}

Example

contact.attach!(Customer, [1198], {:role => 1})

# File lib/ns_connector/resource.rb, line 88
def attach!(klass, ids, attributes=nil)
        raise ::ArgumentError, 'Need an id to attach!' unless id
        self.class.attach!(klass, id, ids, attributes)
end
check_id10t_errors!() click to toggle source

Just so I don't forget to define certain things.

# File lib/ns_connector/resource.rb, line 42
def check_id10t_errors!
        unless fields then
                raise ::ArgumentError,
                        "Inherited class #{self.class} needs to "\
                        "define @fields class instance variable"
        end
        # Type doesn't matter
        unless fields.include? 'id' or fields.include? :id
                raise ::ArgumentError,
                        "Inherited class #{self.class} must define "\
                        "an 'id' field"
        end
        unless type_id then
                raise ::ArgumentError,
                        "Inherited class #{self.class} needs to "\
                        "define @type_id class instance variable"
        end
        unless sublists then
                raise ::ArgumentError,
                        "Inherited class #{self.class} needs to "\
                        "define @sublists class instance variable"
        end
end
delete!() click to toggle source

Delete ourself from NetSuite

Returns
true

If object deleted

false

If object was not deleted as it never existed

# File lib/ns_connector/resource.rb, line 167
def delete!
        return false unless in_netsuite? 
        fail 'Sanity check: resource should have an ID' unless id
        self.class.delete!(id)

        # We set our :id to nil as we don't have one anymore and it
        # allows us to call save on our newly deleted record, in case
        # we wanted to undelete or something crazy like that.
        @store[:id] = nil
        @in_netsuite = false

        return true
end
detach!(klass, ids) click to toggle source

Detach ids on target klass to this record

Arguments
klass

Target class to detach from, i.e. Contact

ids

Array of ids to detach

# File lib/ns_connector/resource.rb, line 97
def detach!(klass, ids)
        raise ::ArgumentError, 'Need an id to detach!' unless id
        self.class.detach!(klass, id, ids)
end
fields() click to toggle source

List of all fields for class

# File lib/ns_connector/resource.rb, line 72
def fields
        self.class.fields
end
in_netsuite?() click to toggle source

Is this resource already in NetSuite?

Returns
true

if this resource has been retrieved from netsuite,

false

if it is a new resource being created for the first time.

# File lib/ns_connector/resource.rb, line 121
def in_netsuite?
        @in_netsuite
end
inspect() click to toggle source

Format an object like: '#<NSConnector::PseudoResource:1>'

# File lib/ns_connector/resource.rb, line 113
def inspect
        "#<NSConnector::#{self.class}:#{id.inspect}>"
end
save!() click to toggle source

Save ourself to NetSuite.

Raises

NSConnector::Errors various errors if something explodes

Returns

true

# File lib/ns_connector/resource.rb, line 129
def save!
        # Convert all of our sublist objects to hashes
        sublist_data = Hash[@sublist_store.map {|sublist_id, objects|
                [sublist_id, objects.map {|object|
                        object.to_hash
                }]
        }]

        @store = NSConnector::Restlet.execute!(
                :action => in_netsuite? ? 'update' : 'create',
                :type_id => type_id,
                :fields => fields,
                :data => @store,
                :sublists => sublist_data,
        )

        # If we got this far, we're probably in NetSuite
        @in_netsuite = true

        # Now we save our sublist(s)
        @sublist_store.each do |sublist_id, sublist_items|
                # Overwriting the current item
                @sublist_store[sublist_id] = NSConnector::SubList.save!(
                        sublist_items, 
                        self,
                        sublist_id,
                        sublists[sublist_id]
                )
        end

        return true
end
sublists() click to toggle source

List of all sublists for class

# File lib/ns_connector/resource.rb, line 77
def sublists
        self.class.sublists
end
transform!(klass, &block) click to toggle source

Transform this instance into target klass

Arguments
klass

Target class, e.g. CustomerPayment

&block

optional block, will recieve an instance of target klass

to perform optional modifications to the object before it is
saved in NetSuite, like setting payment details.
# File lib/ns_connector/resource.rb, line 108
def transform!(klass, &block)
        self.class.transform!(klass, id, &block)
end
type_id() click to toggle source

Retrieve class's internal id, e.g. 'contact' for a Contact Resource

# File lib/ns_connector/resource.rb, line 67
def type_id
        self.class.type_id
end

Private Instance Methods

create_sublist_accessors!() click to toggle source

Given a sublist of {:addressbook => ['fields']} we want a method addressbook that looks up the sublist if we have an ID, otherwise returns the empty array.

And finally we need a method to create new sublist objects that is generic and not too crazy. So we have new_addressbook that returns a SubList object that can be stored and later turned into a hash to send to NetSuite.

# File lib/ns_connector/resource.rb, line 318
def create_sublist_accessors!
        sublists.each do |sublist_name, fields|
                self.class.class_eval do
                        define_method sublist_name do
                                # We are an object in netsuite,
                                # we might just have sublist
                                # items already. So we check.
                                @sublist_store[sublist_name] ||= \
                                        NSConnector::SubList.fetch(
                                                self,
                                                sublist_name,
                                                fields
                                        ) if in_netsuite?

                                @sublist_store[sublist_name] ||= []
                        end

                        define_method("#{sublist_name}=") do |value|
                                @sublist_store[sublist_name] = value
                        end

                        define_method(
                                "new_#{sublist_name}_item"
                        ) do |upstream_store = nil|
                                NSConnector::SubListItem.new(
                                        sublist_name,
                                        fields,
                                        self,
                                        upstream_store
                                )
                        end
                end
        end
end