|
From: <lu...@us...> - 2003-12-20 06:38:38
|
Update of /cvsroot/neelix/neelix/model
In directory sc8-pr-cvs1:/tmp/cvs-serv27370/model
Modified Files:
model.rb
Log Message:
"Move Up" works! We may want to find some nicer way of doing the
promote/demote, but this was the only way I could get around the notify call
halfway through the swap (which is a Bad Thing [tm], since one of the
ingredients is just plain deleted if the notification happens too soon).
Index: model.rb
===================================================================
RCS file: /cvsroot/neelix/neelix/model/model.rb,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** model.rb 15 Dec 2003 20:11:54 -0000 1.4
--- model.rb 20 Dec 2003 06:38:35 -0000 1.5
***************
*** 41,44 ****
--- 41,65 ----
end
+ class ObservableArray
+ # should these instead be in a subclass of ObservableArray?
+ def promote(obj)
+ i = self.index(obj)
+ return if i.nil? or i == 0
+
+ tmp = self[i]
+ send("__old_[]=", i, self[i - 1]);
+ send("__old_[]=", i - 1, tmp);
+ end
+ def demote(obj)
+ i = self.index(obj)
+ return if i.nil? or i == self.length - 1
+
+ tmp = self[i]
+ send("__old_[]=", i, self[i + 1]);
+ send("__old_[]=", i + 1, tmp);
+ end
+ wrap_method :promote, :demote
+ end
+
class Food
include Observable
***************
*** 166,169 ****
--- 187,202 ----
end
+ def position
+ @dbh.select_one("select position from ingredient where ingredient_id=?",@id)['position']
+ end
+ def position=(p)
+ p2 = position
+ if p2 != p then
+ @dbh.do("update ingredient set position=? where ingredient_id=?", p, @id)
+ notify_observers
+ end
+ p
+ end
+
def to_s
s = "#{quantity} #{measure} #{food}"
***************
*** 186,190 ****
@id = recipe_id
@ingredients = ObservableArray.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=?", @id).each do |row|
@ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
--- 219,223 ----
@id = recipe_id
@ingredients = ObservableArray.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=? order by position", @id).each do |row|
@ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
***************
*** 192,196 ****
# which ingredients were previously in the DB?
old_ingredients = Array.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=?",@id).each do |row|
old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
--- 225,229 ----
# which ingredients were previously in the DB?
old_ingredients = Array.new
! @dbh.select_all("select ingredient_id from ingredient where recipe_id=? order by position",@id).each do |row|
old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
end
***************
*** 204,207 ****
--- 237,247 ----
(old_ingredients - @ingredients).each do |ingredient|
@dbh.do("delete from ingredient where ingredient_id=?",ingredient.id)
+ end
+
+ # reorder the rows in the database
+ position = 1
+ @ingredients.each do |ingredient|
+ ingredient.position = position
+ position += 1
end
}
|
|
From: Hans F. <ha...@fu...> - 2003-12-20 16:30:11
|
I have an idea for the ordering stuff. Position is stored in the
ingredient itself; why not make the ingredients array a set? Then the
array only ever changes when the set changes (addition or deletion) but
not when the ordering changes. To get the items in order, just do
recipe.ingredients.sort. (we need to def <=3D> for Ingredient, of course)
To change orders, just twiddle the position numbers - no array
notification since the array itself doesn't change, just the items.
Move the repositioning (promote/demote) into the presenter. When the
presenter has done its thing, it can call ingredientList.update.
/* Quoth lu...@us...
on Fri, 19 Dec 2003 at 22:38 -0800
in <E1A...@sc...> */
> Update of /cvsroot/neelix/neelix/model
> In directory sc8-pr-cvs1:/tmp/cvs-serv27370/model
>=20
> Modified Files:
> model.rb=20
> Log Message:
> "Move Up" works! We may want to find some nicer way of doing the
> promote/demote, but this was the only way I could get around the notify c=
all
> halfway through the swap (which is a Bad Thing [tm], since one of the
> ingredients is just plain deleted if the notification happens too soon).
>=20
>=20
> Index: model.rb
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> RCS file: /cvsroot/neelix/neelix/model/model.rb,v
> retrieving revision 1.4
> retrieving revision 1.5
> diff -C2 -d -r1.4 -r1.5
> *** model.rb 15 Dec 2003 20:11:54 -0000 1.4
> --- model.rb 20 Dec 2003 06:38:35 -0000 1.5
> ***************
> *** 41,44 ****
> --- 41,65 ----
> end
> =20
> + class ObservableArray
> + # should these instead be in a subclass of ObservableArray?
> + def promote(obj)
> + i =3D self.index(obj)
> + return if i.nil? or i =3D=3D 0
> +=20
> + tmp =3D self[i]
> + send("__old_[]=3D", i, self[i - 1]);
> + send("__old_[]=3D", i - 1, tmp);
> + end
> + def demote(obj)
> + i =3D self.index(obj)
> + return if i.nil? or i =3D=3D self.length - 1
> +=20
> + tmp =3D self[i]
> + send("__old_[]=3D", i, self[i + 1]);
> + send("__old_[]=3D", i + 1, tmp);
> + end
> + wrap_method :promote, :demote
> + end
> +=20
> class Food
> include Observable
> ***************
> *** 166,169 ****
> --- 187,202 ----
> end
> =20
> + def position
> + @dbh.select_one("select position from ingredient where ingredient_id=
=3D?",@id)['position']
> + end
> + def position=3D(p)
> + p2 =3D position
> + if p2 !=3D p then
> + @dbh.do("update ingredient set position=3D? where ingredient_id=3D=
?", p, @id)
> + notify_observers
> + end
> + p
> + end
> +=20
> def to_s
> s =3D "#{quantity} #{measure} #{food}"
> ***************
> *** 186,190 ****
> @id =3D recipe_id
> @ingredients =3D ObservableArray.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe_id=
=3D?", @id).each do |row|
> @ingredients << $replicator.get('ingredient', row['ingredient_id'])
> end
> --- 219,223 ----
> @id =3D recipe_id
> @ingredients =3D ObservableArray.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe_id=
=3D? order by position", @id).each do |row|
> @ingredients << $replicator.get('ingredient', row['ingredient_id'])
> end
> ***************
> *** 192,196 ****
> # which ingredients were previously in the DB?
> old_ingredients =3D Array.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe=
_id=3D?",@id).each do |row|
> old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
> end
> --- 225,229 ----
> # which ingredients were previously in the DB?
> old_ingredients =3D Array.new
> ! @dbh.select_all("select ingredient_id from ingredient where recipe=
_id=3D? order by position",@id).each do |row|
> old_ingredients << $replicator.get('ingredient', row['ingredient_id'])
> end
> ***************
> *** 204,207 ****
> --- 237,247 ----
> (old_ingredients - @ingredients).each do |ingredient|
> @dbh.do("delete from ingredient where ingredient_id=3D?",ingredient.i=
d)
> + end
> +=20
> + # reorder the rows in the database
> + position =3D 1
> + @ingredients.each do |ingredient|
> + ingredient.position =3D position
> + position +=3D 1
> end
> }
>=20
>=20
>=20
>=20
> -------------------------------------------------------
> This SF.net email is sponsored by: IBM Linux Tutorials.
> Become an expert in LINUX or just sharpen your skills. Sign up for IBM's
> Free Linux Tutorials. Learn everything from the bash shell to sys admin.
> Click now! http://ads.osdn.com/?ad_id=3D1278&alloc_id=3D3371&op=3Dclick
> _______________________________________________
> neelix-devel mailing list
> nee...@li...
> https://lists.sourceforge.net/lists/listinfo/neelix-devel
>=20
--=20
Hans Fugal | De gustibus non disputandum est.
http://hans.fugal.net/ | Debian, vim, mutt, ruby, text, gpg
http://gdmxml.fugal.net/ | WindowMaker, gaim, UTF-8, RISC, JS Bach
---------------------------------------------------------------------
GnuPG Fingerprint: 6940 87C5 6610 567F 1E95 CB5E FC98 E8CD E0AA D460
|
|
From: Hans F. <ha...@fu...> - 2003-12-22 23:33:21
|
/* Quoth Jacob Fugal <ja...@fu...> on Mon, 22 Dec 2003 at 15:46 -0700 in <3FE...@fu...> */ > Hans Fugal wrote: > | I have an idea for the ordering stuff. Position is stored in the > | ingredient itself; why not make the ingredients array a set? Then the > | array only ever changes when the set changes (addition or deletion) but > | not when the ordering changes. To get the items in order, just do > | recipe.ingredients.sort. (we need to def <=3D> for Ingredient, of cours= e) > | To change orders, just twiddle the position numbers - no array > | notification since the array itself doesn't change, just the items. > | Move the repositioning (promote/demote) into the presenter. When the > | presenter has done its thing, it can call ingredientList.update. >=20 > Ok, I added some separate presenter logic to take care of the > promotion/demotion/deletion. Pretty slick, a lot less confusing than > what I did at first. I did still run into one problem though: >=20 > Hans, why did you wrap the non-in-place array operations in > ObservableArray? Specifically, sort (not sort!, just sort). > > I added a sort to the ingredients display code block in view/fox.rb and > found myself in an infinite loop. sort notifies the ingredients > observers, of which the method calling sort is one. Taking :sort out of > the wrap_methods list fixed it. Is there a reason it needs to be in there? Hmm, I claim the fifth. I think I had a reason, but I think it was probably a poor reason, not well thought out. > Second, rather than having the presenter function call notify_observers > on the ingredients array|set, I just added an observer to the ingredient > itself that tell the ingredients set(s) it belongs to to > notfiy_observers. That way if the ingredient is somehow changed > somewhere else (think maybe concurrent editing), any changes are > instantly transmitted to all ingredient lists, not just the one that > requested the change. Capiche? Is there any reason not to do it this way? That is about what I had envisioned. I'm still not clear on how you're doing it but when I see the code I will be more so. > I'll commit the changes (the 'structure' in the presenter isn't very > refined, but it's rudimentary and it works -- we can refactor it as we > add more stuff) once you confirm this approach. Go ahead. --=20 Hans Fugal | De gustibus non disputandum est. http://hans.fugal.net/ | Debian, vim, mutt, ruby, text, gpg http://gdmxml.fugal.net/ | WindowMaker, gaim, UTF-8, RISC, JS Bach --------------------------------------------------------------------- GnuPG Fingerprint: 6940 87C5 6610 567F 1E95 CB5E FC98 E8CD E0AA D460 |