FinalCAD/dir_model

Future

Closed this issue · 0 comments

Future

Define a schema for the directory.

class ImageDir
  include DirModel

  file :zone_image, path: "{{root}}/{{branch}}", name: "{{zone_name}}" 
end

To export, define your export model like ActiveModel::Serializer
and generate the directory requirements:

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

dir = DirModel::Export::AggregateDir.new(ImageExportDir, context).generate do |dir|
   dir << source_model
end

#then you can:
zipper = ZipDir::Zipper.new
zip_file = zipper.generate(dir.path)

# and `dir_model` will be independent of  `zip_dir` (no need to check `require` order

To import, define your import model, which works like ActiveRecord
wrapping over a directory:

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

  presenter do
     # presenter magic
  end
end

DirModel::Import::AggregateDir.new(path, ImageImportDir, context).each do |dir_model|
   # dir_model.....
end