active_node 0.0.6 → 0.0.7

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.
@@ -31,6 +31,10 @@ module ActiveNode
31
31
  klass && klass < ActiveNode::Base && klass || default_klass
32
32
  end
33
33
 
34
+ def filterClass(nodes, klass)
35
+ wrap(nodes.select{|node| klass.nil? || node.type == klass.name.underscore}, klass)
36
+ end
37
+
34
38
  private
35
39
  def new_instance node
36
40
  node && new(node)
@@ -76,14 +80,18 @@ module ActiveNode
76
80
  end
77
81
 
78
82
  def incoming(types=nil, klass=nil)
79
- node && self.class.wrap(node.incoming(types), klass)
83
+ related(:incoming, types, klass)
80
84
  end
81
85
 
82
86
  def outgoing(types=nil, klass=nil)
83
- node && self.class.wrap(node.outgoing(types), klass)
87
+ related(:outgoing, types, klass)
84
88
  end
85
89
 
86
90
  private
91
+ def related(direction, types, klass)
92
+ node && self.class.filterClass(node.send(direction, types), klass)
93
+ end
94
+
87
95
  def destroy_associations include_associations
88
96
  rels = node.rels
89
97
  return false unless rels.empty? || include_associations
@@ -19,9 +19,9 @@ module ActiveNode
19
19
  module ClassMethods
20
20
  def create_reflection(macro, name, options, model)
21
21
  case macro
22
- when :has_many, :has_one
23
- klass = options[:through] ? ThroughReflection : AssociationReflection
24
- reflection = klass.new(macro, name, options, model)
22
+ when :has_many, :has_one
23
+ klass = options[:through] ? ThroughReflection : AssociationReflection
24
+ reflection = klass.new(macro, name, options, model)
25
25
  end
26
26
 
27
27
  self.reflections = self.reflections.merge(name => reflection)
@@ -91,9 +91,9 @@ module ActiveNode
91
91
 
92
92
 
93
93
  def initialize(macro, name, options, model)
94
- @macro = macro
95
- @name = name
96
- @options = options
94
+ @macro = macro
95
+ @name = name
96
+ @options = options
97
97
  @model = model
98
98
  end
99
99
 
@@ -118,44 +118,48 @@ module ActiveNode
118
118
  end
119
119
 
120
120
  def type
121
- @type ||= (options[:type] || name.to_s.singularize)
121
+ @type ||= (options[:type] || derive_type)
122
122
  end
123
123
 
124
124
  # Returns +true+ if +self+ and +other_aggregation+ have the same +name+ attribute, +model+ attribute,
125
125
  # and +other_aggregation+ has an options hash assigned to it.
126
126
  def ==(other_aggregation)
127
127
  super ||
128
- other_aggregation.kind_of?(self.class) &&
129
- name == other_aggregation.name &&
130
- other_aggregation.options &&
131
- model == other_aggregation.model
128
+ other_aggregation.kind_of?(self.class) &&
129
+ name == other_aggregation.name &&
130
+ other_aggregation.options &&
131
+ model == other_aggregation.model
132
132
  end
133
133
 
134
134
  private
135
- def derive_class_name
136
- name.to_s.camelize
137
- end
135
+ def derive_class_name
136
+ name.to_s.camelize
137
+ end
138
+
139
+ def derive_type
140
+ direction == :outgoing ? name.to_s.singularize : @model.name.underscore
141
+ end
138
142
  end
139
143
 
140
144
 
141
145
  # Holds all the meta-data about an association as it was specified in the
142
146
  # Active Record class.
143
147
  class AssociationReflection < MacroReflection #:nodoc:
144
- # Returns the target association's class.
145
- #
146
- # class Author < ActiveRecord::Base
147
- # has_many :books
148
- # end
149
- #
150
- # Author.reflect_on_association(:books).klass
151
- # # => Book
152
- #
153
- # <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
154
- # a new association object. Use +build_association+ or +create_association+
155
- # instead. This allows plugins to hook into association object creation.
156
- #def klass
157
- # @klass ||= model.send(:compute_type, class_name)
158
- #end
148
+ # Returns the target association's class.
149
+ #
150
+ # class Author < ActiveRecord::Base
151
+ # has_many :books
152
+ # end
153
+ #
154
+ # Author.reflect_on_association(:books).klass
155
+ # # => Book
156
+ #
157
+ # <b>Note:</b> Do not call +klass.new+ or +klass.create+ to instantiate
158
+ # a new association object. Use +build_association+ or +create_association+
159
+ # instead. This allows plugins to hook into association object creation.
160
+ #def klass
161
+ # @klass ||= model.send(:compute_type, class_name)
162
+ #end
159
163
 
160
164
  def initialize(*args)
161
165
  super
@@ -204,27 +208,27 @@ module ActiveNode
204
208
 
205
209
  def association_class
206
210
  case macro
207
- when :has_many
208
- if options[:through]
209
- Associations::HasManyThroughAssociation
210
- else
211
- Associations::HasManyAssociation
212
- end
213
- when :has_one
214
- if options[:through]
215
- Associations::HasOneThroughAssociation
216
- else
217
- Associations::HasOneAssociation
218
- end
211
+ when :has_many
212
+ if options[:through]
213
+ Associations::HasManyThroughAssociation
214
+ else
215
+ Associations::HasManyAssociation
216
+ end
217
+ when :has_one
218
+ if options[:through]
219
+ Associations::HasOneThroughAssociation
220
+ else
221
+ Associations::HasOneAssociation
222
+ end
219
223
  end
220
224
  end
221
225
 
222
226
  private
223
- def derive_class_name
224
- class_name = name.to_s.camelize
225
- class_name = class_name.singularize if collection?
226
- class_name
227
- end
227
+ def derive_class_name
228
+ class_name = name.to_s.camelize
229
+ class_name = class_name.singularize if collection?
230
+ class_name
231
+ end
228
232
  end
229
233
 
230
234
  # Holds all the meta-data about a :through association as it was specified
@@ -1,3 +1,3 @@
1
1
  module ActiveNode
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  class Client < ActiveNode::Base
2
2
  attribute :name, type: String
3
3
 
4
- has_many :users, type: :client, direction: :incoming, class_name: 'NeoUser'
4
+ has_many :users, direction: :incoming, class_name: 'NeoUser'
5
5
 
6
6
  validates :name, presence: true
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_node
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: