lib/looksist.rb in looksist-0.0.8 vs lib/looksist.rb in looksist-0.1.0
- old
+ new
@@ -1,81 +1,31 @@
require 'looksist/version'
require 'jsonpath'
require 'json'
+require 'looksist/core'
require 'looksist/redis_service'
require 'looksist/hashed'
require 'looksist/her_collection'
+require 'looksist/safe_lru_cache'
+
module Looksist
+
extend ActiveSupport::Concern
- class << self;
- attr_accessor :lookup_store_client, :driver
- end
- module ClassMethods
+ include Core
+ include Hashed
- def bucket_name(entity_id)
- entity = entity_id.to_s.gsub('_id', '')
- entity.pluralize
- end
+ class << self
+ attr_accessor :lookup_store, :driver, :cache_buffer_size, :redis_service
- def memoized(key)
- self.storage ||= OpenStruct.new
- self.storage[key] = self.storage[key] || Looksist.lookup_store_client.get(key)
- end
-
- def mmemoized(key, values)
- key_and_bucket = id_and_buckets.find{|h| h[:id] == key}
- return if key_and_bucket.nil?
- redis_keys = values.collect{|v| redis_key(key_and_bucket[:bucket], v)}
- left_keys_to_lookup = redis_keys.select{|k| self.storage[k].nil?}
- Looksist.lookup_store_client.mapped_mget(left_keys_to_lookup).each do |key, value|
- self.storage[key] = value
+ def configure
+ yield self
+ self.redis_service = Looksist::RedisService.instance do |lookup|
+ lookup.client = self.lookup_store
+ lookup.buffer_size = self.cache_buffer_size || 50000
end
-
end
- def redis_key bucket, value
- [bucket, '/', value].join('')
- end
-
- def lookup(what, using, bucket = bucket_name(using))
- self.lookup_attributes ||= []
- self.id_and_buckets ||= []
- self.id_and_buckets << {id: using, bucket: bucket}
- if what.is_a? Array
- what.each do |method_name|
- define_method(method_name) do
- JSON.parse(self.class.memoized(self.class.redis_key(bucket, self.send(using).try(:to_s))) || '{}')[method_name.to_s]
- end
- self.lookup_attributes << method_name
- end
- else
- define_method(what) do
- self.class.memoized(self.class.redis_key(bucket, self.send(using).try(:to_s)))
- end
- self.lookup_attributes << what.to_sym
- end
- end
end
-
-
- def as_json(opts)
- Looksist.driver.json_opts(self, opts)
- end
-
- included do |base|
- base.class_attribute :lookup_attributes, :storage, :id_and_buckets
- end
-
- module Serializers
- class Her
- class << self
- def json_opts(obj, opts)
- obj.class.lookup_attributes ||= []
- obj.attributes.merge(obj.class.lookup_attributes.each_with_object({}) { |a, acc| acc[a] = obj.send(a) })
- end
- end
- end
- end
end
\ No newline at end of file