README.md in dir_model-0.1.0 vs README.md in dir_model-0.2.0

- old
+ new

@@ -19,32 +19,54 @@ $ gem install dir_model ## Usage ### Export + ```ruby -class ImageDir < DirModel::Export - file :zone_image +class ImageDir + include DirModel - def zone_image_source - File.new("spec/fixtures/image.png") + file :image, path: -> { "#{dir}/#{sub_dir}" }, name: -> { "#{image_name}.png" } +end + +class ImageExportDir < ImageDir + include DirModel::Export + + def dir + 'Sectors' end - def zone_image_name - "testing.png" + + def sub_dir + source_model.sector_name end - def _generate - mk_chdir "level1" do - mk_chdir "level2" do - copy_file :zone_image - end - end + def image_name + source_model.zone_name end + + def image + source_model.zone + end end -image_dir = ImageDir.new -image_dir.path # => path representing the above: "#{image_dir.path}/level1/level2/testing.png" +fixture_models = [ + OpenStruct.new({ + id: 42, + sector_name: 'sector_name', + zone_name: 'zone_name', + zone: File.new('spec/fixtures/image.png'), + }) +] + +exporter = DirModel::Export::AggregateDir.new(ImageExportDir) + +exporter.generate do |dir| + models.each { |model| dir << model } +end + +exporter.dir_path # => path of generated dir .../Sectors ``` ## zip_dir Use [`zip_dir`](https://github.com/FinalCAD/zip_dir) to zip DirModel::Export instances: ```ruby @@ -53,58 +75,6 @@ zip_file = zipper.generate do |z| z.add_and_cleanup_dir __dir_model_export__ end ``` -**Ensure that `require zip_dir` occurs before `dir_model`** - -## Future -Define a schema for the directory. -```ruby -class ImageDir - include DirModel - - file :zone_image, path: "{{root}}/{{branch}}", name: "{{zone_name}}" -end -``` - -To export, define your export model like [`ActiveModel::Serializer`](https://github.com/rails-api/active_model_serializers) -and generate the directory requirements: - -```ruby -class ImageExportDir < ImageDir - include DirModel::Export - - # below are method overrides with default implementation - # - # overriding is recommended - # - def root; source_model.root end - def branch; source_model.branch end - def zone_name; source_model.zone_name end - - # define your image source - # - # override with something like: File.new("path/to/image.png") - # - def zone_image - source_model.zone_image - end -end -``` - -To import, define your import model, which works like [`ActiveRecord`](http://guides.rubyonrails.org/active_record_querying.html) -wrapping over a directory: - -```ruby -class ImageImportDir < ImageDir - include DirModel::Import - - # - # below are method overrides with default implementation - # - def root; "level1" end - def branch; "level2" end - def zone_name; "testing" end - def zone_image; File.new("....") end -end -``` \ No newline at end of file +**Ensure that `require zip_dir` occurs before `dir_model` (for now)**