Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with hashes not being indifferent access if available on document property build #181

Open
wants to merge 1 commit into
base: 3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dolly/framework_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Dolly
module FrameworkHelper
def rails?
defined?(ActiveSupport::HashWithIndifferentAccess)
!!defined?(ActiveSupport::HashWithIndifferentAccess)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

end
end
end
4 changes: 2 additions & 2 deletions lib/dolly/property.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def cast_value(value)
end

def custom_class(value)
value = value.is_a?(Hash) ? value.symbolize_keys : value
value = value.is_a?(Hash) ? hash_with_indifferent_access_value(value) : value
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to change a explicit hash value to a HashWithI...?? this sound shady

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, its explicit it should keep that value and not be changed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your sample with contracts in School is contracts defined in properties as HashorHashWithIndifferentAccess` ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but this means changing all models to use HashWith... in the class_name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its defined as property :contracts, class_name: Hash, default: Hash.new

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now that i think about it, dolly is already changing hashes to HashWithIndifferent, this was just missing that transformation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, but then this is weird... as there will be cases where it makes more sense a Hash, and we are force-casting that property to HWIA, right now the default is HWIF, for all data that comes in, but properties are casted to the class that we set on the property method, that way people con define the right type for the property they want. Force casting Hash to HWIA, even if the document is parsed as such is confusing...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do property :foo, class_name: Hash I will expect a Hash, not a HashWithINdifferentAccess, and yeah If you want to use HWIA you need to set it as such.

I wonder how Rails work with this...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, then again, a HWIA is a Hash, and dolly is setting now everything as string keys by default, so ok... I guess we can use this. Just want to tested on BS and some other projects that sue newer dolly and see if there arent any side effects

self_klass.new(value)
end

Expand All @@ -34,7 +34,7 @@ def string_value(value)
end

def hash_value(value)
value.to_h
hash_with_indifferent_access_value(value)
end

def hash_with_indifferent_access_value(value)
Expand Down
6 changes: 3 additions & 3 deletions lib/dolly/property_set.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Dolly
class PropertySet < Set
def include? key
keys.include?(key)
def include?(key)
keys.include?(key.to_sym)
end

def <<(property)
Expand All @@ -11,7 +11,7 @@ def <<(property)

def [](key)
return detect {|property| property.key == key } if key.is_a?(Symbol)
super
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[] does not exist in Set

detect {|property| property.key.to_s == key }
end

private
Expand Down