Class: ActiveZuora::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/active_zuora/fields/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, namespace, options = {}) ⇒ Field

Returns a new instance of Field.


6
7
8
9
10
11
# File 'lib/active_zuora/fields/field.rb', line 6

def initialize(name, namespace, options={})
  @name = name.to_s
  @namespace = namespace
  @zuora_name = options[:zuora_name] || @name.camelize
  @default = options[:default]
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.


4
5
6
# File 'lib/active_zuora/fields/field.rb', line 4

def default
  @default
end

#nameObject

Returns the value of attribute name.


4
5
6
# File 'lib/active_zuora/fields/field.rb', line 4

def name
  @name
end

#namespaceObject

Returns the value of attribute namespace.


4
5
6
# File 'lib/active_zuora/fields/field.rb', line 4

def namespace
  @namespace
end

#zuora_nameObject

Returns the value of attribute zuora_name.


4
5
6
# File 'lib/active_zuora/fields/field.rb', line 4

def zuora_name
  @zuora_name
end

Instance Method Details

#build_xml(xml, soap, value, options = {}) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_zuora/fields/field.rb', line 57

def build_xml(xml, soap, value, options={})
  qualifier = soap.namespace_by_uri(namespace)
  nil_strategy = options[:nil_strategy] || :omit
  # The extra qualifiers attribute needs to be passed in
  # in case the field is another complexType that needs
  # to be namespaced.
  if !value.nil? || nil_strategy == :whitespace
    xml.tag!(qualifier, zuora_name.to_sym, value.to_s)
  elsif nil_strategy == :fields_to_null
    xml.tag!(qualifier, :fieldsToNull, zuora_name.to_sym)
  end
end

#clear_changed_attributes(value) ⇒ Object


70
71
72
73
# File 'lib/active_zuora/fields/field.rb', line 70

def clear_changed_attributes(value)
  # If the value of this field has attribute changes to clear,
  # override this function.
end

#define_instance_methods(zuora_class) ⇒ Object


17
18
19
20
21
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
47
48
49
50
51
52
53
54
55
# File 'lib/active_zuora/fields/field.rb', line 17

def define_instance_methods(zuora_class)
  # We store this into a variable so we can use it in our
  # class eval below.
  field_name = name
  # Add dirty helpers
  # The value will still be stored in an instance
  # variable with the name of the field.
  # But we'll define extra writer methods so that we
  # can handle any input from savon.
  # Savon just calls underscore on the element names,
  # but our fields allow for any combination
  # of field name and Zuora name.
  # This is especially useful for custom fields which
  # are named like CustomField__c.  You might choose
  # to make this field :custom_field, and therefore
  # we'll need a writer for :custom_field, :custom_field_c,
  # and I threw in a :CustomField__c just for good measure.      
  writers = [field_name, zuora_name, zuora_name.underscore].uniq
  zuora_class.class_eval do
    # Define the methods on an included module, so we can override
    # them using super.
    generated_attribute_methods.module_eval do
      # Getter
      attr_reader field_name
      # Boolean check.
      define_method "#{field_name}?" do
        !!send(field_name)
      end
      # Writers
      writers.each do |writer_name|
        define_method "#{writer_name}=" do |value|
          write_attribute(field_name, value)
        end
      end
    end
    # Dirty attribute helpers.
    define_attribute_methods [field_name]
  end
end

#type_cast(value) ⇒ Object


13
14
15
# File 'lib/active_zuora/fields/field.rb', line 13

def type_cast(value)
  value
end