jrochkind/traject — pull request #17 by billdueber (2013-08-30 19:01:25+00:00)
Title: #to_field new syntax proposal

Currently, #to_field can take any of the following in addition to a field name

* a lambda with 2/3 arguments
* a block with 2/3 arguments
* both

I'm proposing adding a fourth option

* a block that take zero arguments and returns a lambda with 2/3 arguments

The advantage of this syntax is that it allows you to create a closure in what I think it a more syntactically satisfying way in your configuration. Compare:

```ruby
# Current style

```
oh35a_spec = Traject::MarcExtractor.new('035a')
srdmap = Traject::TranslationMap.new("lcc_top_level")
to_field 'sdrnum' do |record, acc|
oh35a_spec.extract(record).grep(/^sdr-?(.*)/).each do |sdr|
acc << sdrmap[sdr]
end
end

...to this:

```ruby
to_field 'sdrnum' do
oh35a_spec = Traject::MarcExtractor.new('035a')
srdmap = Traject::TranslationMap.new("lcc_top_level")
lambda do |record, acc|
oh35a_spec.extract(record).grep(/^sdr-?(.*)/).each do |sdr|
acc << sdrmap[sdr]
end
end
end

```

Obviously, it's a minor difference, but I really like having the relevant stuff all encapsulated under the #to_field call itself instead of hanging out above it somewhere.