Module: ActiveZuora::Fields::ClassMethods

Defined in:
lib/active_zuora/fields.rb

Instance Method Summary collapse

Instance Method Details

#add_field(name, field) ⇒ Object



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

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



116
117
118
119
120
# File 'lib/active_zuora/fields.rb', line 116

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

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



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

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)


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

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

#field_namesObject



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

def field_names
  fields_by_name.keys
end

#fieldsObject



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

def fields
  fields_by_name.values
end

#fields_by_nameObject



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

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

#get_field(name) ⇒ Object



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

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

#get_field!(name) ⇒ Object



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

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