Skip to content

Commit

Permalink
Map string keys to symbol keys
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed May 22, 2018
1 parent e9a170e commit 52c35e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/mobility/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,13 @@ def get_class_from_key(parent_class, key)
end

module InstanceMethods
# Return a new backend for an attribute name.
# @return [Hash] Hash of attribute names and backend instances
def mobility_backends
@mobility_backends ||= Hash.new do |hash, name|
hash[name] = self.class.mobility_backend_class(name).new(self, name.to_s)
hash[name] = String === name ?
hash[name.to_sym] :
self.class.mobility_backend_class(name).new(self, name.to_s)
end
end

Expand Down
11 changes: 11 additions & 0 deletions spec/mobility/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@
end
end

it "maps string keys to symbol key values" do
mod = described_class.new("title", backend: :null)
Article.include mod

article = Article.new
expect(article.mobility_backends[:title]).to be_a(Mobility::Backends::Null)
expect(article.mobility_backends["title"]).to be_a(Mobility::Backends::Null)

expect(article.mobility_backends[:title]).to eq(article.mobility_backends["title"])
end

it "resets when model is duplicated" do
mod = described_class.new("title", backend: :null)
Article.include mod
Expand Down

0 comments on commit 52c35e8

Please sign in to comment.