Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove nth occurrence of aggregated item from an aggregation. #46

Open
elrayle opened this issue Jul 13, 2015 · 5 comments
Open

Remove nth occurrence of aggregated item from an aggregation. #46

elrayle opened this issue Jul 13, 2015 · 5 comments

Comments

@elrayle
Copy link
Contributor

elrayle commented Jul 13, 2015

Items can repeat in aggregation. The syntax for the delete method needs to support removing the nth occurrence.

Example without repeat...

class DummyObject < ActiveFedora::Base
  aggregates :members, predicate: RDFVocabularies::PCDMTerms.hasMember, class_name: "ActiveFedora::Base"
end

do_parent = DummyObject.new
do_child1 = DummyObject.new
do_child2 = DummyObject.new
do_child3 = DummyObject.new

do_parent.members = [do_child1,do_child2,do_child3]   # => [do_child1,do_child2,do_child3]
do_parent.members.delete do_child1   # => do_child1

Example with repeat...

class DummyObject < ActiveFedora::Base
  aggregates :members, predicate: RDFVocabularies::PCDMTerms.hasMember, class_name: "ActiveFedora::Base"
end

do_parent = DummyObject.new
do_child1 = DummyObject.new
do_child2 = DummyObject.new
do_child3 = DummyObject.new

do_parent.members = [do_child1,do_child2,do_child1,do_child3]   # => [do_child1,do_child2,do_child1,do_child3]

# delete 2nd occurrence of do_child1
do_parent.members.delete( do_child1, 2 )   # => do_child1
do_parents.members          # => [do_child1,do_child2,do_child3]

See also...

@elrayle
Copy link
Contributor Author

elrayle commented Jul 13, 2015

If nth occurrence is specified and is positive, then count from the beginning of the list.
If nth occurrence is specified and is negative, then count from the end of the list.

@elrayle
Copy link
Contributor Author

elrayle commented Jul 13, 2015

See usage with filtered associations... #45

@jcoyne
Copy link
Contributor

jcoyne commented Jul 14, 2015

It would more closely match the ruby enumerable API, and be more generally useful if we supported delete_at(n) rather than delete( child, n )

@elrayle
Copy link
Contributor Author

elrayle commented Jul 21, 2015

I am fine with delete_at(n). What would you expect to happen if delete(child) is used and child is in the list multiple times.

  • delete all instances of child
  • delete first instance of child

Examples...

col1.members = [col1,obj1,obj2,col2,obj1,col1]
deleted_objs = col1.members.delete_at 3   # => [obj2]
col1.members                              # => [col1,obj1,col2,obj1,col1]

col1.members = [col1,obj1,obj2,col2,obj1,col1]
deleted_objs = col1.members.delete obj1   # => [obj1]
col1.members                              # => [col1,obj2,col2,obj1,col1]   OR   
                                          # => [col1,obj2,col2,col1]

col1.members = [col1,obj1,obj2,col2,obj1,col1]
col1.child_objects                              # => [obj1,obj2,obj1]
deleted_objs = col1.child_objects.delete_at 3   # => [obj1]
col1.child_objects                              # => [obj1,obj2]
col1.members                                    # => [col1,obj1,obj2,col2,col1]

col1.members = [col1,obj1,obj2,col2,obj1,col1]
col1.child_objects                              # => [obj1,obj2,obj1]
deleted_objs = col1.child_objects.delete obj1   # => [obj1]
col1.child_objects                              # => [obj2,obj1]   OR   [obj2]

@escowles
Copy link
Contributor

The standard ruby Array behavior is to delete all of them, and use delete_at if you want to delete just one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants