dir_model 0.5.0 → 0.5.1

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: 3a463aecfe9ed4b92a734acdca662de128f7e342
4
- data.tar.gz: 512616906b07badd6ee5ebb1b4a3c797d6a8cade
3
+ metadata.gz: 31f2c4f7e3899b8393fcd1246acdaf6e34e57128
4
+ data.tar.gz: 11a8f61f3fa2cf01ccc673df484794caa293cfdc
5
5
  SHA512:
6
- metadata.gz: e46872a46024038ba6997fe2b4967f9e85f349f5f58ede009bc17eaf8ccc47d47c47243e714cd91a936525c3eea646d1afaf59f5987ee4007cf08408052b9cac
7
- data.tar.gz: 9fdf00b25d1318468b1907f4ad574b8e5a98e1f776fb7c3524ad29549bd17cb8f7ffcdbf097808d8be409e6d01810463b3c94e4e5bc3b66bb8b44be6d3e9be64
6
+ metadata.gz: 565270b028ebd60317079ab4ed93ab1111e91467ca832108216ff7920c9bad0048a6bf7d0cfe0faf96ad6ced0e8b1fdc22975436245b6a3730bf0bf147be6c8f
7
+ data.tar.gz: 9c64417df2fdfcc362f7089a5559e49af51a439485f777b733ae096304802e56845c2aa67c6ce41f41cc36559bd058d1814c98ec2ebfbb7d04388fd08f0fce25
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ### VERSION 0.5.1
2
+
3
+ * Add relation has_many to a dir_model
4
+
1
5
  ### VERSION 0.5.0
2
6
 
3
7
  * Add relation has_one to a dir_model
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
@@ -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
- @file_infos = {}
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
- if dir_model.has_relations?
33
- current_position = path.index
34
- path.rewind
35
- loop do # loop until find related file (has_one relation)
36
- path.read_path
37
- break if dir_model.append_dir_model(path.current_path, index: path.index, context: context)
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
- dir_model
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, :file_infos
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(self.class.options[:regex].call)
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
@@ -1,3 +1,3 @@
1
1
  module DirModel
2
- VERSION = '0.5.0'
2
+ VERSION = '0.5.1'
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.0
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-30 00:00:00.000000000 Z
11
+ date: 2015-12-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport