poly_belongs_to 0.1.6 → 0.1.7

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: 83e5505c4c5c26183c2dfa5f8d85fdae05961441
4
- data.tar.gz: e5e52a5f7f72f4c6b44ec06d499fd108e676faf3
3
+ metadata.gz: 6db0bb5452d0a17b0a99a45f875cd30532ade12a
4
+ data.tar.gz: 976a70a95737ad12583cde8401dc30411b85c1a8
5
5
  SHA512:
6
- metadata.gz: 54de1623a7a0534038290b76cb9a85731c62466870c3634dc29690c5c77bf6e87756cc406fc908b9e1ce0ad418f1ba14a203a5cbf233566f4939416d2dcda20c
7
- data.tar.gz: 36a62d5c63c4474f9840feb49b2c2ce743a8958b12a875a1b03df49018f97682acfb9d5a9c14ac03ba6c496d9a3e4587b933762d20881c3c4ee0b0d4490e89f1
6
+ metadata.gz: 6d49e5970ec7375eddfc93752569b611c11b741c1caf9181da5e58ac91dea9a702cf28c167d2b87be4347863636d565553983584add6bd9ac21e2f263f782040
7
+ data.tar.gz: cde4c040599f9cd89c41ed96141d1ed811a8420996cf01e812b31b4ad7ced1d886a9aa6e4867799278d514a85e90d66131b7ea6408a97c748cbb7659ccfab100
data/README.md CHANGED
@@ -119,6 +119,11 @@ PolyBelongsTo::Pbt::IsPlural[ obj, child ]
119
119
  # For has_one it's the object ref itself.
120
120
  PolyBelongsTo::Pbt::CollectionProxy[ obj, child ]
121
121
 
122
+ # Always returns a collection proxy; fakes a collection proxy for has_one.
123
+ PolyBelongsTo::Pbt::AsCollectionProxy[ obj, child ]
124
+
125
+ # Wrapper for has_one objects to be a collection proxy
126
+ PolyBelongsTo::FakedCollection.new(obj, child)
122
127
  ```
123
128
  ##Record Duplication
124
129
 
@@ -5,10 +5,8 @@ module PolyBelongsTo
5
5
  included do
6
6
  def self.pbt_dup_build(item_to_build_on, item_to_duplicate)
7
7
  if PolyBelongsTo::Pbt::IsReflected[item_to_build_on, item_to_duplicate]
8
- build_cmd = PolyBelongsTo::Pbt::BuildCmd[item_to_build_on, item_to_duplicate]
9
- dup_attrs = PolyBelongsTo::Pbt::AttrSanitizer[item_to_duplicate]
10
-
11
- build_cmd ? eval("item_to_build_on.#{build_cmd}(#{dup_attrs})") : nil
8
+ PolyBelongsTo::Pbt::AsCollectionProxy[item_to_build_on, item_to_duplicate].
9
+ build PolyBelongsTo::Pbt::AttrSanitizer[item_to_duplicate]
12
10
  end
13
11
  end
14
12
 
@@ -16,26 +14,12 @@ module PolyBelongsTo
16
14
  pbt_dup_build(item_to_build_on, item_to_duplicate)
17
15
  PolyBelongsTo::Pbt::Reflects[item_to_duplicate].each do |ref|
18
16
  child = eval("item_to_duplicate.#{ref}")
19
- core = eval("item_to_build_on.#{PolyBelongsTo::Pbt::CollectionProxy[item_to_build_on, item_to_duplicate]}")
20
- if child.respond_to?(:build)
21
- child.each do |spawn|
22
- if core.respond_to?(:build)
23
- core.each do |subscore|
24
- subscore.pbt_deep_dup_build(spawn)
25
- end
26
- else
27
- core.pbt_deep_dup_build(spawn)
28
- end
29
- end
30
- else
31
- if core.respond_to?(:build)
32
- core.each do |subcore|
33
- subcore.pbt_deep_dup_build(child)
34
- end
35
- else
36
- core.pbt_deep_dup_build(child)
17
+ PolyBelongsTo::Pbt::AsCollectionProxy[item_to_build_on, item_to_duplicate].
18
+ each do |builder|
19
+ child.respond_to?(:build) ? child.each {|spawn|
20
+ builder.pbt_deep_dup_build(spawn)
21
+ } : builder.pbt_deep_dup_build(child)
37
22
  end
38
- end
39
23
  end
40
24
  item_to_build_on
41
25
  end
@@ -0,0 +1,64 @@
1
+ module PolyBelongsTo
2
+ class FakedCollection
3
+ def initialize(obj, child)
4
+ raise "Not a has_one rleationship for FakedCollection" unless PolyBelongsTo::Pbt::IsSingular[obj,child]
5
+ @obj = obj
6
+ @child = child
7
+ @instance = eval("@obj.#{PolyBelongsTo::Pbt::CollectionProxy[@obj,@child]}")
8
+ self
9
+ end
10
+
11
+ def all
12
+ Array[@instance].compact
13
+ end
14
+
15
+ def first
16
+ @instance
17
+ end
18
+
19
+ def last
20
+ @instance
21
+ end
22
+
23
+ def size
24
+ all.size
25
+ end
26
+
27
+ def ancestors
28
+ klass.ancestors.unshift(PolyBelongsTo::FakedCollection)
29
+ end
30
+
31
+ def kind_of?(thing)
32
+ ancestors.include? thing
33
+ end
34
+
35
+ def count
36
+ size
37
+ end
38
+
39
+ def klass
40
+ @instance.class
41
+ end
42
+
43
+ def build(*args)
44
+ @instance = eval("@obj.#{PolyBelongsTo::Pbt::BuildCmd[@obj, @child]}(#{args.join(',')})")
45
+ self
46
+ end
47
+
48
+ def each(*args, &block)
49
+ all.each(*args, &block)
50
+ end
51
+
52
+ def respond_to?(method_name)
53
+ @instance.respond_to?(method_name) || super
54
+ end
55
+
56
+ def method_missing(method_name, *args, &block)
57
+ if @instance.respond_to?(method_name)
58
+ @instance.send method_name, *args, &block
59
+ else
60
+ super
61
+ end
62
+ end
63
+ end
64
+ end
@@ -58,5 +58,15 @@ module PolyBelongsTo
58
58
  proxy = ActiveModel::Naming.plural(child).to_sym
59
59
  reflects.include?(proxy) ? proxy : nil
60
60
  }
61
+
62
+ AsCollectionProxy = lambda {|obj, child|
63
+ return [] unless obj && child
64
+ reflects = Reflects[obj]
65
+ proxy = ActiveModel::Naming.singular(child).to_sym
66
+ return PolyBelongsTo::FakedCollection.new(obj, child) if reflects.include? proxy
67
+ proxy = ActiveModel::Naming.plural(child).to_sym
68
+ reflects.include?(proxy) ? eval("obj.#{PolyBelongsTo::Pbt::CollectionProxy[obj, child]}") : []
69
+ }
61
70
  end
71
+
62
72
  end
@@ -1,3 +1,3 @@
1
1
  module PolyBelongsTo
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -5,6 +5,7 @@ $: << File.join(File.dirname(__FILE__), "/poly_belongs_to")
5
5
  require 'poly_belongs_to/version'
6
6
  require 'poly_belongs_to/dup'
7
7
  require 'poly_belongs_to/poly_belongs_to'
8
+ require 'poly_belongs_to/faked_collection'
8
9
  require 'active_support/concern'
9
10
 
10
11
  module PolyBelongsTo
Binary file