Class: Findable::Seed
- Inherits:
-
Object
- Object
- Findable::Seed
- Defined in:
- lib/findable/seed.rb
Instance Attribute Summary collapse
-
#ext ⇒ Object
readonly
Returns the value of attribute ext.
-
#full_path ⇒ Object
Returns the value of attribute full_path.
-
#namespaced ⇒ Object
readonly
Returns the value of attribute namespaced.
Class Method Summary collapse
Instance Method Summary collapse
- #base_name ⇒ Object
- #bootstrap! ⇒ Object
-
#initialize(path, seed_dir) ⇒ Seed
constructor
A new instance of Seed.
- #load_file ⇒ Object
- #model ⇒ Object
Constructor Details
#initialize(path, seed_dir) ⇒ Seed
Returns a new instance of Seed.
5 6 7 8 |
# File 'lib/findable/seed.rb', line 5 def initialize(path, seed_dir) @_seed_dir = seed_dir self.full_path = path end |
Instance Attribute Details
#ext ⇒ Object (readonly)
Returns the value of attribute ext.
3 4 5 |
# File 'lib/findable/seed.rb', line 3 def ext @ext end |
#full_path ⇒ Object
Returns the value of attribute full_path.
3 4 5 |
# File 'lib/findable/seed.rb', line 3 def full_path @full_path end |
#namespaced ⇒ Object (readonly)
Returns the value of attribute namespaced.
3 4 5 |
# File 'lib/findable/seed.rb', line 3 def namespaced @namespaced end |
Class Method Details
.patterns(seed_dir) ⇒ Object
53 54 55 56 57 |
# File 'lib/findable/seed.rb', line 53 def patterns(seed_dir) %w(yml).map {|format| Rails.root.join("#{seed_dir}/**/*.#{format}").to_s } end |
Instance Method Details
#base_name ⇒ Object
17 18 19 |
# File 'lib/findable/seed.rb', line 17 def base_name @_base ||= @namespaced.sub(@ext, "") end |
#bootstrap! ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/findable/seed.rb', line 36 def bootstrap! model.transaction do model.delete_all records = load_file.map {|data| model.new(data) } model.import records end end |
#load_file ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/findable/seed.rb', line 27 def load_file case @ext when ".yml" YAML.load_file(@full_path).values else raise UnexpectedFormat end end |
#model ⇒ Object
21 22 23 24 25 |
# File 'lib/findable/seed.rb', line 21 def model base_name.split("/").reverse.map.with_index {|n,i| i.zero? ? n.singularize : n }.reverse.map(&:camelize).join("::").constantize end |