Simple lensing for your data extraction needs
Add it to your gemfile:
gem 'sorta-lens', '~> 0.1.0'
or install globally for use in scripts or IRB
gem install sorta-lens
Simply instantiate a Lens and start applying it to your objects:
complex1 = { id: 451, .... status: :active }
complex2 = User.first # has property `id` and method `status`
lens = Sorta::Lens.on :id, :status
lens.(complex1) # => { id: 451, status: :active }
lens.(complex2) # => { id: 1, status: :online }
There is also a typed version:
complex1 = { id: 451, .... status: :active }
complex2 = { id: 9001, .... status: "online" }
lens = Sorta::Lens.typed.on id: Integer, status: String
lens.(complex1) # => TypedLens::TypeError (Unexpected type. Expected String got Symbol)
lens.(complex2) # => { id: 9001, status: "online" }