Skip to content

Instantly share code, notes, and snippets.

@abevoelker
Created August 9, 2012 19:09
Show Gist options
  • Save abevoelker/3307213 to your computer and use it in GitHub Desktop.
Save abevoelker/3307213 to your computer and use it in GitHub Desktop.
Goal DSL for XFA manipulation
data = {:name => 'Bob', :gender => 'm', :relation => 'Uncle' }
pdfs = capture_pdfs do
pdf :basic_info do
text 'name', data[:name]
radio_group 'sex' do
fill 'male', :when => data[:gender] == 'm'
fill 'female', :when => data[:gender] == 'f'
end
checkbox_group 'relation' do
check 'parent', :when => ['Mom', 'Dad'].include?(data[:relation])
check 'sibling', :when => ['Brother', 'Sister'].include?(data[:relation])
check 'other', :unless => ['Brother', 'Sister', 'Mom', 'Dad'].include?(data[:relation])
# OR
case data[:relation]
when 'Mom', 'Dad'
check 'parent'
when 'Brother', 'Sister'
check 'sibling'
else
check 'other'
end
end
end
end
class PDF
def initialize(name=nil, opts={})
@name = name if name
@fields = []
end
def text(name, value)
@fields << {:name => name, :value => value, :type => :text}
end
def radio_group(name, &blk)
fields = []
# TODO: replace w/ singleton method?
PDF.instance_eval do
send(:define_method, :fill) do |value, opts={}|
return if opts.has_key?(:when) && !opts[:when]
return if opts.has_key?(:if) && !opts[:if]
return if opts.has_key?(:unless) && opts[:unless]
fields << {:name => name, :value => value, :type => :radio}
end
blk.call
send(:undef_method, :fill)
end
@fields += fields
end
def checkbox_group(name, &blk)
fields = []
# TODO: replace w/ singleton method?
PDF.instance_eval do
send(:define_method, :check) do |value, opts={}|
return if opts.has_key?(:when) && !opts[:when]
return if opts.has_key?(:if) && !opts[:if]
return if opts.has_key?(:unless) && opts[:unless]
fields << {:name => name, :value => value, :type => :checkbox}
end
blk.call
send(:undef_method, :check)
end
@fields += fields
end
end
module Kernel
def pdf(name=nil, opts={}, &blk)
r = PDF.new(name, opts)
r.instance_eval(&blk)
r
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment