adsteel / hash_dot Goto Github PK
View Code? Open in Web Editor NEWUse dot syntax with Ruby hashes.
License: MIT License
Use dot syntax with Ruby hashes.
License: MIT License
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
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.
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'
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')
Hi,
I am looking to using this nice gem, and would like to raise a couple of points:
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.
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
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.
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.
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!
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
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.