ActiveModel-shaped, framework-free
A FieldStruct object answers valid?, errors, attributes, assign_attributes and to_json. A Rails developer already knows that vocabulary, and none of it needs Rails.
class Hut < FieldStruct::Base required :name, :string optional :places, :integerend
hut = Hut.new(name: '', places: '41')hut.valid? # => falsehut.errors.class # => FieldStruct::Errorshut.errors.full_messages # => ["Name is required"]hut.attributes # => {name: "", places: 41}defined?(ActiveModel) # => nilMirror the shape, not the code
Section titled “Mirror the shape, not the code”We took the method names and the behaviour callers depend on, and left the rest. The persistence half (save, new_record?) isn’t there because there is nothing to persist, and no line of ActiveModel’s code was reused. The reasons are simple.
- Code that reads
hut.errors[:name]today keeps working. Form builders, serializers and your own helpers can’t tell the difference. - The gem runs in a script, in a worker, or inside one of your gems that has no Rails to load.
- Our behaviour is ours to fix. A message, a coercion rule or a default can change without waiting on someone else’s release.
The cost is that we maintain our own Errors, our own full_messages and our own humanizing, which does the bare minimum: underscores become spaces and the first letter goes up. There is no inflector and no i18n. A Rails app that wants translated messages has to bridge them itself for now. We would rather ship a small honest version than a big half-compatible one.
Same behaviour with and without ActiveSupport
Section titled “Same behaviour with and without ActiveSupport”Loading ActiveSupport doesn’t change what a struct does. A date string is never read through String#to_time, and a DateTime converts the same way either way. The test is easy to state: a result that depends on whether some library happens to be loaded is a bug, since code that works in your console would act differently in the worker.
Actionable follows the same rule
Section titled “Actionable follows the same rule”Actionable’s core is plain Ruby. Rails, RSpec, job queues, coverage and the RuboCop cop each sit behind their own require, and nothing loads by itself.
defined?(ActiveRecord) # => nilActionable::Action.respond_to?(:transactional) # => falseActionable::Action.respond_to?(:honor_retry_metadata) # => false
require 'actionable/job'Actionable::Action.respond_to?(:honor_retry_metadata) # => trueWe turned down the friendlier option of loading the Rails adapter whenever ActiveRecord is present. Explicit wins here. If transactional showed up because of a gem you never meant to depend on, a missing require in one file would hide in the one place you want it loud. Without the adapter you get a plain NoMethodError when the class is defined.
What this does not mean
Section titled “What this does not mean”The gems don’t avoid Rails users. The adapters exist for them, and Use ActiveRecord transactions is the code you would write anyway. The point is that Rails is something you plug in, and not the ground the gems stand on.
See also
Section titled “See also”FieldStruct, Actionable and their adapters are open source under the MIT license.