dir_model 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 97702ad9ff5a3415d30ca57f0ddfb416ca123aa7
4
- data.tar.gz: 35f8b36a9e1a8c8567523188aa85fe452084be9b
3
+ metadata.gz: 4bd12bf7d01c2bad2a2523951ab4ab1c568e0e02
4
+ data.tar.gz: fc427c58ec981822cb6cbb2094133d565ca11d89
5
5
  SHA512:
6
- metadata.gz: 4c7f45eaf2273af554f5698443a6c4c32e3a29fa1c4a0815bce3a7bc2bb5728dea044fc7087f8a304111f79cccdbcdb35326e38cebc9bdd7d056c170b71f27cb
7
- data.tar.gz: 1da95475764bc987b22499a9236757480113f7f9eb2c96882b0a303d419c79b8864a93e69a96614c506bf0217c157bb298aa38a6af95bdd48c45a235f8af1b6c
6
+ metadata.gz: 16742b98db89c722a6781663280852b7c37a5fb957c9d8ce586b88b0ee910463919c6c487ce3fa32fd2a8913b248b3fb57487df2696352a6edc8781b840b2894
7
+ data.tar.gz: 4404f164ed161c2629abc6ebf18e51ca3790c02f85732a379161bb23b09ec0e7e211935a4a0364f6131a6d723f3282b3736a38e281600aab5cb977bb017c42cd
data/.travis.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.2.3
3
+ - 2.3.0
4
4
  before_install: gem install bundler -v 1.10.6
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### VERSION 0.6.0
2
+
3
+ * Fix relations
4
+
1
5
  ### VERSION 0.5.1
2
6
 
3
7
  * Add relation has_many to a dir_model
data/README.md CHANGED
@@ -60,13 +60,17 @@ A dir_model can have a relation like `has_one` basically is
60
60
  class ChildImportDirModel
61
61
  include DirModel::Model
62
62
  include DirModel::Import
63
- file :metadata, regex: -> { /Zones\/Sector_(?<sector_id>.*)\/Zone_(?<zone_id>.*)\.(?<extension>json)/i }
63
+ file :metadata, regex: ->(foreign_value) { "Zones\/(?<sector_name>#{foreign_value})\/Zone_(?<zone_id>.*)\.(?<extension>json)" }
64
64
  end
65
65
  ```
66
66
 
67
67
  ```ruby
68
68
  class ParentImportDirModel < BasicImportDirModel
69
- has_one :dependency, ChildImportDirModel
69
+ has_one :dependency, ChildImportDirModel, foreign_key: :sector_name
70
+
71
+ def sector_name
72
+ "sector_#{sector_id}"
73
+ end
70
74
  end
71
75
  ```
72
76
 
@@ -85,7 +89,7 @@ NOTE regex on child pass explicitly the value
85
89
  class ZoneDirModel
86
90
  include DirModel::Model
87
91
  include DirModel::Import
88
- file :image, regex: ->(foreign_value) { "Zones\/#{foreign_value}\/Zone_(?<zone_id>.*)\.(?<extension>png|jpg)" }
92
+ file :image, regex: ->(foreign_value) { "Zones\/(?<sector_name>#{foreign_value})\/Zone_(?<zone_id>.*)\.(?<extension>png|jpg)" }
89
93
  end
90
94
  ```
91
95
 
data/UPGRADE.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Upgrading
2
2
 
3
+ # Upgrading from 0.5.1 to 0.6.0
4
+
5
+ * relation need to have a foreign_key and dir_model related need to have a regex with the foreign_key in params
6
+
3
7
  # Upgrading from 0.4.0 to 0.5.0
4
8
 
5
9
  * Ensure you have only one definition of `file:` by dir_model
@@ -30,7 +30,7 @@ module DirModel
30
30
 
31
31
  path.read_path
32
32
  dir_model = new(path.current_path, index: path.index, context: context, previous: previous)
33
- find_relations(dir_model, path, context)
33
+ find_relations(dir_model, path, context) unless dir_model.skip?
34
34
 
35
35
  dir_model
36
36
  end
@@ -38,20 +38,18 @@ module DirModel
38
38
  private
39
39
 
40
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) }
41
+ if dir_model.has_relations?
42
+ if dir_model.has_one?
43
+ child = search(path, context) do |_path, _context|
44
+ dir_model.append_dir_model(_path.current_path, index: _path.index, context: _context)
45
+ end.first
46
+ find_relations(child, path, context) if child
47
+ end
48
+ if dir_model.has_many?
49
+ children = search(path, context) do |_path, _context|
50
+ dir_model.append_dir_models(_path.current_path, index: _path.index, context: _context)
54
51
  end
52
+ children.each { |_child| find_relations(_child, path, context) }
55
53
  end
56
54
  end
57
55
  end
@@ -21,9 +21,13 @@ module DirModel
21
21
  # @return [Model] return the child if it is valid, otherwise returns nil
22
22
  def append_dir_model(source_path, options={})
23
23
  relation_name = self.class.has_one_relationship.keys.first
24
- related_class = self.class.has_one_relationship.values.first
24
+ _options = self.class.has_one_relationship.values.first
25
+ related_class = _options[:dir_model_class]
26
+ foreign_key = _options[:foreign_key]
27
+ foreign_value = self.send(foreign_key)
25
28
 
26
- related_dir_model = related_class.new(source_path, options.reverse_merge(parent: self))
29
+ related_dir_model = related_class.new(source_path,
30
+ options.reverse_merge(parent: self, foreign_value: foreign_value))
27
31
 
28
32
  return unless related_dir_model
29
33
 
@@ -74,12 +78,12 @@ module DirModel
74
78
  #
75
79
  # @param [Symbol] relation_name the name of the relation
76
80
  # @param [DirModel::Import] dir_model_class class of the relation
77
- def has_one(relation_name, dir_model_class)
81
+ def has_one(relation_name, dir_model_class, options)
78
82
  raise "for now, DirModel's has_one may only be called once" if @_has_one_relationship.present?
79
83
 
80
84
  relation_name = relation_name.to_sym
81
85
 
82
- merge_has_one_relationship(relation_name => dir_model_class)
86
+ merge_has_one_relationship(relation_name => { dir_model_class: dir_model_class }.merge(options))
83
87
 
84
88
  define_method(:has_one?) do
85
89
  true
@@ -94,6 +98,10 @@ module DirModel
94
98
  end
95
99
  end
96
100
 
101
+ def has_one?
102
+ !!@_has_one_relationship
103
+ end
104
+
97
105
  # Defines a relationship between a dir model
98
106
  #
99
107
  # @param [Symbol] relation_name the name of the relation
@@ -118,6 +126,14 @@ module DirModel
118
126
  instance_variable_get(variable_name) || instance_variable_set(variable_name, [])
119
127
  end
120
128
  end
129
+
130
+ def has_many?
131
+ !!@_has_many_relationship
132
+ end
133
+
134
+ def has_relations?
135
+ has_one? || has_many?
136
+ end
121
137
  end
122
138
  end
123
139
  end
@@ -1,3 +1,3 @@
1
1
  module DirModel
2
- VERSION = '0.5.2'
2
+ VERSION = '0.6.0'
3
3
  end
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.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Chung
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-02-04 00:00:00.000000000 Z
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport