Module: Fixie
Defined Under Namespace
Modules: Model
Constant Summary collapse
- MAX_ID =
2 ** 30 - 1
- VERSION =
"0.2.0"
Class Attribute Summary collapse
-
.dbs ⇒ Object
Returns the value of attribute dbs.
-
.dir ⇒ Object
Returns the value of attribute dir.
-
.fixtures ⇒ Object
Returns the value of attribute fixtures.
Class Method Summary collapse
- .all_fixtures ⇒ Object
- .fixture(db_name, table_name, fixture_name) ⇒ Object
- .identify(label) ⇒ Object
- .load_fixtures ⇒ Object
- .version ⇒ Object
Class Attribute Details
.dbs ⇒ Object
Returns the value of attribute dbs.
20 21 22 |
# File 'lib/fixie.rb', line 20 def dbs @dbs end |
.dir ⇒ Object
Returns the value of attribute dir.
20 21 22 |
# File 'lib/fixie.rb', line 20 def dir @dir end |
.fixtures ⇒ Object
Returns the value of attribute fixtures.
20 21 22 |
# File 'lib/fixie.rb', line 20 def fixtures @fixtures end |
Class Method Details
.all_fixtures ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/fixie.rb', line 36 def self.all_fixtures @all_fixtures ||= begin unless Dir.exists?(Fixie.dir) raise "There is no directory in the $LOAD_PATH with a 'fixtures' directory in it" end all_fixtures = {} now = Time.now.utc dbs.each do |db_name, db| all_fixtures[db_name] = {} # First pass, load all the fixtures Dir[File.join(Fixie.dir, "#{db_name}/*.yml")].each do |file| table_name = File.basename(file, '.yml') fixtures = YAML.load(ERB.new(IO.read(file)).result(binding)).symbolize_keys fixtures.each do |name, data| data["id"] ||= identify(name) end all_fixtures[db_name][table_name.to_sym] = fixtures end # Do a second pass to resolve associations and load data in DB all_fixtures[db_name].each do |table_name, fixtures| table = db[table_name] table_has_created_at = table.columns.include?(:created_at) table_has_updated_at = table.columns.include?(:updated_at) fixtures.each do |name, data| # Change attributes like city: baltimore to city_id: baltimore.id data.keys.each do |attr| associated_fixtures = all_fixtures[db_name][attr.to_s.pluralize.to_sym] if associated_fixtures && table.columns.include?("#{attr}_id".to_sym) associated_fixture = associated_fixtures[data[attr].to_sym] if associated_fixture data["#{attr}_id"] = associated_fixture['id'] data.delete(attr) end end data["created_at"] = now if table_has_created_at && !data.key?("created_at") data["updated_at"] = now if table_has_updated_at && !data.key?("updated_at") end # Set created_at/updated_at if they exist # Finally, put the data in the DB table.insert(data) end end end all_fixtures end end |
.fixture(db_name, table_name, fixture_name) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/fixie.rb', line 97 def self.fixture(db_name, table_name, fixture_name) db = all_fixtures[db_name] if db fixtures = db[table_name] if fixtures fixture = fixtures[fixture_name] if fixture fixture else raise "No fixture #{fixture_name.inspect} in #{db_name}.#{table_name}" end else raise "No fixtures for #{table_name.inspect} in #{db_name.inspect}" end else raise "Unknown fixture database #{db_name.inspect}" end end |
.identify(label) ⇒ Object
11 12 13 |
# File 'lib/fixie.rb', line 11 def self.identify(label) Zlib.crc32(label.to_s) % MAX_ID end |
.load_fixtures ⇒ Object
116 117 118 |
# File 'lib/fixie.rb', line 116 def self.load_fixtures Fixie.all_fixtures end |
.version ⇒ Object
15 16 17 |
# File 'lib/fixie.rb', line 15 def self.version VERSION end |