Giter VIP home page Giter VIP logo

hash_dot's People

Contributors

adsteel avatar afeld avatar bosunolanrewaju avatar duck-rh avatar redterror avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

hash_dot's Issues

Conflicts with Arel/ActiveRecord

I don't know how to best describe this, or even how to go about properly debugging. Just pulled my hair out for hours then by happen stance disabled this gem and my issues were resolved. I've done multiple tests to verify that indeed, I can introduce my problem just by enabling this gem.

If I have:

require 'hash_dot'
Hash.use_dot_syntax = true

I Receive on a nested association query:

Association named 'to_ary' was not found on GameData; perhaps you misspelled it? (ActiveRecord::AssociationNotFoundError)

I believe its specifically interferring with the arel table. Which I imagine is using hash and maybe a .method I'm replacing? Maybe .name?

I was going to stick this on stackoverflow, but I figured since its specifically related to the gem, this would be the right place.

Respect default values

Current behavior.

h1 = {}.to_dot
h2 = Hash.new('default').to_dot
h1.a => NoMethodError
h2.a => NoMethodError

Expected/desired behavior.

h1 = {}.to_dot
h2 = Hash.new('default').to_dot
h1.a => NoMethodError
h2.a => 'default'

unable to use send for nested hashes

I have written functionality to allow for the following specs to pass.
I am interested in creating a pr but permissions are blocking me.

... spec/hash_dot_spec.rb

   it "allows dot send access for a nested instance" do
      one = { a: { b: 1 } }.to_dot

      expect( one.send('a.b') ).to eq( 1 )
    end

    it "allows dot send set for a nested instance" do
      one = { a: { b: nil } }.to_dot
      one.send("a.b=", 1)

      expect( one.send("a.b") ).to eq( 1 )
      expect( one.a.b ).to eq( 1 )
    end

#respond_to? should be true if the property exists

Since hash_dot does not override respond_to? we get weird inconsistent results which can screw up other libraries. (e.g. SimpleDelegator)

[12] pry()> hash = {'foo' => 'bar'}.to_dot
=> {"foo"=>"bar"}
[13] pry()> hash.foo
=> "bar"
[14] pry()> hash.respond_to?(:foo)
=> false

This should be fairly easy to accomplish. I'll try and put together a pull request this weekend.

Default values

Hi,

I am looking to using this nice gem, and would like to raise a couple of points:

1. Readme mistake?

It seems like the README is wrong about what happens when you use default values globally. This code:

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

returns nil, and not 'default'. I think it is an important distinction to make, since this would probably be the desired behavior by people who are using global default.

2. Syntax for instance defaults

Another thing I was wondering about, is the syntax required to make a default on a per instance basis.

Right now, the README states that this is how to do it:

h = Hash.new('default').to_dot(use_default: true)

But wouldn't it be more idiomatic and intuitive to allow users to do this instead?

h = { my: 'hash' }.to_dot default: nil

3. Refinements instead of monkey-patching

And one last thought - since this is monkey patching Hash (and Symbol), which are very fundumental components of the language, and are heavily used (and patched) in Rails - wouldn't it be safer to use refinements?

EDIT: I take back the refinements thought, it will probably make it safer but much less usable.

#to_dot doesn't traverse arrays

Test case:

require 'hash_dot'
{foo: {bar: 1}}.to_dot.foo.bar
#=> 1
{foo: [{bar: 1}]}.to_dot.foo.first.bar
#=> NoMethodError: undefined method `bar' for {:bar=>1}:Hash
# from /Users/aidan/.rvm/gems/ruby-2.5.0/gems/hash_dot-2.2.0/lib/hash.rb:17:in `method_missing

Thanks!

Hash with "default" key

Hey,

Is there a workaround for the cases when hash includes keys named "default"?

Ex.:

hash = {
  "key0" => {
    "default" => { "key" => "value" }
  }
}
[5] pry(#<Controller>)> hash = {
  "key0" => {
    "default" => { "key" => "value" }
  }
=> {"key0"=>{"default"=>{"key"=>"value"}}}

[6] pry(#<Controller>)> hash.to_dot
=> {"key0"=>{"default"=>{"key"=>"value"}}}

[7] pry(#<Controller>)> hash.key0
=> {"default"=>{"key"=>"value"}}

[8] pry(#<Controller>)> hash.key0.default
=> nil

Found the default setting but that seems to be a bit different from what I've faced.

Thank you.

Traversing arrays does not work with send

I have this object:

data = {
        "_json" =>
            [{ "object" =>
                   { "user" =>
                         { "name" => "Ray" }
                   }
             }]
    }

The following works:

data._json[0].object.user.name

But this does not:

data.send('_json[0].object.user.name')

Revise benchmarks?

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

user_hash = { address: { category: { desc: 'Urban'}}}

iterations = 50000

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

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

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

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

  user_dot = user_hash.to_dot
  bm.report("Dot Notation Single:") {
    iterations.times do; user_dot.address; end
  }
end

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.