README.md in dir_model-0.4.0 vs README.md in dir_model-0.5.0
- old
+ new
@@ -21,11 +21,11 @@
## Usage
### Import
```ruby
-class ImageDir
+class BasicDirModel
include DirModel::Model
file :image, regex: -> { /Zones\/Sector_(?<sector_id>.*)\/Zone_(?<zone_id>.*)\.(?<extension>png|jpg)/i }
end
```
@@ -33,37 +33,52 @@
named matches are available under `matches[:sector_id]` or directly when you calling `sector_id`
An implementation possible of Import
```ruby
-class ImageImportDir < ImageDir
+class BasicImportDirModel < BasicDirModel
include DirModel::Import
- def assign!
- model.send(method, image)
- end
-
protected
def model
Project.find(context[:project_id]).sectors.find(sector_id).zones.find(zone_id)
end
-
- def method
- :blueprint
- end
end
```
You can have access at the file through
-`ImageImportDir.new(source_path, project_id: 42).image`
+`BasicImportDirModel.new(source_path, project_id: 42).image`
+#### Relation
+
+A dir_model can have a relation like `has_one` basically is
+
+```ruby
+class ChildImportDirModel
+ include DirModel::Model
+ include DirModel::Import
+ file :metadata, regex: -> { /Zones\/Sector_(?<sector_id>.*)\/Zone_(?<zone_id>.*)\.(?<extension>json)/i }
+end
+```
+
+```ruby
+class ParentImportDirModel < BasicImportDirModel
+ has_one :dependency, ChildImportDirModel
+end
+```
+
+```ruby
+parent_instance.dependency # => ChildImportDirModel
+child.parent # => parent_instance
+```
+
### Export
```ruby
-class ImageDir
+class BasicDirModel
include DirModel::Model
file :image, path: -> { "#{dir}/#{sub_dir}" }, name: -> { image_name }
end
```
@@ -71,11 +86,11 @@
`path` and `name` can take Proc or String if doesn't have any interpolation.
If you don't know the extension of your image it will be automatically discover, but this works only for image so if you send, for instance, a json file you have to explicitly provide extension on the `:name` options
```ruby
-class ImageExportDir < ImageDir
+class BasicExportDirModel < BasicDirModel
include DirModel::Export
def dir
'Sectors'
end
@@ -104,11 +119,11 @@
zone_name: 'zone_name',
zone: File.new('spec/fixtures/image.png'),
})
]
-exporter = DirModel::Export::AggregateDir.new(ImageExportDir)
+exporter = DirModel::Export::AggregateDir.new(BasicExportDirModel)
exporter.generate do |dir|
models.each { |model| dir << model }
end
@@ -117,20 +132,20 @@
an skip? method based on the name of file :image is create, this method is named `image_skip?`
default implementation
```
-def image_skip?
+def skip?
image.present?
end
```
NOTE Safe to override on your Exporter
In fact this is equivalent to
```
-def image_skip?
+def skip?
source_model.zone.present?
end
```
by default Exporter provide a link between `source_model` and your `file` like
@@ -153,17 +168,5 @@
def image_extension
source_model.image.file.extension
end
```
Otherwise return nil, safe to override on your Exporter
-
-## zip_dir
-Use [`zip_dir`](https://github.com/FinalCAD/zip_dir) to zip DirModel::Export instances:
-```ruby
-# Zip
-zipper = ZipDir::Zipper.new
-zip_file = zipper.generate do |z|
- z.add_and_cleanup_dir __dir_model_export__
-end
-```
-
-**Ensure that `require zip_dir` occurs before `dir_model` (for now)**