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

$ Remove usage of eval #6

Merged
merged 1 commit into from
Apr 20, 2015
Merged
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
39 changes: 21 additions & 18 deletions lib/key_path/enumerable/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,32 @@ def set_keypath(keypath, value)
# handle both string and KeyPath::Path forms
keypath = keypath.to_keypath if keypath.is_a?(String)

# create a collection at the keypath
collection = keypath.to_collection

# set the value in the collection
depth = ''
keypath.to_a.each do |e|
# walk down set and make up the right place to assign
if e.is_number?
key = "#{e}"
else
key = ":#{e}"
end
depth << "[#{key}]"
keypath_parts = keypath.to_a
# Return self if path empty
return self if keypath_parts.empty?

key = keypath_parts.shift
# Just assign value to self when it's a direct path
# Remember, this is after calling keypath_parts#shift
if keypath_parts.length == 0
key = key.is_number? ? Integer(key) : key.to_sym

self[key] = value
return self
end

# assign it
if value.is_a? String
eval "collection#{depth} = '#{value}'"
# keypath_parts.length > 0
# Remember, this is after calling keypath_parts#shift
collection = if key.is_number?
Array.new
else
eval "collection#{depth} = #{value}"
Hash.new
end

# Remember, this is after calling keypath_parts#shift
collection.set_keypath(keypath_parts.join('.'), value)

# merge the new collection into self
self.deep_merge!(collection)
self[key] = collection
end
end
25 changes: 0 additions & 25 deletions lib/key_path/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,6 @@ def to_a
@path.split('.')
end

def to_collection
collection = {}
s = to_a
depth = ''

s.each do |e|
# assemble the key
if e.is_number?
key = "#{e}"
else
key = ":#{e}"
end
depth << "[#{key}]"

# figure out the correct type to push
type = {}
type = [] if e.is_plural?

# evaluate this stage
eval "collection#{depth} = #{type}"
end

collection
end

def inspect
"#<#{self.class.name}:#{object_id} path=#{@path}>"
end
Expand Down
27 changes: 0 additions & 27 deletions spec/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,3 @@
path.parent.must_be_nil
end
end

describe 'KeyPath::Path collections generation' do
it 'returns an empty hash with an empty path' do
path = KeyPath::Path.new('')
path.to_collection.must_equal({})
end

it 'returns a nested hash for a single path unit' do
path = KeyPath::Path.new('item')
path.to_collection.must_equal(item: {})
end

it 'returns a nested array when the key is plural' do
path = KeyPath::Path.new('items')
path.to_collection.must_equal(items: [])
end

it 'returns a double nested array with a two set keypath' do
path = KeyPath::Path.new('item.id')
path.to_collection.must_equal(item: { id: {} })
end

it 'returns a nested array with an item' do
path = KeyPath::Path.new('items.0.id')
path.to_collection.must_equal(items: [{ id: {} }])
end
end