Module: DataMapper::Is::List::InstanceMethods

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#movedObject



330
331
332
# File 'lib/dm-is-list/is/list.rb', line 330

def moved
  @moved
end

Instance Method Details

#detach(scope = list_scope) ⇒ DataMapper::Collection

detaches a list item from the list, essentially setting the position as nil

Examples:

Usage

Parameters:

  • scope (Hash) (defaults to: list_scope)

    Optional (Default is #list_scope)

Returns:

  • (DataMapper::Collection)

    the list items within the given scope



426
427
428
429
# File 'lib/dm-is-list/is/list.rb', line 426

def detach(scope = list_scope)
  list(scope).all(:position.gt => position).adjust!({ :position => -1 },true)
  self.position = nil
end

#left_siblingModel Also known as: higher_item, previous_item

finds the previous higher item in the list (lower in number position)

Examples:

Usage

Todo.get(2).left_sibling  => Todo.get(1)
Todo.get(2).higher_item  => Todo.get(1)
Todo.get(2).previous_item  => Todo.get(1)

Returns:

  • (Model)

    the previous list item



463
464
465
# File 'lib/dm-is-list/is/list.rb', line 463

def left_sibling
  list.reverse.first(:position.lt => position)
end

#list(scope = list_query) ⇒ DataMapper::Collection

returns the list the current item belongs to

Examples:

Usage

Todo.get(2).list  => [ list of Todo items within the same scope as item]
Todo.get(2).list(:user_id => 2 )  => [ list of Todo items with user_id => 2]

Parameters:

  • scope (Hash) (defaults to: list_query)

    Optional (Default is #list_query)

Returns:

  • (DataMapper::Collection)

    the list items within the given scope



389
390
391
# File 'lib/dm-is-list/is/list.rb', line 389

def list(scope = list_query)
  model.all(scope)
end

#list_queryHash

returns the query conditions

Examples:

Usage

Todo.get(2).list_query => { :user_id => 1, :order => [:position] }

Returns:

  • (Hash)

    …?



373
374
375
# File 'lib/dm-is-list/is/list.rb', line 373

def list_query
  list_scope.merge(:order => [ :position ])
end

#list_scopeHash

returns the scope of the current list item

Examples:

Usage

Todo.get(2).list_scope => { :user_id => 1 }

Returns:

  • (Hash)

    …?



342
343
344
# File 'lib/dm-is-list/is/list.rb', line 342

def list_scope
  Hash[ model.list_options[:scope].map { |p| [ p, attribute_get(p) ] } ]
end

#move(vector) ⇒ TrueClass, FalseClass

move item to a position in the list. position should only be changed through this

Examples:

Usage

* node.move :higher                 # moves node higher unless it is at the top of list
* node.move :lower                  # moves node lower unless it is at the bottom of list
* node.move :below => other_node    # moves this node below the other resource in the list
* node.move :above => Node.get(2)   # moves this node above the other resource in the list
* node.move :to => 2                # moves this node to the position given in the list
* node.move(2)                      # moves this node to the position given in the list

Parameters:

  • vector (Symbol, Hash, Integer)

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

  • :higher<Symbol> (Hash)

    a customizable set of options

  • :lower<Symbol> (Hash)

    a customizable set of options

  • :up<Symbol> (Hash)

    a customizable set of options

  • :down<Symbol> (Hash)

    a customizable set of options

  • :highest<Symbol> (Hash)

    a customizable set of options

  • :lowest<Symbol> (Hash)

    a customizable set of options

  • :top<Symbol> (Hash)

    a customizable set of options

  • :bottom<Symbol> (Hash)

    a customizable set of options

  • :above<Resource> (Hash)

    a customizable set of options

  • :below<Resource> (Hash)

    a customizable set of options

  • :to<Hash{Symbol (Hash)

    a customizable set of options

  • <Integer> (Hash)

    a customizable set of options

Returns:

  • (TrueClass, FalseClass)

    returns false if it cannot move to the position, otherwise true

See Also:

  • #move_without_saving


517
518
519
# File 'lib/dm-is-list/is/list.rb', line 517

def move(vector)
  move_without_saving(vector) && save
end

#move_to_list(scope, pos = nil) ⇒ Boolean

moves an item from one list to another

Examples:

Usage

Todo.get(2).move_to_list(2)
Todo.get(2).move_to_list(2, 10)

Parameters:

  • scope (Integer)

    must be the id value of the scope

  • pos (Integer) (defaults to: nil)

    Optional sets the entry position for the item in the new list

Returns:

  • (Boolean)

    True/False based upon result



444
445
446
447
448
449
450
# File 'lib/dm-is-list/is/list.rb', line 444

def move_to_list(scope, pos = nil)
  detach   # remove from current list
  attribute_set(model.list_options[:scope][0], scope.to_i) # set new scope
  save     # save progress. Needed to get the positions correct.
  reload   # get a fresh new start
  move(pos) unless pos.nil?
end

#original_list_scopeHash

returns the original scope of the current list item

Examples:

Usage

item = Todo.get(2) # with user_id 1
item.user_id = 2
item.original_list_scope  => { :user_id => 1 }

Returns:

  • (Hash)

    …?



357
358
359
360
361
362
# File 'lib/dm-is-list/is/list.rb', line 357

def original_list_scope
  pairs = model.list_options[:scope].map do |p|
    [ p, (property = properties[p]) && original_attributes.key?(property) ? original_attributes[property] : attribute_get(p) ]
  end
  Hash[ pairs ]
end

#reorder_list(order) ⇒ Boolean

reorder the list this item belongs to

Examples:

Usage

Todo.get(2).reorder_list([:title.asc])

Parameters:

  • order (Array)

    …?

Returns:

  • (Boolean)

    True/False based upon result



412
413
414
# File 'lib/dm-is-list/is/list.rb', line 412

def reorder_list(order)
  model.repair_list(list_scope.merge(:order => order))
end

#repair_listObject

repair the list this item belongs to



397
398
399
# File 'lib/dm-is-list/is/list.rb', line 397

def repair_list
  model.repair_list(list_scope)
end

#right_siblingModel Also known as: lower_item, next_item

finds the next lower item in the list (higher in number position)

Examples:

Usage

Todo.get(2).right_sibling  => Todo.get(3)
Todo.get(2).lower_item  => Todo.get(3)
Todo.get(2).next_item  => Todo.get(3)

Returns:

  • (Model)

    the next list item



480
481
482
# File 'lib/dm-is-list/is/list.rb', line 480

def right_sibling
  list.first(:position.gt => position)
end