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

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

Overview

mod ClassMethods

Instance Method Summary collapse

Instance Method Details

#ancestor?(descendant) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#ancestorsObject

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

#attributes_set(hash) ⇒ Object

: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

#descendant?(ancestor) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#descendantsObject

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

#descendentsObject

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

#destroyObject

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
280
# File 'lib/dm-is-awesome_set.rb', line 265

def destroy
  return true if destroyed?
  catch :halt do
    sads = self_and_descendants
    # Trigger all the before :destroy methods
    sads.each { |sad| sad.before_destroy_hook }
    # 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| sad.after_destroy_hook }
  end
  destroyed?
end

#destroy!Object

Same as @destroy, but does not run the hooks



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

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

#full_setObject

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

#leavesObject

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

#levelObject



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

def level
  ancestors.length
end

#move(vector) ⇒ Object

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

#next_siblingObject

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

#previous_siblingObject

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

#rootObject

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

#rootsObject

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

#self_and_ancestorsObject

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

#self_and_descendantsObject



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

def self_and_descendants;  self_and_descendents; end

#self_and_descendentsObject

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

#self_and_siblingsObject

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

#self_or_ancestor?(descendant) ⇒ Boolean

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

#self_or_descendant?(ancestor) ⇒ Boolean

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

#siblingsObject

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