From: Dreamcat Four Date: 2010-03-10T20:27:58+09:00 Subject: Re: Best / cleanest DSL for manipulating data files? So i've been looking at the current API interface to my classes. To be honest, Ive been playing with examples all morning, but cannot find a way to make them user friendly enough. Hopefully at least one person here would be kind enough to suggest to me a regular object to initializer eg `.new()` with an options hash? I wonder a lot how people have done this for other kinds of files. A while ago I write a gem for manipulating yaml files called `yamldoc` which tackled the problem of reading / writing yaml settings files. http://github.com/dreamcat4/yamldoc Yamldoc uses a very different approach. So im not saying that its necessarily applicable to this problem here. Just maybe another example to draw from. How best to solve a problem like this? Can any of you point to an examples from elsewhere in the Ruby world? These are my attempts so-far. Can't stress enough how im emphatically *not at all* happy with them yet. So don't waste your time replying to say how bad they are, or whats wrong with them. I already know :) But hopefully you can get the idea of what its trying to do. # 1. First attempt (but none of the options) launchd_plist filename do label "com.github.homebrew.myprogram" program_arguments ["/usr/bin/myprogram"] run_at_load true working_directory "/var/db/myprogram" standard_out_path "/var/log/myprogram.log" sockets do sock_service_name "netbios-ssn" end sockets do sock_service_name "netbios" bonjour ['smb'] end end plist.finalize # Module to avoid namespace conflicts include ::Plist4r # 2. This is not much different to 1 except the 'w' argument # it doesnt really seem to work for me p = plist4r("/Library/LaunchDaemons/org.cups.cupsd.plist",'w') p.keys do key1 "value1" key2 true end p.finalize # 3. If a plist is just a file then maybe ::File syntax? ::Plist4r.open(plist,'w') do key1 "value1" key2 true end ::Plist4r.open(plist,'r') do |hash| puts hash.inspect end # 4. Like 3 but we are writing a binary plist? ::Plist4r.open(plist,'w', :binary =>true) do key1 "value1" key2 true end # 5. Different again plist = ::Plist4r.open(plist,'w', :delayed_write => true) do key1 "value1" key2 true end plist.format = :binary # force to binary plist.finalize # actually reads and writes the plist -- Posted via http://www.ruby-forum.com/.