Module: DataMapper::Is::AwesomeSet::InstanceMethods

Defined in:
lib/dm-is-awesome_set.rb

Overview

mod ClassMethods

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) ancestor?(descendant)

Returns:

  • (Boolean)


241
242
243
# File 'lib/dm-is-awesome_set.rb', line 241

def ancestor?(descendant)
  descendant.lft > lft && descendant.rgt < rgt
end

- (Object) ancestors

Gets all ancestors of this node



189
190
191
# File 'lib/dm-is-awesome_set.rb', line 189

def ancestors
  get_class.all(scope_hash.merge(:lft.lt => lft, :rgt.gt => rgt, :order => [:lft.asc]))
end

- (Object) attributes_set(hash)

:nodoc:



258
259
260
261
# File 'lib/dm-is-awesome_set.rb', line 258

def attributes_set(hash) #:nodoc:
  hash = hash || {}
  self.attributes = hash
end

- (Boolean) descendant?(ancestor)

Returns:

  • (Boolean)


233
234
235
# File 'lib/dm-is-awesome_set.rb', line 233

def descendant?(ancestor)
  ancestor.lft < lft && ancestor.rgt > rgt
end

- (Object) descendants

Fixed spelling for when English majors are peering over your shoulder



250
# File 'lib/dm-is-awesome_set.rb', line 250

def descendants; descendents; end

- (Object) descendents

Gets all descendents of this node



224
225
226
# File 'lib/dm-is-awesome_set.rb', line 224

def descendents
  get_class.all(scope_hash.merge(:rgt.lt => rgt, :lft.gt => lft, :order => [:lft.asc]))
end

- (Object) destroy

Destroys the current node and all children nodes, running their before and after hooks Returns the destroyed objects



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/dm-is-awesome_set.rb', line 265

def destroy
  sads = self_and_descendants
  hooks = get_class.const_get('INSTANCE_HOOKS')
  before_methods = hooks[:destroy][:before].map { |hash| hash[:name] }
  after_methods =  hooks[:destroy][:after].map  { |hash| hash[:name] }
  # Trigger all the before :destroy methods
  sads.each { |sad| before_methods.each { |bf| sad.send(bf) } }
  # dup is called here because destroy! likes to clear out the array, understandably.
  get_class.transaction do
    sads.dup.destroy!
    adjust_gap!(full_set, lft, -(rgt - lft + 1))
  end
  # Now go through after all the after :destroy methods.
  sads.each { |sad| after_methods.each { |bf| sad.send(bf) } }
end

- (Object) destroy!

Same as @destroy, but does not run the hooks



282
283
284
285
286
287
288
289
# File 'lib/dm-is-awesome_set.rb', line 282

def destroy!
  sad = self_and_descendants
  get_class.transaction do
    sad.dup.destroy!
    adjust_gap!(full_set, lft, -(rgt - lft + 1))
  end
  sad
end

- (Object) full_set

Returns the full set within this scope



219
220
221
# File 'lib/dm-is-awesome_set.rb', line 219

def full_set
  get_class.all(scope_hash)
end

- (Object) leaves

Retrieves the nodes without any children.



254
255
256
# File 'lib/dm-is-awesome_set.rb', line 254

def leaves
  get_class.leaves(self)
end

- (Object) level



174
175
176
# File 'lib/dm-is-awesome_set.rb', line 174

def level
  ancestors.length
end

- (Object) move(vector)

move self / node to a position in the set. position can only be changed through this

Examples:

Usage

* node.move :higher           # moves node higher unless it is at the top of parent
* node.move :lower            # moves node lower unless it is at the bottom of parent
* node.move :below => other   # moves this node below other resource in the set
* node.move :into => other    # same as setting a parent-relationship

Parameters:

  • vector (Symbol, Hash)

    A symbol, or a key-value pair that describes the requested movement

  • :higher<Symbol> (Hash)

    a customizable set of options

  • :highest<Symbol> (Hash)

    a customizable set of options

  • :lower<Symbol> (Hash)

    a customizable set of options

  • :lowest<Symbol> (Hash)

    a customizable set of options

  • :indent<Symbol> (Hash)

    a customizable set of options

  • :outdent<Symbol> (Hash)

    a customizable set of options

  • :root<Symbol|Hash|Resource> (Hash)

    a customizable set of options

  • :into<Resource> (Hash)

    a customizable set of options

  • :above<Resource> (Hash)

    a customizable set of options

  • :below<Resource> (Hash)

    a customizable set of options

  • :to<Integer> (Hash)

    a customizable set of options

See Also:

  • move_without_saving


166
167
168
169
170
171
172
# File 'lib/dm-is-awesome_set.rb', line 166

def move(vector)
  get_class.transaction do
    move_without_saving(vector)
    save!
  end
  reload
end

- (Object) next_sibling

Returns next node with same parent, or nil



209
210
211
# File 'lib/dm-is-awesome_set.rb', line 209

def next_sibling
  get_class.first(scope_and_parent_hash.merge(:lft.gt => rgt, :order => [:lft.asc]))
end

- (Object) previous_sibling

Returns previous node with same parent, or nil



214
215
216
# File 'lib/dm-is-awesome_set.rb', line 214

def previous_sibling
  get_class.first(scope_and_parent_hash.merge(:rgt.lt => lft, :order => [:rgt.desc]))
end

- (Object) root

Gets the root of this node



179
180
181
# File 'lib/dm-is-awesome_set.rb', line 179

def root
  get_class.first(root_hash.merge(:lft.lt => lft, :rgt.gt => rgt))
end

- (Object) roots

Gets all the roots of this node's tree



184
185
186
# File 'lib/dm-is-awesome_set.rb', line 184

def roots
  get_class.all(root_hash.merge(:order => [:lft.asc]))
end

- (Object) self_and_ancestors

Same as ancestors, but also including this node



194
195
196
# File 'lib/dm-is-awesome_set.rb', line 194

def self_and_ancestors
  get_class.all(scope_hash.merge(:lft.lte => lft, :rgt.gte => rgt, :order => [:lft.asc]))
end

- (Object) self_and_descendants



251
# File 'lib/dm-is-awesome_set.rb', line 251

def self_and_descendants;  self_and_descendents; end

- (Object) self_and_descendents

Same as descendents, but returns self as well



229
230
231
# File 'lib/dm-is-awesome_set.rb', line 229

def self_and_descendents
  get_class.all(scope_hash.merge(:rgt.lte => rgt, :lft.gte => lft, :order => [:lft.asc]))
end

- (Object) self_and_siblings

Same as siblings, but returns this node as well



204
205
206
# File 'lib/dm-is-awesome_set.rb', line 204

def self_and_siblings
  get_class.all(scope_and_parent_hash.merge(:order => [:lft.asc]))
end

- (Boolean) self_or_ancestor?(descendant)

Returns:

  • (Boolean)


245
246
247
# File 'lib/dm-is-awesome_set.rb', line 245

def self_or_ancestor?(descendant)
  descendant.lft >= lft && descendant.rgt <= rgt
end

- (Boolean) self_or_descendant?(ancestor)

Returns:

  • (Boolean)


237
238
239
# File 'lib/dm-is-awesome_set.rb', line 237

def self_or_descendant?(ancestor)
  ancestor.lft <= lft && ancestor.rgt >= rgt
end

- (Object) siblings

Gets all nodes that share the same parent node, except for this node



199
200
201
# File 'lib/dm-is-awesome_set.rb', line 199

def siblings
  get_class.all(scope_and_parent_hash.merge(:order => [:lft.asc], :lft.not => lft))
end