Giter Club home page Giter Club logo

hash_dot's Introduction

Build Status

HashDot

HashDot allows you to get and set your Ruby hash properties with a dot syntax.

  require 'hash_dot'

  user = {name: 'Anna', job: {title: 'Programmer'}}.to_dot

  user.job.title #=> 'Programmer'
  user.job.title = 'Senior Programmer'
  user.job.title #=> 'Senior Programmer'
  user.job.delete(:title)
  user.job.title #=> NoMethodError
  user.job.title = 'Engineer'
  user.job.title #=> 'Engineer'
  user.job.department = 'DevOps'
  user.job.department #=> 'DevOps'
  user.send('beverages=', {}.to_dot)
  user.send('beverages.coffee=', 'Short Black')
  user.send('beverages.coffee') #=> 'Short Black'
  user #=> {:name=>'Anna', :job=>{:title=>'Engineer', :department=>'DevOps'}, :beverages=> { :coffee => 'Short Black'} }

You can also allow dot syntax for all hashes via the class setting.

Hash.use_dot_syntax = true

{name: 'Pat'}.name #=> 'Pat'

By default HashDot raises a NoMethodError when accessing a non-existent key with the dot syntax. The value of Hash#default can be used instead, either globally or per-instance, via:

# globally
Hash.use_dot_syntax = true
Hash.hash_dot_use_default = true
{}.a #=> 'default'

# per instance
h = Hash.new('default').to_dot(use_default: true)
h.a #=> 'default'
{}.to_dot.a #=> NoMethodError

Installation

Add this line to your application's Gemfile:

gem 'hash_dot'

And then execute:

$ bundle

Or install it yourself as:

$ gem install hash_dot

Benchmarks

Benchmarks should be taken with a grain of salt, as always. For example, OpenStruct is much slower to initialize, but calls its methods faster once initialized. The OpenStruct solution also can't traverse more than a single dot without recursively instantiating all sub-hashes into OpenStructs.

require 'ostruct'
require 'benchmark'
require 'hash_dot'

user = { address: { category: { desc: 'Urban'}}}.to_dot

iterations = 50000

Benchmark.bm(8) do |bm|
  bm.report("Default Notation   :") {
    iterations.times do; user[:address][:category][:desc]; end
  }

  bm.report("Dot Notation       :") {
    iterations.times do; user.address.category.desc; end
  }

  bm.report("OpenStruct         :") {
    iterations.times do; OpenStruct.new(user); end
  }

  # Minus OpenStruct instantiation cost
  os_user = OpenStruct.new(user)
  bm.report("OpenStruct Single  :") {
    iterations.times do; os_user.address; end
  }

  bm.report("Dot Notation Single:") {
    iterations.times do; user.address; end
  }
end

# Benchmark Example
#                       user     system      total        real
Default Notation   :  0.010000   0.000000   0.010000 (  0.008807)
Dot Notation       :  0.190000   0.000000   0.190000 (  0.195819)
OpenStruct         :  0.400000   0.010000   0.410000 (  0.399542)
OpenStruct Single  :  0.010000   0.000000   0.010000 (  0.011259)
Dot Notation Single:  0.080000   0.000000   0.080000 (  0.082606)

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/adsteel/hash_dot. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

hash_dot's People

Contributors

adsteel avatar redterror avatar afeld avatar bosunolanrewaju avatar

Watchers

James Cloos avatar Muhammad Yana Mulyana 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.