Module: BigRecord::DynamicSchema

Defined in:
lib/big_record/dynamic_schema.rb

Class Method Summary

Instance Method Summary

Class Method Details

+ (Object) included(base)

:nodoc:



4
5
6
7
8
9
10
11
# File 'lib/big_record/dynamic_schema.rb', line 4

def self.included(base) #:nodoc:
  super

  base.alias_method_chain :column_for_attribute, :dynamic_schema
  base.alias_method_chain :attributes_from_column_definition, :dynamic_schema
  base.alias_method_chain :inspect, :dynamic_schema
  base.alias_method_chain :define_read_methods, :dynamic_schema
end

Instance Method Details

- (Object) add_dynamic_column(col)

Add an existing dynamic column to this record



23
24
25
26
27
# 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

- (Object) attributes_from_column_definition_with_dynamic_schema

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.



56
57
58
59
60
61
62
63
# 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

- (Object) column_for_attribute_with_dynamic_schema(name)

Returns the column object for the named attribute.



46
47
48
49
50
# 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

- (Object) column_names



41
42
43
# File 'lib/big_record/dynamic_schema.rb', line 41

def column_names
  @column_names ||= columns_hash.keys
end

- (Object) columns



37
38
39
# File 'lib/big_record/dynamic_schema.rb', line 37

def columns
  @columns ||= columns_hash.values
end

- (Object) columns_hash



29
30
31
32
33
34
35
# 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

- (Object) define_read_methods_with_dynamic_schema

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.



78
79
80
81
82
83
84
85
86
87
88
# 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

- (Object) dynamic_column(name, type, options = {})

Create and add a dynamic column to this record



18
19
20
# 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

- (Object) initialize_columns(options = {})

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



14
15
# File 'lib/big_record/dynamic_schema.rb', line 14

def initialize_columns(options={})
end

- (Object) inspect_with_dynamic_schema

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



66
67
68
69
70
71
72
73
# 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