module BigRecord::DynamicSchema

Public Instance Methods

add_dynamic_column(col) click to toggle source

Add an existing dynamic column to this record

# File lib/big_record/dynamic_schema.rb, line 23
def add_dynamic_column(col)
  columns_hash[col.name] = col
  @columns_name= nil; @columns= nil #reset
  col
end
attributes_from_column_definition_with_dynamic_schema() click to toggle source

Initializes the attributes array with keys matching the columns from the linked table and the values matching the corresponding default value of that column, so that a new instance, or one populated from a passed-in Hash, still has all the attributes that instances loaded from the database would.

# File lib/big_record/dynamic_schema.rb, line 56
def attributes_from_column_definition_with_dynamic_schema
  self.columns.inject({}) do |attributes, column|
    unless column.name == self.class.primary_key
      attributes[column.name] = column.default
    end
    attributes
  end
end
column_for_attribute_with_dynamic_schema(name) click to toggle source

Returns the column object for the named attribute.

# File lib/big_record/dynamic_schema.rb, line 46
def column_for_attribute_with_dynamic_schema(name)
  name_string = name.to_s
  self.columns_hash[name_string] || self.columns_hash["#{self.class.default_column_prefix}#{name_string}"] ||
    self.columns.select{|c|c.alias==name_string}.first
end
column_names() click to toggle source
# File lib/big_record/dynamic_schema.rb, line 41
def column_names
  @column_names ||= columns_hash.keys
end
columns() click to toggle source
# File lib/big_record/dynamic_schema.rb, line 37
def columns
  @columns ||= columns_hash.values
end
columns_hash() click to toggle source
# File lib/big_record/dynamic_schema.rb, line 29
def columns_hash
  unless @columns_hash
    @columns_hash = self.class.columns_hash.dup
    initialize_columns
  end
  @columns_hash
end
define_read_methods_with_dynamic_schema() click to toggle source

Called on first read access to any given column and generates reader methods for all columns in the #columns_hash if ActiveRecord::Base.generate_read_methods is set to true.

# File lib/big_record/dynamic_schema.rb, line 78
def define_read_methods_with_dynamic_schema
  columns_hash.each do |name, column|
    unless respond_to_without_attributes?(name)
      define_read_method(name.to_sym, name, column)
    end

    unless respond_to_without_attributes?("#{name}?")
      define_question_method(name)
    end
  end
end
dynamic_column(name, type, options={}) click to toggle source

Create and add a dynamic column to this record

# File lib/big_record/dynamic_schema.rb, line 18
def dynamic_column(name, type, options={})
  add_dynamic_column ConnectionAdapters::Column.new(name.to_s, type, options)
end
initialize_columns(options={}) click to toggle source

Stub of the callback for setting the dynamic columns. Override this to add dynamic columns

# File lib/big_record/dynamic_schema.rb, line 14
def initialize_columns(options={})
end
inspect_with_dynamic_schema() click to toggle source

Returns the contents of the record as a nicely formatted string.

# File lib/big_record/dynamic_schema.rb, line 66
def inspect_with_dynamic_schema
  attributes_as_nice_string = self.column_names.collect { |name|
    if has_attribute?(name) || new_record?
      "#{name}: #{attribute_for_inspect(name)}"
    end
  }.compact.join(", ")
  "#<#{self.class} #{attributes_as_nice_string}>"
end