Module: Spreedly::Fields::ClassMethods

Defined in:
lib/spreedly/common/fields.rb

Instance Method Summary collapse

Instance Method Details

#add_accessor_for(f, field_type) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
# 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) ⇒ Object



66
67
68
69
70
# 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) ⇒ Object



72
73
74
75
76
77
# 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) ⇒ Object



26
27
28
29
30
31
32
33
# 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

#fieldsObject



35
36
37
# File 'lib/spreedly/common/fields.rb', line 35

def fields
  @fields ||= []
end

#inherited(subclass) ⇒ Object



39
40
41
# File 'lib/spreedly/common/fields.rb', line 39

def inherited(subclass)
  subclass.instance_variable_set("@fields", instance_variable_get("@fields"))
end