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 b037b9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/mobility/attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,11 @@ 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|
next hash[name.to_sym] if String === name
hash[name] = self.class.mobility_backend_class(name).new(self, name.to_s)
end
end
Expand Down
14 changes: 14 additions & 0 deletions spec/mobility/attributes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@
end
end

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

article = Article.new

aggregate_failures do
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.size).to eq(1)
expect(article.mobility_backends[:title]).to eq(article.mobility_backends["title"])
end
end

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

0 comments on commit b037b9b

Please sign in to comment.