I spent some time today hacking out an experimental implementation of native Ruby array data binding. It works by using a CLR helper class called DataBinder that implements IList, and calls back to Ruby using delegates to retrieve the data. This gives the following usage model:
names = ['John', 'Paul', 'George', Ringo']
list_box.data_source = names.make_bindable
To make this work, I inject a make_bindable method into the Array class:
def make_bindable
binder = DataBinder.new
binder.get_data do |sender, args|
args.current_object = self[args.index]
end
binder.get_count do |sender, args|
args.count = self.length
end
binder
end
Thoughts? Does this feel good?
4 archived reader comments
Justin (2006-02-24)
Ideally, you’d want to just assign the array directly to the data source:
listbox.datasource = names
That’s how it is in .NET and it also feels more natural in Ruby. Of course, technically that could be a challenge.
Mark (2006-02-24)
Great progress. I’m curious why you didn’t just handle this in the marshalling of the array into the CLR-world. (always wrap them with the IList helper) That seems far more trivial than, say..supporting nested types (bravo on that).
If you’re assigning to a common interface, or passing as an argument (IEnumerable, ICollection, IList, etc.), create an appropriate wrapper (either statically or dynamically for real fun) and assign it instead.
I haven’t gotten a chance to look at your latest drop (stupid day job), but I’d love to help you figure out how to implement this approach. I know you’re driving to support certain scenarios, but seems like a broader approach would solve the subset of issues for databinding and open up opportunities for more scenarios and a more seamless experience across the boundary.
+1 for RubyForge project.
John Lam (2006-02-25)
Mark - I agree with your suggestion. I just have to think a bit more about how to make it happen.
Mail me later on this weekend if you have any spare cycles, and I can run some ideas by you then.
BTW, if you look at the source code for nested types, you’ll see the grotesque hack that’s in there right now as a placeholder :)
Thanks!
Justin (2006-02-24)
Ideally, you’d want to just assign the array directly to the data source:
list_box.data_source= namesThat’s how it is in .NET and it also feels more natural in Ruby. Of course, technically that could be a challenge.