Coder Social home page Coder Social logo

exaspark / better_struct Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 22 KB

Use your data without pain :construction_worker:

Home Page: https://speakerdeck.com/exaspark/content-integration-at-bookmate

License: MIT License

Ruby 100.00%
openstruct maybe monad ruby

better_struct's Introduction

BetterStruct

Build Status

BetterStruct is a data structure which allows you to use your data without pain.

It behaves like an OpenStruct on steroids with monad.

hash = { "FooBar1" => { "FooBar2" => "Hello World!" } }

# Instead of this:
if hash["FooBar1"] && hash["FooBar1"]["FooBar2"] && hash["FooBar1"]["FooBar2"].respond_to?(:sub)
  hash["FooBar1"]["FooBar2"].sub("Hello ", "") # => "World!"
end

# Simply use:
BetterStruct.new(hash).foo_bar1.foo_bar2.sub("Hello ", "").value # => "World!"

Installation

Add this line to your application's Gemfile:

gem "better_struct"

And then execute:

$ bundle

Or install it yourself as:

$ gem install better_struct

Usage

Maybe monad

BetterStruct.new(nil) == BetterStruct.new(nil).this_method.does_not_exist # => true

Everything is wrapped

better_struct = BetterStruct.new("foobar")

better_struct[0..2] == BetterStruct.new("foo") # => true
better_struct = BetterStruct.new([1, 2, 3])

better_struct.all? { |i| i.is_a?(BetterStruct) } == BetterStruct.new(true) # => true

Like an OpenStruct on steroids

some_hash = { foo_bar1: { foo_bar2: "Hello World!" } }
better_struct = BetterStruct.new(some_hash)

better_struct.foo_bar1.foo_bar2 == BetterStruct.new("Hello World!") # => true

Leaving the monad

better_struct = BetterStruct.new("foobar")

better_struct.gsub("foo", "super-").value == "super-bar" # => true

Testing

$ ruby -Ilib:test test/better_struct_test.rb

Benchmarking

BetterStruct is even faster than an OpenStruct:

$ ruby scripts/benchmark.rb

Calculating -------------------------------------
          OpenStruct     7.334k i/100ms
        BetterStruct     7.856k i/100ms
-------------------------------------------------
          OpenStruct     75.971k (± 7.1%) i/s -    381.368k
        BetterStruct     84.520k (± 4.5%) i/s -    424.224k

Comparison:
        BetterStruct:    84519.8 i/s
          OpenStruct:    75971.1 i/s - 1.11x slower

TODO

  • Handle respond_to?.

Contributing

  1. Fork it ( https://github.com/exAspArk/better_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

better_struct's People

Contributors

exaspark avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.