Module: Spreedly::Fields::ClassMethods
- Defined in:
- lib/spreedly/common/fields.rb
Instance Method Summary collapse
- #add_accessor_for(f, field_type) ⇒ Object
- #add_boolean_accessor(f) ⇒ Object
- #add_date_time_accessor(f) ⇒ Object
- #add_integer_accessor(f) ⇒ Object
- #field(*fields_to_add) ⇒ Object
- #fields ⇒ Object
- #inherited(subclass) ⇒ Object
Instance Method Details
#add_accessor_for(f, field_type) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/spreedly/common/fields.rb', line 40 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 '#{[:type]}' for field '#{f}'" end end |
#add_boolean_accessor(f) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/spreedly/common/fields.rb', line 55 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
63 64 65 66 67 |
# File 'lib/spreedly/common/fields.rb', line 63 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
69 70 71 72 73 74 |
# File 'lib/spreedly/common/fields.rb', line 69 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
23 24 25 26 27 28 29 30 |
# File 'lib/spreedly/common/fields.rb', line 23 def field(*fields_to_add) = fields_to_add. @fields ||= [] fields_to_add.each do |f| @fields += [ f ] add_accessor_for(f, [:type]) end end |
#fields ⇒ Object
32 33 34 |
# File 'lib/spreedly/common/fields.rb', line 32 def fields @fields ||= [] end |
#inherited(subclass) ⇒ Object
36 37 38 |
# File 'lib/spreedly/common/fields.rb', line 36 def inherited(subclass) subclass.instance_variable_set("@fields", instance_variable_get("@fields")) end |