Class: ActiveRecordSchema::Schema

Inherits:
Object
  • Object
show all
Includes:
SchemaDiff
Defined in:
lib/active_record_schema/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SchemaDiff

#_column_names, #_connection, #_diff_fields_add, #_diff_indexes_add, #_diff_joins_add, #_index_exists?, #_table, #_table_exists?, #_table_names, #diff, #nothing_to_do?, #prefixed_table_name, #table_exists?

Constructor Details

#initialize(model) ⇒ Schema

Returns a new instance of Schema.



12
13
14
15
16
17
# File 'lib/active_record_schema/schema.rb', line 12

def initialize(model)
  @model   = model
  @fields  = ActiveSupport::OrderedHash.new
  @indexes = {}
  @joins   = {}
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



10
11
12
# File 'lib/active_record_schema/schema.rb', line 10

def fields
  @fields
end

#indexesObject (readonly)

Returns the value of attribute indexes.



10
11
12
# File 'lib/active_record_schema/schema.rb', line 10

def indexes
  @indexes
end

#joinsObject (readonly)

Returns the value of attribute joins.



10
11
12
# File 'lib/active_record_schema/schema.rb', line 10

def joins
  @joins
end

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/active_record_schema/schema.rb', line 10

def model
  @model
end

Instance Method Details

#add_field(column, type, options) ⇒ Object



39
40
41
# File 'lib/active_record_schema/schema.rb', line 39

def add_field(column, type, options)
  @fields[:"#{column}"]  = Field.new(column, type, options)
end

#add_index(column, options = {}) ⇒ Object



43
44
45
# File 'lib/active_record_schema/schema.rb', line 43

def add_index(column, options = {})
  @indexes[:"#{column}"] = Index.new(column, options)
end

#add_join(table, key1, key2, index = true) ⇒ Object



47
48
49
# File 'lib/active_record_schema/schema.rb', line 47

def add_join(table, key1, key2, index = true)
  @joins[:"#{table}"] = Join.new(table, key1, key2) 
end

#field_namesObject



35
36
37
# File 'lib/active_record_schema/schema.rb', line 35

def field_names
  fields.values.map(&:name).map(&:to_s)
end

#hierarchy_field_namesObject



31
32
33
# File 'lib/active_record_schema/schema.rb', line 31

def hierarchy_field_names
  hierarchy_fields.values.map(&:name).map(&:to_s)
end

#hierarchy_fieldsObject



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_record_schema/schema.rb', line 19

def hierarchy_fields
  if @hierarchy_fields
    @hierarchy_fields
  else
    @hierarchy_fields ||= ActiveSupport::OrderedHash.new
    model.ancestors.select { |c| c < ActiveRecord::Base }.reverse_each do |klass|
      @hierarchy_fields = @hierarchy_fields.merge(klass.schema.fields)
    end
    @hierarchy_fields  
  end
end