Module: NetSuite::Support::Fields::ClassMethods

Defined in:
lib/netsuite/support/fields.rb

Instance Method Summary collapse

Instance Method Details

#field(name, klass = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/netsuite/support/fields.rb', line 22

def field(name, klass = nil)
  name_sym = name.to_sym
  fields << name_sym
  if klass
    define_method(name_sym) do
      attributes[name_sym] ||= klass.new
    end

    define_method("#{name_sym}=") do |value|
      if value.nil?
        attributes.delete(name_sym)
      else
        attributes[name_sym] = value.kind_of?(klass) ? value : klass.new(value)
      end
    end
  else
    define_method(name_sym) do
      attributes[name_sym]
    end

    define_method("#{name_sym}=") do |value|
      attributes[name_sym] = value
    end
  end
end

#fields(*args) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/netsuite/support/fields.rb', line 12

def fields(*args)
  if args.empty?
     @fields ||= Set.new
  else
    args.each do |arg|
      field arg
    end
  end
end

#inherited(klass) ⇒ Object

a bit of trickery: this is for classes which inherit from other classes i.e. the AssemblyItem, KitItem, etc; this copies the superclass’s fields over



66
67
68
69
# File 'lib/netsuite/support/fields.rb', line 66

def inherited(klass)
  klass.instance_variable_set("@fields", self.fields)
  klass.instance_variable_set("@read_only_fields", self.read_only_fields)
end

#read_only_field(name) ⇒ Object



58
59
60
61
62
# File 'lib/netsuite/support/fields.rb', line 58

def read_only_field(name)
  name_sym = name.to_sym
  read_only_fields << name_sym
  field name
end

#read_only_fields(*args) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/netsuite/support/fields.rb', line 48

def read_only_fields(*args)
  if args.empty?
     @read_only_fields ||= Set.new
  else
    args.each do |arg|
      read_only_field arg
    end
  end
end