Module: ActiveZuora::Fields::ClassMethods

Defined in:
lib/active_zuora/fields.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, field) ⇒ Object


79
80
81
82
83
# File 'lib/active_zuora/fields.rb', line 79

def add_field(name, field)
  fields_by_name[name.to_sym] = field
  # Define the setters, getters, and changed helpers.
  field.define_instance_methods(self)
end

#default_attributesObject


114
115
116
117
118
# File 'lib/active_zuora/fields.rb', line 114

def default_attributes
  default_attributes = {}
  fields.each { |field| default_attributes[field.name] = field.default }
  default_attributes
end

#field(name, type, options = {}) ⇒ Object


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_zuora/fields.rb', line 93

def field(name, type, options={})
  # Check if this field should be an array, don't pass
  # this option down to the field.
  field_is_array = options.delete(:array) || false
  # Create and register the field.
  field = case type
    when :string then StringField.new(name, namespace, options)
    when :boolean then BooleanField.new(name, namespace, options)
    when :integer then IntegerField.new(name, namespace, options)
    when :decimal then DecimalField.new(name, namespace, options)
    when :datetime then DateTimeField.new(name, namespace, options)
    when :object
      class_name = options[:class_name] || nested_class_name(name.to_s.camelize)
      ObjectField.new(name, namespace, class_name, options)
    else
      ArgumentError.new "Unknown field type: #{type}"
    end
  field = ArrayFieldDecorator.new(field) if field_is_array
  add_field(name, field)
end

#field?(name) ⇒ Boolean

Returns:

  • (Boolean)

75
76
77
# File 'lib/active_zuora/fields.rb', line 75

def field?(name)
  fields_by_name.key?(name.to_sym)
end

#field_namesObject


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

def field_names
  fields_by_name.keys
end

#fieldsObject


67
68
69
# File 'lib/active_zuora/fields.rb', line 67

def fields
  fields_by_name.values
end

#fields_by_nameObject


62
63
64
65
# File 'lib/active_zuora/fields.rb', line 62

def fields_by_name
  # { :field_name_symbol => field_object }
  @fields ||= {}
end

#get_field(name) ⇒ Object


85
86
87
# File 'lib/active_zuora/fields.rb', line 85

def get_field(name)
  fields_by_name[name.to_sym]
end

#get_field!(name) ⇒ Object


89
90
91
# File 'lib/active_zuora/fields.rb', line 89

def get_field!(name)
  get_field(name) || raise(ArgumentError.new("No field in #{self} named #{name}"))
end