Class: Findable::Seed
- Inherits:
-
Struct
- Object
- Struct
- Findable::Seed
- Defined in:
- lib/findable/seed.rb
Instance Attribute Summary collapse
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#seed_files ⇒ Object
Returns the value of attribute seed_files.
Class Method Summary collapse
- .from_seed_dir(seed_dir, seed_file) ⇒ Object
- .model_name(seed_dir, seed_file) ⇒ Object
- .pathname(path) ⇒ Object
- .target_files(seed_dir: nil, seed_files: nil) ⇒ Object
- .without_ext(seed_file) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#model_name ⇒ Object
Returns the value of attribute model_name
15 16 17 |
# File 'lib/findable/seed.rb', line 15 def model_name @model_name end |
#seed_files ⇒ Object
Returns the value of attribute seed_files
15 16 17 |
# File 'lib/findable/seed.rb', line 15 def seed_files @seed_files end |
Class Method Details
.from_seed_dir(seed_dir, seed_file) ⇒ Object
42 43 44 |
# File 'lib/findable/seed.rb', line 42 def from_seed_dir(seed_dir, seed_file) pathname(seed_file).relative_path_from(seed_dir) end |
.model_name(seed_dir, seed_file) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/findable/seed.rb', line 34 def model_name(seed_dir, seed_file) if seed_dir != seed_file.dirname && seed_file.basename.to_s.match(/^data/) from_seed_dir(seed_dir, seed_file.dirname).to_s.classify else from_seed_dir(seed_dir, without_ext(seed_file)).to_s.classify end end |
.pathname(path) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/findable/seed.rb', line 50 def pathname(path) case path when Pathname then path when String then Pathname.new(path) else nil end end |
.target_files(seed_dir: nil, seed_files: nil) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/findable/seed.rb', line 17 def target_files(seed_dir: nil, seed_files: nil) target_dir = pathname(seed_dir || Findable.config.seed_dir) raise UnknownSeedDir.new(target_dir) unless target_dir.try(:exist?) seed_files = seed_files.map!(&:to_s) if seed_files _model_name = method(:model_name).to_proc.curry.call(target_dir) _selected = Proc.new do |seed| seed_files.present? ? seed.table_name.in?(seed_files) : true end Pathname.glob(target_dir.join("**", "*")) .select(&:file?) .group_by(&_model_name) .map {|name, files| new(files, name) } .select(&_selected) end |
.without_ext(seed_file) ⇒ Object
46 47 48 |
# File 'lib/findable/seed.rb', line 46 def without_ext(seed_file) pathname(seed_file).dirname.join(pathname(seed_file).basename(".*")) end |
Instance Method Details
#bootstrap! ⇒ Object
74 75 76 77 78 79 |
# File 'lib/findable/seed.rb', line 74 def bootstrap! model.query.lock do model.delete_all model.query.import load_files end end |
#load_files ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/findable/seed.rb', line 63 def load_files seed_files.sort_by(&:to_s).inject([]) do |data, file| data | case file.extname when ".yml" then load_yaml(file) when ".csv" then load_csv(file) else raise UnknownFormat end end end |
#model ⇒ Object
59 60 61 |
# File 'lib/findable/seed.rb', line 59 def model @_model_class ||= model_name.constantize end |