0.0
No commit activity in last 3 years
No release in over 3 years
A mighty struct which wraps objects in order to provide deep method access to its properties.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.3
>= 0
>= 0
< 4.0.0, >= 3.0.0
>= 0.8.0
 Project Readme

MightyStruct

Build Status Test Coverage Code Climate Dependency Status

MightyStruct is an object wrapper which gives deep method access to properties. It combines beneficial features from functionally related projects like OpenStruct and Hashie::Mash into an non-inversive, transparant decorator like object wrapper.

Key features

  • wraps any object that is an Enumerable (e.g Array or Hash)
  • creates method accessors for any object that additionally responds to :keys (e.g. Hash)
  • deep method access to object properties
  • property accessors are implemented via methods, not method_missing
    • as a result tab completion in pry works
  • dispite property accessors, the namespace of wrapped objects isn't touched
  • all method calls which don't hit a property accessor are dispatched to the wrapped object
    • results are again wrapped to instances of MightyStruct if possible
  • the wrapped object can be retrieved at any time using MightyStruct.to_object(obj)

Example

require "mighty_struct"

hash = {
  a: [
    { b: 1 },
    { b: 2 },
  ],
}

# create it from some hash or array
mighty_struct = MightyStruct.new(hash)

# access deeply nested properties 
mighty_struct.a[0].b # => 1

# call methods transparently on the wrapped objects
mighty_struct.a.last.b # => 2

# get back the original object ... look ma', it's still the same hash
MightyStruct.to_object(mighty_struct).eql?(hash) # => true

Or play with it on your own. It's just one command (line) away.

git clone https://github.com/msievers/mighty_struct.git && cd mighty_struct && bundle && bin/console

Another of this "method invocation hashes", really?!

Before I started coding this, I tried the following three alternatives

  • OpenStruct
  • recursive-open-struct
  • Hashie::Mash

But neither of them provided everything I wanted.

               | MightyStruct | OpenStruct | recursive-open-struct | Hashie::Mash

--- | :----------: | :--------: | :-------------------: | :----------: deep method access | ✔️ | ✖️ | (✔️) | ✔️ real method accessors | ✔️ | ✔️ | ✔️ | ✖️ works without object dupping | ✔️ | ✖️ | ✖️ | ✖️ transparent method dispatching | ✔️ | ✖️ | ✖️ | ✖️ original object retrieval | ✔️ | ✖️ | ✖️ | ✖️

Why are real methods as property accessors cool?

Method accessors for object properties can either be implemented via method_missing or by defining (singleton) methods. The benefit of real methods is, that if you are using a debugger (e.g. pry), you can use tab completion to discover methods defined on a object. This does not work for method_missing based accessors.

With real method accessors in place, playing with a mighty_struct within pry just feels like working within a shell.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release to create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

  1. Fork it ( https://github.com/msievers/mighty_struct/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request