Module: LightRecord::RecordAttributes
- Defined in:
- lib/light_record.rb
Overview
ActiveRecord extension for class methods Defines klass.define_fields Overrides klass.column_names and klass.define_attribute_methods
Instance Method Summary collapse
-
#column_names ⇒ Object
Active record keep it as strings, but I keep it as symbols.
-
#define_attribute_methods ⇒ Object
used in Record#respond_to?.
- #define_fields(fields) ⇒ Object
Instance Method Details
#column_names ⇒ Object
Active record keep it as strings, but I keep it as symbols
103 104 105 |
# File 'lib/light_record.rb', line 103 def column_names @fields.map(&:to_s) end |
#define_attribute_methods ⇒ Object
used in Record#respond_to?
99 100 |
# File 'lib/light_record.rb', line 99 def define_attribute_methods end |
#define_fields(fields) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/light_record.rb', line 74 def define_fields(fields) @fields ||= [] fields.each do |field| field = field.to_sym unless field.is_a?(Symbol) @fields << field define_method(field) do @data[field] end # to avoid errors when try saving data define_method("#{field}=") do |value| @data[field] = value end end # ActiveRecord make method :id refers to primary key, even there is no column "id" if !fields.include?(:id) && !fields.include?("id") && primary_key.present? define_method(:id) do @data[self.class.primary_key.to_sym] end end end |