Skip to content

Commit

Permalink
Merge pull request #6 from PikachuEXE/refactor/no-eval
Browse files Browse the repository at this point in the history
Remove usage of `eval`.
  • Loading branch information
nickcharlton committed Apr 20, 2015
2 parents 0684dfb + 5a95e95 commit 113b122
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 70 deletions.
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

0 comments on commit 113b122

Please sign in to comment.