Class: Dataset::Record::Fixture

Inherits:
Object
  • Object
show all
Defined in:
lib/dataset/record/fixture.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(meta, attributes, symbolic_name, session_binding) ⇒ Fixture

Returns a new instance of Fixture.



9
10
11
12
13
14
15
16
# File 'lib/dataset/record/fixture.rb', line 9

def initialize(meta, attributes, symbolic_name, session_binding)
  @meta            = meta
  @attributes      = attributes.stringify_keys
  @symbolic_name   = symbolic_name || object_id
  @session_binding = session_binding
  
  install_default_attributes!
end

Instance Attribute Details

#metaObject (readonly)

Returns the value of attribute meta.



7
8
9
# File 'lib/dataset/record/fixture.rb', line 7

def meta
  @meta
end

#session_bindingObject (readonly)

Returns the value of attribute session_binding.



7
8
9
# File 'lib/dataset/record/fixture.rb', line 7

def session_binding
  @session_binding
end

#symbolic_nameObject (readonly)

Returns the value of attribute symbolic_name.



7
8
9
# File 'lib/dataset/record/fixture.rb', line 7

def symbolic_name
  @symbolic_name
end

Instance Method Details

#createObject



18
19
20
21
# File 'lib/dataset/record/fixture.rb', line 18

def create
  record_class.connection.insert_fixture to_fixture, meta.table_name
  id
end

#idObject



23
24
25
# File 'lib/dataset/record/fixture.rb', line 23

def id
  @attributes['id']
end

#install_default_attributes!Object



45
46
47
48
# File 'lib/dataset/record/fixture.rb', line 45

def install_default_attributes!
  @attributes['id'] ||= symbolic_name.to_s.hash.abs
  install_timestamps!
end

#install_timestamps!Object



50
51
52
53
54
# File 'lib/dataset/record/fixture.rb', line 50

def install_timestamps!
  meta.timestamp_columns.each do |column|
    @attributes[column.name] = now(column) unless @attributes.key?(column.name)
  end
end

#now(column) ⇒ Object



56
57
58
# File 'lib/dataset/record/fixture.rb', line 56

def now(column)
  (ActiveRecord::Base.default_timezone == :utc ? Time.now.utc : Time.now).to_s(:db)
end

#record_classObject



27
28
29
# File 'lib/dataset/record/fixture.rb', line 27

def record_class
  meta.record_class
end

#to_fixtureObject



31
32
33
# File 'lib/dataset/record/fixture.rb', line 31

def to_fixture
  ::Fixture.new(to_hash, meta.class_name)
end

#to_hashObject



35
36
37
38
39
40
41
42
43
# File 'lib/dataset/record/fixture.rb', line 35

def to_hash
  hash = @attributes.dup
  hash[meta.inheritance_column] = meta.sti_name if meta.inheriting_record?
  record_class.reflections.each do |name, reflection|
    name = name.to_s
    add_reflection_attributes(hash, name, reflection) if hash[name]
  end
  hash
end