dir_model 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +35 -0
- data/lib/dir_model/import.rb +49 -13
- data/lib/dir_model/model/relations.rb +65 -8
- data/lib/dir_model/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31f2c4f7e3899b8393fcd1246acdaf6e34e57128
|
4
|
+
data.tar.gz: 11a8f61f3fa2cf01ccc673df484794caa293cfdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 565270b028ebd60317079ab4ed93ab1111e91467ca832108216ff7920c9bad0048a6bf7d0cfe0faf96ad6ced0e8b1fdc22975436245b6a3730bf0bf147be6c8f
|
7
|
+
data.tar.gz: 9c64417df2fdfcc362f7089a5559e49af51a439485f777b733ae096304802e56845c2aa67c6ce41f41cc36559bd058d1814c98ec2ebfbb7d04388fd08f0fce25
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -52,6 +52,8 @@ You can have access at the file through
|
|
52
52
|
|
53
53
|
#### Relation
|
54
54
|
|
55
|
+
### Has One
|
56
|
+
|
55
57
|
A dir_model can have a relation like `has_one` basically is
|
56
58
|
|
57
59
|
```ruby
|
@@ -73,6 +75,39 @@ parent_instance.dependency # => ChildImportDirModel
|
|
73
75
|
child.parent # => parent_instance
|
74
76
|
```
|
75
77
|
|
78
|
+
### Has Many
|
79
|
+
|
80
|
+
You can define an `has_many` relation with another `DirModel`, is this case syntax is a bit different you have to pass a `foreign_key`, basically the name of the method should return a value will be replace in the regex in children relationships, like examples below
|
81
|
+
|
82
|
+
NOTE regex on child pass explicitly the value
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
class ZoneDirModel
|
86
|
+
include DirModel::Model
|
87
|
+
include DirModel::Import
|
88
|
+
file :image, regex: ->(foreign_value) { "Zones\/#{foreign_value}\/Zone_(?<zone_id>.*)\.(?<extension>png|jpg)" }
|
89
|
+
end
|
90
|
+
```
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
class SectorDirModel
|
94
|
+
include DirModel::Model
|
95
|
+
file :image, regex: -> { /Sectors\/Sector_(?<sector_id>.*)\.(?<extension>png|jpg)/i }
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class SectorImportDirModel < SectorDirModel
|
101
|
+
include DirModel::Import
|
102
|
+
|
103
|
+
has_many :dependencies, ZoneDirModel, foreign_key: :sector_name
|
104
|
+
|
105
|
+
def sector_name
|
106
|
+
'sector_1'
|
107
|
+
end
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
76
111
|
### Export
|
77
112
|
|
78
113
|
```ruby
|
data/lib/dir_model/import.rb
CHANGED
@@ -2,14 +2,14 @@ module DirModel
|
|
2
2
|
module Import
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
attr_reader :context, :source_path, :index, :previous
|
5
|
+
attr_reader :context, :source_path, :index, :previous, :foreign_value
|
6
6
|
|
7
7
|
def initialize(path, options={})
|
8
8
|
super # set parent
|
9
9
|
@source_path, @context = path, OpenStruct.new(options[:context])
|
10
10
|
@index, @previous = options[:index], options[:previous].try(:dup)
|
11
11
|
@load_state = :ghost
|
12
|
-
@
|
12
|
+
@foreign_value = options[:foreign_value]
|
13
13
|
end
|
14
14
|
|
15
15
|
def skip?
|
@@ -26,26 +26,54 @@ module DirModel
|
|
26
26
|
|
27
27
|
module ClassMethods
|
28
28
|
def next(path, context={}, previous=nil)
|
29
|
+
return if path.end?
|
30
|
+
|
29
31
|
path.read_path
|
30
32
|
dir_model = new(path.current_path, index: path.index, context: context, previous: previous)
|
33
|
+
find_relations(dir_model, path, context)
|
34
|
+
|
35
|
+
dir_model
|
36
|
+
end
|
31
37
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
+
private
|
39
|
+
|
40
|
+
def find_relations(dir_model, path, context)
|
41
|
+
unless dir_model.skip?
|
42
|
+
if dir_model.has_relations?
|
43
|
+
if dir_model.has_one?
|
44
|
+
child = search(path, context) do |_path, _context|
|
45
|
+
dir_model.append_dir_model(_path.current_path, index: _path.index, context: _context)
|
46
|
+
end.first
|
47
|
+
find_relations(child, path, context) if child
|
48
|
+
end
|
49
|
+
if dir_model.has_many?
|
50
|
+
children = search(path, context) do |_path, _context|
|
51
|
+
dir_model.append_dir_models(_path.current_path, index: _path.index, context: _context)
|
52
|
+
end
|
53
|
+
children.each { |_child| find_relations(_child, path, context) }
|
54
|
+
end
|
38
55
|
end
|
39
|
-
path.set_position(current_position)
|
40
56
|
end
|
41
|
-
|
42
|
-
|
57
|
+
end
|
58
|
+
|
59
|
+
def search(path, context)
|
60
|
+
return unless block_given?
|
61
|
+
|
62
|
+
dir_models = []
|
63
|
+
current_position = path.index
|
64
|
+
path.rewind
|
65
|
+
while !path.end? do
|
66
|
+
path.read_path
|
67
|
+
dir_models << yield(path, context)
|
68
|
+
end
|
69
|
+
path.set_position(current_position)
|
70
|
+
dir_models.uniq.compact
|
43
71
|
end
|
44
72
|
end
|
45
73
|
|
46
74
|
private
|
47
75
|
|
48
|
-
attr_reader :load_state
|
76
|
+
attr_reader :load_state
|
49
77
|
|
50
78
|
def match?
|
51
79
|
return if load_state == :loaded
|
@@ -54,7 +82,15 @@ module DirModel
|
|
54
82
|
alias_method :load, :match?
|
55
83
|
|
56
84
|
def find_match
|
57
|
-
@_match = (source_path||'').match(
|
85
|
+
@_match = (source_path||'').match(get_regexp)
|
86
|
+
end
|
87
|
+
|
88
|
+
def get_regexp
|
89
|
+
if foreign_value
|
90
|
+
Regexp.new(self.class.options[:regex].call(foreign_value), Regexp::IGNORECASE)
|
91
|
+
else
|
92
|
+
self.class.options[:regex].call
|
93
|
+
end
|
58
94
|
end
|
59
95
|
end
|
60
96
|
end
|
@@ -4,15 +4,16 @@ module DirModel
|
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
included do
|
6
6
|
inherited_class_hash :has_one_relationship
|
7
|
+
inherited_class_hash :has_many_relationship
|
7
8
|
end
|
8
|
-
|
9
|
+
|
9
10
|
# @return [Boolean] returns true, if the instance is a child
|
10
11
|
def child?
|
11
12
|
!!parent
|
12
13
|
end
|
13
|
-
|
14
|
+
|
14
15
|
def has_relations?
|
15
|
-
has_one?
|
16
|
+
has_one? || has_many?
|
16
17
|
end
|
17
18
|
|
18
19
|
# Appends model to the parent and returns it
|
@@ -23,8 +24,13 @@ module DirModel
|
|
23
24
|
related_class = self.class.has_one_relationship.values.first
|
24
25
|
|
25
26
|
related_dir_model = related_class.new(source_path, options.reverse_merge(parent: self))
|
26
|
-
|
27
|
+
|
28
|
+
return unless related_dir_model
|
29
|
+
|
27
30
|
unless related_dir_model.skip?
|
31
|
+
if public_send(relation_name).present?
|
32
|
+
raise StandardError.new("There are more of one #{relation_name} => #{related_class} for #{self.class.name}")
|
33
|
+
end
|
28
34
|
public_send("#{relation_name}=", related_dir_model)
|
29
35
|
related_dir_model
|
30
36
|
else
|
@@ -32,10 +38,37 @@ module DirModel
|
|
32
38
|
end
|
33
39
|
end
|
34
40
|
|
41
|
+
# Appends modeld to the parent and returns it
|
42
|
+
#
|
43
|
+
# @return [Model] return the child if it is valid, otherwise returns nil
|
44
|
+
def append_dir_models(source_path, options={})
|
45
|
+
relation_name = self.class.has_many_relationship.keys.first
|
46
|
+
_options = self.class.has_many_relationship.values.first
|
47
|
+
related_class = _options[:dir_model_class]
|
48
|
+
foreign_key = _options[:foreign_key]
|
49
|
+
foreign_value = self.send(foreign_key)
|
50
|
+
|
51
|
+
related_dir_model = related_class.new(source_path,
|
52
|
+
options.reverse_merge(parent: self, foreign_value: foreign_value))
|
53
|
+
|
54
|
+
return unless related_dir_model
|
55
|
+
|
56
|
+
unless related_dir_model.skip?
|
57
|
+
public_send(relation_name) << related_dir_model
|
58
|
+
related_dir_model
|
59
|
+
else
|
60
|
+
nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
35
64
|
def has_one?
|
36
65
|
false
|
37
66
|
end
|
38
|
-
|
67
|
+
|
68
|
+
def has_many?
|
69
|
+
false
|
70
|
+
end
|
71
|
+
|
39
72
|
class_methods do
|
40
73
|
# Defines a relationship between a dir model
|
41
74
|
#
|
@@ -47,11 +80,11 @@ module DirModel
|
|
47
80
|
relation_name = relation_name.to_sym
|
48
81
|
|
49
82
|
merge_has_one_relationship(relation_name => dir_model_class)
|
50
|
-
|
83
|
+
|
51
84
|
define_method(:has_one?) do
|
52
85
|
true
|
53
86
|
end
|
54
|
-
|
87
|
+
|
55
88
|
define_method("#{relation_name}=") do |value|
|
56
89
|
instance_variable_set("@#{relation_name}", value)
|
57
90
|
end
|
@@ -60,8 +93,32 @@ module DirModel
|
|
60
93
|
instance_variable_get("@#{relation_name}")
|
61
94
|
end
|
62
95
|
end
|
63
|
-
end
|
64
96
|
|
97
|
+
# Defines a relationship between a dir model
|
98
|
+
#
|
99
|
+
# @param [Symbol] relation_name the name of the relation
|
100
|
+
# @param [DirModel::Import] dir_model_class class of the relation
|
101
|
+
# @param [Hash] basically for set :foreign_key
|
102
|
+
def has_many(relation_name, dir_model_class, options)
|
103
|
+
raise "for now, DirModel's has_many may only be called once" if @_has_many_relationship.present?
|
104
|
+
|
105
|
+
relation_name = relation_name.to_sym
|
106
|
+
|
107
|
+
merge_has_many_relationship(relation_name => { dir_model_class: dir_model_class }.merge(options))
|
108
|
+
|
109
|
+
define_method(:has_many?) do
|
110
|
+
true
|
111
|
+
end
|
112
|
+
|
113
|
+
define_method(relation_name) do
|
114
|
+
#
|
115
|
+
# equal to: @relation_name ||= []
|
116
|
+
#
|
117
|
+
variable_name = "@#{relation_name}"
|
118
|
+
instance_variable_get(variable_name) || instance_variable_set(variable_name, [])
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
65
122
|
end
|
66
123
|
end
|
67
124
|
end
|
data/lib/dir_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dir_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Chung
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-12-
|
11
|
+
date: 2015-12-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|