Skip to content

Commit 2216ed9

Browse files
Merge pull request #18 from Sage/feature_attribute_caching
Feature - Attribute Store
2 parents 3223e89 + b5c2b43 commit 2216ed9

6 files changed

Lines changed: 68 additions & 2 deletions

File tree

benchmark/Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
source 'http://rubygems.org'
2+
gem 'class_kit'

benchmark/Gemfile.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
class_kit (0.6.0)
5+
hash_kit
6+
json
7+
hash_kit (0.6.0)
8+
json (2.1.0)
9+
10+
PLATFORMS
11+
ruby
12+
13+
DEPENDENCIES
14+
class_kit
15+
16+
BUNDLED WITH
17+
1.16.1

benchmark/benchmark.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require_relative '../lib/class_kit'
2+
require 'date'
3+
require 'benchmark'
4+
5+
class Item
6+
extend ClassKit
7+
8+
attr_accessor_type :text, type: String
9+
attr_accessor_type :integer, type: Integer
10+
attr_accessor_type :float, type: Float
11+
attr_accessor_type :date, type: Date
12+
attr_accessor_type :time, type: Time
13+
attr_accessor_type :bool, type: :bool
14+
end
15+
16+
items = []
17+
10_000.times do
18+
items << Item.new.tap do |e|
19+
e.text = 'foo bar'
20+
e.integer = 50
21+
e.float = 25.2
22+
e.date = Date.today
23+
e.time = Time.now
24+
e.bool = true
25+
end
26+
end
27+
28+
helper = ClassKit::Helper.new
29+
30+
json = ''
31+
32+
puts '***serialize items***'
33+
puts Benchmark.measure { json = helper.to_json(items) }
34+
35+
puts '***deserialize items***'
36+
puts Benchmark.measure { helper.from_json(json: json, klass: Item) }

lib/class_kit/attribute_helper.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ def self.instance
55
@instance ||= ClassKit::AttributeHelper.new
66
end
77

8+
def initialize
9+
@attribute_store = {}
10+
end
11+
812
# Get attributes for a given class
913
#
1014
# @param klass [ClassKit] a class that has been extended with ClassKit
1115
#
1216
# @return [Hash]
1317
def get_attributes(klass)
18+
return @attribute_store[klass] if @attribute_store.key?(klass)
19+
1420
attributes = []
1521
klass.ancestors.map do |k|
1622
hash = k.instance_variable_get(:@class_kit_attributes)
@@ -20,7 +26,10 @@ def get_attributes(klass)
2026
end
2127
end
2228
end
23-
attributes.compact
29+
attributes.compact!
30+
31+
@attribute_store[klass] = attributes
32+
attributes
2433
end
2534

2635
# Get attribute for a given class and name

lib/class_kit/helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def to_hash(object, use_alias = false)
5353
def from_hash(hash:, klass:, use_alias: false)
5454
validate_class_kit(klass)
5555

56+
return hash.map { |i| from_hash(hash: i, klass: klass, use_alias: use_alias) } if hash.is_a?(Array)
57+
5658
@hash_helper.indifferent!(hash)
5759
entity = klass.new
5860
attributes = @attribute_helper.get_attributes(klass)

lib/class_kit/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Namespace
22
module ClassKit
33
# :nodoc:
4-
VERSION = "0.6.0"
4+
VERSION = '0.7.0'
55
end

0 commit comments

Comments
 (0)