Sha256: aa9b1e9f76fea03925623ca21fba55f59ffdd35ed633eb009e23ce5afc05d14d
Contents?: true
Size: 1.72 KB
Versions: 1
Compression:
Stored size: 1.72 KB
Contents
require "pop/version" require "yaml" module Pop extend self # Populates a table with data in a filename. # # table - The Symbol that references a table name. # filename - The String that references the filename path. # # Examples: # # City # => City(id: integer, region_id: integer, name: string) # populate(:cities) # populate(:countries, "db/data/countries.yaml") # # Returns the populated data. def populate(table, filename = nil) model = modelize(table) filename ||= "#{Rails.root}/db/#{table}.yml" result = [] YAML.load_file(filename).each do |record| puts "Inserting #{record.inspect} into #{table.to_s.inspect}" result << model.create(record) end result end # Delete the records of the given tables. # # tables - The Array of tables represented by symbols. # # Examples: # # City.count #=> 1 # clean(:cities) # City.count #=> 0 # # Returns the given tables name. def clean(*tables) tables.each do |table| puts "Cleaning #{table.to_s.inspect}" modelize(table).delete_all end end # Clean and Populate a table. # # tables - The Array of tables represented by symbols # # Examples: # # setup :cities, :regions # # Returns the last populated data def setup(table, filename = nil) clean(table) populate(table, filename) end private # Tries to find a constant with the table name specified # in the argument symbol. # # table - The Symbol that references a table name. # # Examples: # # modelize(:cities) # # => City # # modelize(:people) # # => Person # # Returns the finded constant. def modelize(table) table.to_s.classify.constantize end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pop-0.0.1 | lib/pop.rb |