selfish_associations 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bd7fff1c41265161aea2372a9fb041ce4ff77e0
4
- data.tar.gz: 7af54ffb489231247f270348800a6e144418d847
3
+ metadata.gz: 37dbe42af345cf9c60fff0b7dde1d3c2fee952a0
4
+ data.tar.gz: 6a31f1c1d716d52783ff858e395a944a628b2b00
5
5
  SHA512:
6
- metadata.gz: 93cdb13cfba94868478e470979fb9cbc4696771ae45344ead80bb9cb3f578039d61622dc833e96914fa86b5869bc2a0a3dfa8a79f012fca3623d8a8b6fcc8e35
7
- data.tar.gz: 3a50a8ef46c954d305ddfce23a041b53b059b33103e7fc88f53c9fa80db0b0a24ca9f3633a9fe953c965678f3e1662f2f614211b0c5a0abf2b8963a7daab39aa
6
+ metadata.gz: cc986cfd6243a3f802aee3c7f1d3cd46bb8516ddebf48e8d0145e3ebd7d416ef4f08581b0124ad3e4cbaeadc58d3e7b2dede36c807d7a7a03c4307b9d7ac5a5f
7
+ data.tar.gz: 9406b9a11d824ecfbc2db5292bfe03e63d0736da2a9f7607df365600fe24f835575ad3489fc5f86115f41e5ca5a355f0fa358c582788e96cfd6e4ba45be6a7c4
@@ -13,10 +13,12 @@ module SelfishAssociations
13
13
  options = options.symbolize_keys
14
14
  @name = name.to_s
15
15
  @model = model
16
- @scope = scope
17
16
  @foreign_class_name = (options[:class_name] || name.to_s.classify).to_s
18
17
  @foreign_key = options[:foreign_key] == false ? false : (options[:foreign_key] || @model.name.foreign_key).to_sym
19
- @foreign_key_scope = @foreign_key.present? ? foreign_key_scope : nil
18
+ @scopes = []
19
+
20
+ add_scope(scope) if scope.present?
21
+ add_scope(foreign_key_scope) if @foreign_key.present?
20
22
  validate
21
23
  end
22
24
 
@@ -38,15 +40,22 @@ module SelfishAssociations
38
40
  foreign_class.where(instance_find_conditions(instance))
39
41
  end
40
42
 
43
+ def initialize_for(instance)
44
+ foreign_class.new(instance_create_attributes(instance))
45
+ end
46
+
41
47
  def create_for(instance)
42
48
  foreign_class.create(instance_create_attributes(instance))
43
49
  end
44
50
 
45
51
  private
46
52
 
53
+ def add_scope(scope)
54
+ @scopes << scope
55
+ end
56
+
47
57
  def apply_scopes(reader)
48
- reader.read(@scope) if @scope.present?
49
- reader.read(@foreign_key_scope) if @foreign_key.present?
58
+ @scopes.each{|scope| reader.read(scope) }
50
59
  return reader
51
60
  end
52
61
 
@@ -111,9 +120,9 @@ module SelfishAssociations
111
120
  end
112
121
 
113
122
  def validate
114
- if @scope.present?
115
- @scope.is_a?(Proc) or raise SelfishAssociations::SelfishException, "Scope must be a Proc"
116
- @scope.arity == 0 || @scope.arity == 1 or raise SelfishAssociations::SelfishException, "Scope must have arity of 0 or 1"
123
+ @scopes.each do |scope|
124
+ scope.is_a?(Proc) or raise SelfishAssociations::SelfishException, "Scope must be a Proc"
125
+ scope.arity == 0 || scope.arity == 1 or raise SelfishAssociations::SelfishException, "Scope must have arity of 0 or 1"
117
126
  end
118
127
  @model.is_a?(Class) && @model < ActiveRecord::Base or raise SelfishAssociations::SelfishException, "Tried to define a SelfishAssociation for an invalid object (#{@model})"
119
128
  end
@@ -54,14 +54,11 @@ module SelfishAssociations
54
54
  merge ? PathMerger.new(@associations).merge : @associations
55
55
  end
56
56
 
57
- # Method Missing pattern (reluctantly).
58
- # Really we could initialize anew at each node and pre-define all methods
59
- # But this actually seems more lightweight.
60
- # Intercept any method to check if it is an association or a column
57
+ # Check if method is an association or a column
61
58
  # If Association, store current node and iterate to the association.
62
59
  # If it is a column, return an Arel::Node representing that value
63
60
  # Else, raise NoMethodError
64
- def method_missing(method, *args)
61
+ def send(method, *args)
65
62
  if @klass.column_names.include?(method.to_s)
66
63
  @associations << @path if @path.present?
67
64
  node = @klass.arel_table[method]
@@ -81,5 +78,12 @@ module SelfishAssociations
81
78
  ::Kernel.raise ::NoMethodError, message
82
79
  end
83
80
  end
81
+
82
+ # Method Missing pattern (reluctantly).
83
+ # Really we could initialize a new at each node and pre-define all methods
84
+ # But this actually seems more lightweight.
85
+ def method_missing(method, *args)
86
+ send(method, *args)
87
+ end
84
88
  end
85
- end
89
+ end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selfish_associations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Schwartz
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-04 00:00:00.000000000 Z
11
+ date: 2016-09-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Create joinable associations with instance/record-level constraints
13
+ description: Use instance-level where conditions with joinable associations
14
14
  email: ozydingo@gmail.com
15
15
  executables: []
16
16
  extensions: []