Module: Kaplan
- Defined in:
- lib/kaplan.rb,
lib/kaplan/railtie.rb,
lib/kaplan/version.rb
Defined Under Namespace
Modules: DatabaseAdapters, WebFrameworks Classes: Railtie
Constant Summary collapse
- LEVELS =
[:none, :info, :debug]
- VERSION =
"0.2.3"
Class Attribute Summary collapse
-
.database_orm ⇒ Object
readonly
Returns the value of attribute database_orm.
-
.web_framework ⇒ Object
readonly
Returns the value of attribute web_framework.
Class Method Summary collapse
- .detect_database_orm ⇒ Object
- .detect_web_framework ⇒ Object
- .plow_database(options = {}) ⇒ Object
- .seed_database(options = {}) ⇒ Object
- .seedable_collections(env, options = {}) ⇒ Object
- .seeds(env, options = {}) ⇒ Object
Class Attribute Details
.database_orm ⇒ Object (readonly)
Returns the value of attribute database_orm.
84 85 86 |
# File 'lib/kaplan.rb', line 84 def database_orm @database_orm end |
.web_framework ⇒ Object (readonly)
Returns the value of attribute web_framework.
84 85 86 |
# File 'lib/kaplan.rb', line 84 def web_framework @web_framework end |
Class Method Details
.detect_database_orm ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/kaplan.rb', line 86 def detect_database_orm if defined?(::ActiveRecord) @database_orm = :activerecord extend Kaplan::DatabaseAdapters::ActiveRecord elsif defined?(::Mongoid) @database_orm = :mongoid extend Kaplan::DatabaseAdapters::Mongoid end end |
.detect_web_framework ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/kaplan.rb', line 96 def detect_web_framework if defined?(::Rails) @web_framework = :rails extend Kaplan::WebFrameworks::Rails elsif defined?(::Padrino) @web_framework = :padrino extend Kaplan::WebFrameworks::Padrino end end |
.plow_database(options = {}) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/kaplan.rb', line 164 def plow_database(={}) level = LEVELS.index([:level] || :debug) .reverse_merge!(:env => current_environment) puts "Plowing the #{[:env]} database..." if level > 0 establish_database([:env]) collections = [:all] ? all_collections : seedable_collections([:env], :only => [:only]) collections.each do |coll| plow_collection(coll) puts " - Plowed #{coll}" if level > 1 end end |
.seed_database(options = {}) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/kaplan.rb', line 139 def seed_database(={}) level = LEVELS.index([:level] || :debug) .reverse_merge!(:env => current_environment) puts "Seeding the #{[:env]} database..." if level > 0 establish_database([:env]) seeds = seeds([:env], :only => [:only]) seeds.each do |filename, ext, collection_name, model| seed_database_for(filename, ext, collection_name, model) do puts " - Adding data for #{collection_name}..." if level > 1 && collection_name end # If the seed file sets an explicit id for a record, the next time you # attempt to create a record, Postgres will bomb because it still thinks # the sequence for the id starts at 1. So this will set the next value for # the sequence to the last id + 1. # # See: <http://stackoverflow.com/questions/1709705/postgresql-nextval-generating-existing-values> # if @database_orm == :activerecord && model && model.connection.adapter_name == "PostgreSQL" puts " - Resetting primary key sequence for #{collection_name}..." ActiveRecord::Base.connection.reset_pk_sequence!(collection_name) end end end |
.seedable_collections(env, options = {}) ⇒ Object
176 177 178 179 |
# File 'lib/kaplan.rb', line 176 def seedable_collections(env, ={}) # Remove collections that don't exist seeds(env, ).map {|filename, ext, collection_name, model| collection_name }.compact & all_collections end |
.seeds(env, options = {}) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/kaplan.rb', line 106 def seeds(env, ={}) if [:only] files = [:only]. map {|path| Dir["#{project_root}/#{path}"] }. flatten. select {|file| File.file?(file) && file =~ /\.(yml|yaml|rb|txt|csv)/ } if files.empty? raise "Error in Kaplan.seeds: Couldn't find the seed files you specified." end else files = [ "#{project_root}/db/seeds.rb", Dir["#{project_root}/seeds/*.{yml,yaml,rb,txt,csv}"], Dir["#{project_root}/seeds/#{env}/*.{yml,yaml,rb,txt,csv}"] ].flatten end files.sort.enum_for(:each_with_index).map do |filename, i| basename = ::File.basename(filename) # Ignore numbers at the beginning of the filename, as they are used for sorting basename =~ /^(?:\d+_)?(.+?)\.([^.]+)$/ extension = $2.downcase # collection_name and model only applies to files within seeds/ if i > 0 collection_name = $1 model = collection_name.classify.constantize end [filename, extension, collection_name, model] end end |