Module: ColumnsOnDemand::InstanceMethods
- Defined in:
- lib/columns_on_demand.rb
Instance Method Summary collapse
- #_read_attribute(attr_name, &block) ⇒ Object
- #_write_attribute(attr_name, value) ⇒ Object
- #attribute_changed_in_place?(attr_name) ⇒ Boolean
- #attribute_names ⇒ Object
- #attributes ⇒ Object
- #changed_in_place?(attr_name) ⇒ Boolean
- #column_loaded?(attr_name) ⇒ Boolean
- #columns_loaded ⇒ Object
- #ensure_loaded(attr_name) ⇒ Object
- #load_attributes(*attr_names) ⇒ Object
- #missing_attribute(attr_name, *args) ⇒ Object
- #read_attribute(attr_name, &block) ⇒ Object
- #read_attribute_before_type_cast(attr_name) ⇒ Object
- #reload(*args) ⇒ Object
- #write_attribute(attr_name, value) ⇒ Object
Instance Method Details
#_read_attribute(attr_name, &block) ⇒ Object
105 106 107 108 |
# File 'lib/columns_on_demand.rb', line 105 def _read_attribute(attr_name, &block) ensure_loaded(attr_name) super(attr_name, &block) end |
#_write_attribute(attr_name, value) ⇒ Object
115 116 117 118 |
# File 'lib/columns_on_demand.rb', line 115 def _write_attribute(attr_name, value) ensure_loaded(attr_name) super(attr_name, value) end |
#attribute_changed_in_place?(attr_name) ⇒ Boolean
91 92 93 |
# File 'lib/columns_on_demand.rb', line 91 def attribute_changed_in_place?(attr_name) column_loaded?(attr_name) && super(attr_name) end |
#attribute_names ⇒ Object
63 64 65 |
# File 'lib/columns_on_demand.rb', line 63 def attribute_names (super + columns_to_load_on_demand).uniq.sort end |
#attributes ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/columns_on_demand.rb', line 54 def attributes loaded_attributes = @attributes.keys if loaded_attributes.size == self.class.columns_to_load_by_default.size && loaded_attributes.size == (self.class.columns_to_load_by_default & loaded_attributes).size load_attributes(*columns_to_load_on_demand.reject {|attr_name| column_loaded?(attr_name)}) end super end |
#changed_in_place?(attr_name) ⇒ Boolean
87 88 89 |
# File 'lib/columns_on_demand.rb', line 87 def changed_in_place?(attr_name) column_loaded?(attr_name) && super(attr_name) end |
#column_loaded?(attr_name) ⇒ Boolean
50 51 52 |
# File 'lib/columns_on_demand.rb', line 50 def column_loaded?(attr_name) !columns_to_load_on_demand.include?(attr_name) || @attributes.key?(attr_name) || new_record? || columns_loaded.include?(attr_name) end |
#columns_loaded ⇒ Object
46 47 48 |
# File 'lib/columns_on_demand.rb', line 46 def columns_loaded @columns_loaded ||= Set.new end |
#ensure_loaded(attr_name) ⇒ Object
83 84 85 |
# File 'lib/columns_on_demand.rb', line 83 def ensure_loaded(attr_name) load_attributes(attr_name.to_s) unless column_loaded?(attr_name.to_s) end |
#load_attributes(*attr_names) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/columns_on_demand.rb', line 67 def load_attributes(*attr_names) return if attr_names.blank? values = self.class.connection.select_rows( "SELECT #{attr_names.collect {|attr_name| self.class.connection.quote_column_name(attr_name)}.join(", ")}" + " FROM #{self.class.quoted_table_name}" + " WHERE #{self.class.connection.quote_column_name(self.class.primary_key)} = #{self.class.connection.quote(id)}") row = values.first || raise(ActiveRecord::RecordNotFound, "Couldn't find #{self.class.name} with ID=#{id}") attr_names.each_with_index do |attr_name, i| columns_loaded << attr_name value = row[i] @attributes.write_from_database(attr_name, value) end end |
#missing_attribute(attr_name, *args) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/columns_on_demand.rb', line 120 def missing_attribute(attr_name, *args) if columns_to_load_on_demand.include?(attr_name) load_attributes(attr_name) else super(attr_name, *args) end end |
#read_attribute(attr_name, &block) ⇒ Object
95 96 97 98 |
# File 'lib/columns_on_demand.rb', line 95 def read_attribute(attr_name, &block) ensure_loaded(attr_name) super(attr_name, &block) end |
#read_attribute_before_type_cast(attr_name) ⇒ Object
100 101 102 103 |
# File 'lib/columns_on_demand.rb', line 100 def read_attribute_before_type_cast(attr_name) ensure_loaded(attr_name) super(attr_name) end |
#reload(*args) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/columns_on_demand.rb', line 128 def reload(*args) super(*args).tap do columns_loaded.clear columns_to_load_on_demand.each do |attr_name| if @attributes.respond_to?(:reset) # 4.2 and above @attributes.reset(attr_name) else # 4.1 and earlier @attributes.delete(attr_name) end end end end |
#write_attribute(attr_name, value) ⇒ Object
110 111 112 113 |
# File 'lib/columns_on_demand.rb', line 110 def write_attribute(attr_name, value) ensure_loaded(attr_name) super(attr_name, value) end |