Class: NetSuite::Records::CustomFieldList

Inherits:
Object
  • Object
show all
Includes:
Namespaces::PlatformCore
Defined in:
lib/netsuite/records/custom_field_list.rb

Instance Method Summary collapse

Methods included from Namespaces::PlatformCore

#record_namespace

Constructor Details

#initialize(attributes = {}) ⇒ CustomFieldList

Returns a new instance of CustomFieldList.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/netsuite/records/custom_field_list.rb', line 6

def initialize(attributes = {})
  case attributes[:custom_field]
  when Hash
    extract_custom_field(attributes[:custom_field])
  when Array
    attributes[:custom_field].each { |custom_field| extract_custom_field(custom_field) }
  end
  
  @custom_fields_assoc = Hash.new
  custom_fields.each do |custom_field|
    reference_id = custom_field.script_id || custom_field.internal_id
    @custom_fields_assoc[reference_id.to_sym] = custom_field
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/netsuite/records/custom_field_list.rb', line 38

def method_missing(sym, *args, &block)
  # read custom field if already set
  if @custom_fields_assoc.include?(sym)
    return @custom_fields_assoc[sym]
  end

  # write custom field
  if sym.to_s.end_with?('=')
    field_name = sym.to_s[0..-2]
    delete_custom_field(field_name.to_sym)
    return create_custom_field(field_name, args.first)
  end

  super(sym, *args, &block)
end

Instance Method Details

#custom_fieldsObject



21
22
23
# File 'lib/netsuite/records/custom_field_list.rb', line 21

def custom_fields
  @custom_fields ||= []
end

#custom_fields_by_type(type) ⇒ Object

In case you want to get only MultiSelectCustomFieldRef for example:

list.custom_fields_by_type "MultiSelectCustomFieldRef"


34
35
36
# File 'lib/netsuite/records/custom_field_list.rb', line 34

def custom_fields_by_type(type)
  custom_fields.select { |field| field.type == "platformCore:#{type}" }
end

#delete_custom_field(field) ⇒ Object



25
26
27
28
# File 'lib/netsuite/records/custom_field_list.rb', line 25

def delete_custom_field(field)
  custom_fields.delete_if { |c| c.internal_id.to_sym == field }
  @custom_fields_assoc.delete(field)
end

#respond_to?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/netsuite/records/custom_field_list.rb', line 54

def respond_to?(sym, include_private = false)
  return true if @custom_fields_assoc.include?(sym)
  super
end

#to_recordObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/netsuite/records/custom_field_list.rb', line 59

def to_record
  {
    "#{record_namespace}:customField" => custom_fields.map do |custom_field|
      if custom_field.value.respond_to?(:to_record)
        custom_field_value = custom_field.value.to_record
      elsif custom_field.value.is_a?(Array)
        custom_field_value = custom_field.value.map(&:to_record)
      else
        custom_field_value = custom_field.value.to_s
      end

      base = {
        "platformCore:value" => custom_field_value,
        '@xsi:type' => custom_field.type
      }

      if custom_field.internal_id
        base['@internalId'] = custom_field.internal_id
      end

      if custom_field.script_id
        base['@scriptId'] = custom_field.script_id
      end

      base
    end
  }
end