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
20
21
22
23
# 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|
    # not all custom fields have an id
    # https://github.com/NetSweet/netsuite/issues/182

    if reference_id = custom_field.send(reference_id_type)
      @custom_fields_assoc[reference_id.to_sym] = custom_field
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/netsuite/records/custom_field_list.rb', line 47

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



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

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"


43
44
45
# File 'lib/netsuite/records/custom_field_list.rb', line 43

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

#delete_custom_field(field) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/netsuite/records/custom_field_list.rb', line 29

def delete_custom_field(field)
  custom_fields.delete_if do |c|
    # https://github.com/NetSweet/netsuite/issues/325
    c.send(reference_id_type) &&
      c.send(reference_id_type).to_sym == field
  end

  @custom_fields_assoc.delete(field)
end

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

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/netsuite/records/custom_field_list.rb', line 63

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

#to_recordObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/netsuite/records/custom_field_list.rb', line 68

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
      }

      # TODO this is broken in > 2013_1; need to conditionally change the synax here
      # if NetSuite::Configuration.api_version < "2013_2"

      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