Add attribute check in cached getter

This commit is contained in:
Matija Čupić 2018-05-16 21:36:20 +02:00
parent 42ab6f8557
commit a4b0876b39
No known key found for this signature in database
GPG Key ID: 4BAF84FFACD2E5DE
2 changed files with 6 additions and 0 deletions

View File

@ -8,6 +8,8 @@ module RedisCacheable
def cached_attr_reader(*attributes)
attributes.each do |attribute|
define_method(attribute) do
raise ArgumentError, "Not a database attribute" unless self.has_attribute?(attribute)
cached_attribute(attribute) || read_attribute(attribute)
end
end

View File

@ -10,6 +10,10 @@ describe RedisCacheable do
def cast_value_from_cache(attribute, cached_value)
cached_value
end
def has_attribute?(attribute)
attributes.has_key?(attribute)
end
end
end