Module: ActiveZuora::Fields::ClassMethods

Defined in:
lib/active_zuora/fields.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, field) ⇒ Object



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

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



121
122
123
124
125
# File 'lib/active_zuora/fields.rb', line 121

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

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



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_zuora/fields.rb', line 99

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 :date     then DateField.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)


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

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

#field_namesObject



77
78
79
# File 'lib/active_zuora/fields.rb', line 77

def field_names
  fields_by_name.keys
end

#fieldsObject



73
74
75
# File 'lib/active_zuora/fields.rb', line 73

def fields
  fields_by_name.values
end

#fields_by_nameObject



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

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

#get_field(name) ⇒ Object



91
92
93
# File 'lib/active_zuora/fields.rb', line 91

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

#get_field!(name) ⇒ Object



95
96
97
# File 'lib/active_zuora/fields.rb', line 95

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