Class: MockedFixtures::MockConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/mocked_fixtures/mock_connection.rb

Defined Under Namespace

Classes: Column

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMockConnection

Returns a new instance of MockConnection.



13
14
15
16
17
# File 'lib/mocked_fixtures/mock_connection.rb', line 13

def initialize
  @schema  = @@schema ||= MockedFixtures::SchemaParser.load_schema
  @columns = {}
  @loaded_fixtures = {}
end

Instance Attribute Details

#current_fixture_labelObject

Returns the value of attribute current_fixture_label.



3
4
5
# File 'lib/mocked_fixtures/mock_connection.rb', line 3

def current_fixture_label
  @current_fixture_label
end

#loaded_fixturesObject

Returns the value of attribute loaded_fixtures.



3
4
5
# File 'lib/mocked_fixtures/mock_connection.rb', line 3

def loaded_fixtures
  @loaded_fixtures
end

#schemaObject

Returns the value of attribute schema.



3
4
5
# File 'lib/mocked_fixtures/mock_connection.rb', line 3

def schema
  @schema
end

Instance Method Details

#columns(table_name) ⇒ Object



57
58
59
# File 'lib/mocked_fixtures/mock_connection.rb', line 57

def columns(table_name)
  @columns[table_name] ||= @schema[table_name.to_s][:columns].collect {|c| Column.new(c[0], c[1]) }
end

#insert_fixture(fixture, table_name) ⇒ Object

stores full fixture after association values loaded and type casting



20
21
22
23
# File 'lib/mocked_fixtures/mock_connection.rb', line 20

def insert_fixture(fixture, table_name)
  loaded_fixtures[table_name] ||= {}
  loaded_fixtures[table_name][@current_fixture_label] = type_cast_fixture(fixture, table_name)
end

#type_cast_fixture(fixture, table_name) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mocked_fixtures/mock_connection.rb', line 25

def type_cast_fixture(fixture, table_name)
  fixture.to_hash.inject({}) do |new_hash, (k, v)|
    begin
      type = @schema[table_name.to_s][:columns].assoc(k)[1]
      new_hash[k.to_sym] = type_cast_value(type, v)
      new_hash
    rescue
      raise "Mock fixture key '#{k}' not found in schema '#{table_name}'"
    end
  end
end

#type_cast_value(type, value) ⇒ Object

Modified from ActiveRecord::Column class.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mocked_fixtures/mock_connection.rb', line 38

def type_cast_value(type, value)
  return nil if value.nil?
  column = ActiveRecord::ConnectionAdapters::Column
  case type.to_sym
    when :string    then value
    when :text      then value
    when :integer   then value.to_i rescue value ? 1 : 0
    when :float     then value.to_f
    when :decimal   then column.value_to_decimal(value)
    when :datetime  then column.string_to_time(value)
    when :timestamp then column.string_to_time(value)
    when :time      then column.string_to_dummy_time(value)
    when :date      then column.string_to_date(value)
    when :binary    then column.binary_to_string(value)
    when :boolean   then column.value_to_boolean(value)
    else value
  end
end