module Spreedly::Fields::ClassMethods

Public Instance Methods

add_accessor_for(f, field_type) click to toggle source
# File lib/spreedly/common/fields.rb, line 43
def add_accessor_for(f, field_type)
  case field_type
  when :boolean
    add_boolean_accessor(f)
  when :date_time
    add_date_time_accessor(f)
  when :integer
    add_integer_accessor(f)
  when nil
    attr_reader f
  else
    raise "Unknown field type '#{options[:type]}' for field '#{f}'"
  end
end
add_boolean_accessor(f) click to toggle source
# File lib/spreedly/common/fields.rb, line 58
def add_boolean_accessor(f)
  define_method(f) do
    return nil unless instance_variable_get("@#{f}")
    "true" == instance_variable_get("@#{f}")
  end
  alias_method "#{f}?", f
end
add_date_time_accessor(f) click to toggle source
# File lib/spreedly/common/fields.rb, line 66
def add_date_time_accessor(f)
  define_method(f) do
    Time.parse(instance_variable_get("@#{f}")) if instance_variable_get("@#{f}")
  end
end
add_integer_accessor(f) click to toggle source
# File lib/spreedly/common/fields.rb, line 72
def add_integer_accessor(f)
  define_method(f) do
    return nil unless instance_variable_get("@#{f}")
    instance_variable_get("@#{f}").to_i
  end
end
field(*fields_to_add) click to toggle source
# File lib/spreedly/common/fields.rb, line 26
def field(*fields_to_add)
  options = fields_to_add.extract_options!
  @fields ||= []
  fields_to_add.each do |f|
    @fields += [ f ]
    add_accessor_for(f, options[:type])
  end
end
fields() click to toggle source
# File lib/spreedly/common/fields.rb, line 35
def fields
  @fields ||= []
end
inherited(subclass) click to toggle source
# File lib/spreedly/common/fields.rb, line 39
def inherited(subclass)
  subclass.instance_variable_set("@fields", instance_variable_get("@fields"))
end