Module: VersionedSeeds::ClassMethods
- Included in:
- VersionedSeeds
- Defined in:
- lib/versioned_seeds.rb
Overview
:nodoc:
Instance Method Summary collapse
-
#all(loaded = already_loaded) ⇒ Object
Loads all the seeding scripts that haven’t been loaded yet.
-
#all_seeds(loaded = already_loaded) ⇒ Object
Returns all the seeding scripts to load.
-
#already_loaded ⇒ Object
Returns the list of already loaded scripts.
- #configure {|_self| ... } ⇒ Object
-
#last_loaded(loaded = already_loaded) ⇒ Object
returns the last loaded seeding script.
- #list_path ⇒ Object
- #list_path=(path) ⇒ Object
-
#load(seeds, write = true) ⇒ Object
Loads a list of seeding scripts and updates the .versioned_seeds file.
-
#load_one(version) ⇒ Object
Loads seeding script with the given version.
-
#next(loaded = already_loaded) ⇒ Object
Loads the next seeding script.
-
#next_seed(loaded = already_loaded) ⇒ Object
Returns the next seeding script to load.
- #root_path ⇒ Object
- #root_path=(path) ⇒ Object
-
#seeds ⇒ Object
Returns the files located in the <root_path>/db/seeds folder sorted by version.
-
#write_loaded(loaded) ⇒ Object
Writes the versions of the loaded script to the .versionned_seeds file.
Instance Method Details
#all(loaded = already_loaded) ⇒ Object
Loads all the seeding scripts that haven’t been loaded yet
24 25 26 |
# File 'lib/versioned_seeds.rb', line 24 def all(loaded=already_loaded) load all_seeds(loaded) end |
#all_seeds(loaded = already_loaded) ⇒ Object
Returns all the seeding scripts to load
34 35 36 |
# File 'lib/versioned_seeds.rb', line 34 def all_seeds(loaded=already_loaded) seeds.delete_if { |seed| loaded.include?(seed.version) } end |
#already_loaded ⇒ Object
Returns the list of already loaded scripts
73 74 75 76 |
# File 'lib/versioned_seeds.rb', line 73 def already_loaded return [0] unless File.exists?(list_path) File.read(list_path).split(/\r?\n/) end |
#configure {|_self| ... } ⇒ Object
8 9 10 |
# File 'lib/versioned_seeds.rb', line 8 def configure yield self end |
#last_loaded(loaded = already_loaded) ⇒ Object
returns the last loaded seeding script
39 40 41 |
# File 'lib/versioned_seeds.rb', line 39 def last_loaded(loaded=already_loaded) loaded.last end |
#list_path ⇒ Object
92 93 94 |
# File 'lib/versioned_seeds.rb', line 92 def list_path @list_path ||= root_path + '.versioned_seeds' end |
#list_path=(path) ⇒ Object
96 97 98 |
# File 'lib/versioned_seeds.rb', line 96 def list_path=(path) @list_path = path end |
#load(seeds, write = true) ⇒ Object
Loads a list of seeding scripts and updates the .versioned_seeds file
44 45 46 47 48 49 50 |
# File 'lib/versioned_seeds.rb', line 44 def load(seeds, write=true) [*seeds].each do |seed| puts "Loading: #{File.basename seed.file}" require seed.file write_loaded seed if write end end |
#load_one(version) ⇒ Object
Loads seeding script with the given version
13 14 15 16 |
# File 'lib/versioned_seeds.rb', line 13 def load_one(version) seed = all_seeds([]).select { |seed| seed.version == version }.first load seed, false end |
#next(loaded = already_loaded) ⇒ Object
Loads the next seeding script
19 20 21 |
# File 'lib/versioned_seeds.rb', line 19 def next(loaded=already_loaded) load next_seed(loaded) end |
#next_seed(loaded = already_loaded) ⇒ Object
Returns the next seeding script to load
29 30 31 |
# File 'lib/versioned_seeds.rb', line 29 def next_seed(loaded=already_loaded) all_seeds(loaded).first end |
#root_path ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'lib/versioned_seeds.rb', line 78 def root_path @root_path ||= Rails.root if defined?(Rails) return @root_path if @root_path raise "VersionedSeeds.root_path is not set.\n" \ "VersionedSeeds.configure do |config|\n" \ " config.root_path = '/root/of/your/app/'\n" \ "end" end |
#root_path=(path) ⇒ Object
87 88 89 90 |
# File 'lib/versioned_seeds.rb', line 87 def root_path=(path) @root_path = path @root_path += '/' unless @root_path.nil? || @root_path.end_with?('/') end |
#seeds ⇒ Object
Returns the files located in the <root_path>/db/seeds folder sorted by version
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/versioned_seeds.rb', line 53 def seeds Dir[root_path + 'db/seeds/*.rb'].inject([]) { |list, file| filename = File.basename(file) version = filename[/^(\d+)_/, 1] next unless version list << OpenStruct.new(:file => file, :version => version) list }.sort { |seed1, seed2| seed1.version <=> seed2.version } end |
#write_loaded(loaded) ⇒ Object
Writes the versions of the loaded script to the .versionned_seeds file
66 67 68 69 70 |
# File 'lib/versioned_seeds.rb', line 66 def write_loaded(loaded) File.open(list_path, 'a') do |f| f.puts loaded.version end end |