Skip to content

Dynamic attribute optimizations #281

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

Merged
merged 5 commits into from
Apr 13, 2018
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
35 changes: 24 additions & 11 deletions lib/json_api_client/helpers/dynamic_attributes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def []=(key, value)
end

def respond_to_missing?(method, include_private = false)
if (method.to_s =~ /^(.*)=$/) || has_attribute?(method)
if has_attribute?(method) || method.to_s.end_with?('=')
true
else
super
Expand All @@ -38,16 +38,19 @@ def has_attribute?(attr_name)
protected

def method_missing(method, *args, &block)
normalized_method = if key_formatter
key_formatter.unformat(method.to_s)
else
method.to_s
end

if normalized_method =~ /^(.*)=$/
set_attribute($1, args.first)
elsif has_attribute?(method)
attributes[method]
if has_attribute?(method)
self.class.class_eval do
define_method(method) do
attributes[method]
end
end
return send(method)
end

normalized_method = safe_key_formatter.unformat(method.to_s)

if normalized_method.end_with?('=')
set_attribute(normalized_method[0..-2], args.first)
else
super
end
Expand All @@ -61,10 +64,20 @@ def set_attribute(name, value)
attributes[name] = value
end

def safe_key_formatter
@safe_key_formatter ||= (key_formatter || DefaultKeyFormatter.new)
end

def key_formatter
self.class.respond_to?(:key_formatter) && self.class.key_formatter
end

class DefaultKeyFormatter
def unformat(method)
method.to_s
end
end

end
end
end
42 changes: 42 additions & 0 deletions test/unit/benchmark_dynamic_attributes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'test_helper'
require 'benchmark'

class BenchmarkDynamicAttributesTest < MiniTest::Test
def test_can_parse_global_meta_data
stub_request(:get, "http://example.com/articles/1")
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
data: {
type: "articles",
id: "1",
attributes: {
title: "Rails is Omakase"
}
},
meta: {
copyright: "Copyright 2015 Example Corp.",
authors: [
"Yehuda Katz",
"Steve Klabnik",
"Dan Gebhardt"
]
},
}.to_json)

article = Article.find(1).first

assert_equal "Rails is Omakase", article.title
assert_equal "1", article.id

n = 10_000
puts
Benchmark.bm do |x|
x.report('read: ') { n.times { article.title; article.id } }
x.report('write:') do
n.times do
article.title = 'New title'
article.better_title = 'Better title'
end
end
end
end
end
12 changes: 6 additions & 6 deletions test/unit/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ def test_formatted_key_accessors
end

with_altered_config(Article, :json_key_format => :underscored_key) do
article = Article.new("foo-bar" => "baz")
article = Article.new("foo-bam" => "baz")
# Does not recognize dasherized attributes, fall back to hash syntax
refute article.respond_to? :foo_bar
assert_equal("baz", article.send("foo-bar"))
assert_equal("baz", article.send(:"foo-bar"))
assert_equal("baz", article["foo-bar"])
assert_equal("baz", article[:"foo-bar"])
refute article.respond_to? :foo_bam
assert_equal("baz", article.send("foo-bam"))
assert_equal("baz", article.send(:"foo-bam"))
assert_equal("baz", article["foo-bam"])
assert_equal("baz", article[:"foo-bam"])
end
end

Expand Down