venduitz 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +22 -0
- data/lib/venduitz/cache.rb +84 -0
- data/lib/venduitz/version.rb +1 -1
- data/lib/venduitz/view.rb +43 -5
- data/lib/venduitz.rb +2 -0
- data/spec/venduitz/cache_spec.rb +95 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 384de3930c931de488374367ac00caea085a6b62
|
4
|
+
data.tar.gz: a143824344b230d069ec60690c0e121c9af4b485
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ab8cab911841cfcfd7a3a073d068f08b408103df125e87801ed26ef5fcfe40329858d0dae50f5b3bb1cdfa07b9114ea794bf3f3090b8ff04a773c646426732d
|
7
|
+
data.tar.gz: 132d6ad295873f43c0807cc0f43c605a4c48d571a74e81740eede1e28edc6606b5c404860be51716d8caa7e7fe37834b529af607b1a94e8ec8bbb4f98f702673
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -70,6 +70,28 @@ res = SearchView.to_json(search, [:images])
|
|
70
70
|
[200, {'Content-Type' => 'application/json'}, res]
|
71
71
|
```
|
72
72
|
|
73
|
+
### Using Cache
|
74
|
+
To use cache you have to define a Driver for the Venduitz:
|
75
|
+
```
|
76
|
+
# Defining the CacheDriver
|
77
|
+
Venduitz::Cache.driver = CacheDriver
|
78
|
+
|
79
|
+
# This cache driver must define some methods (specified on the Vendtuiz::Cache module)
|
80
|
+
|
81
|
+
# As common, generate your view but with some differences
|
82
|
+
# `true` in this case will enable the cache
|
83
|
+
res = SearchView.to_json(search, [:images], true)
|
84
|
+
|
85
|
+
# To use a 'partial views' the method #generate also supports cache
|
86
|
+
class CachedView < ProductView
|
87
|
+
prop :cover, -> (product) { ImageView.generate(product.cover, [], true) }
|
88
|
+
end
|
89
|
+
|
90
|
+
# To pass the expiration time to your driver just use a fourth argument
|
91
|
+
# to define the driver call options
|
92
|
+
res = SearchView.to_json(search, [:images], true, { expire_in: 10.minutes })
|
93
|
+
```
|
94
|
+
|
73
95
|
## Development
|
74
96
|
* Building the docker container: `docker build -t venduitz .`
|
75
97
|
* Running the tests:
|
@@ -0,0 +1,84 @@
|
|
1
|
+
#
|
2
|
+
module Venduitz
|
3
|
+
# Module responsible for creating an interface
|
4
|
+
# for the different cache storages
|
5
|
+
module Cache
|
6
|
+
# Static methods
|
7
|
+
class << self
|
8
|
+
#
|
9
|
+
attr_accessor :digestor, :driver, :namespace
|
10
|
+
|
11
|
+
# Before use it you have to specify the driver used by
|
12
|
+
# this module. The driver must define the following
|
13
|
+
# methods:
|
14
|
+
# - exist?(key)
|
15
|
+
# - get(key)
|
16
|
+
# - set(key, value, options)
|
17
|
+
# - clear(options)
|
18
|
+
|
19
|
+
# Digest
|
20
|
+
def digest(object)
|
21
|
+
# The dumped data
|
22
|
+
dump = Marshal::dump(object)
|
23
|
+
|
24
|
+
# Check if the digestor is empty
|
25
|
+
digestor = Digest::MD5 if digestor.nil?
|
26
|
+
|
27
|
+
# Digest it
|
28
|
+
digestor.hexdigest dump
|
29
|
+
end
|
30
|
+
|
31
|
+
# Check if the key exists
|
32
|
+
def exist?(key)
|
33
|
+
_driver.exist?(key)
|
34
|
+
end
|
35
|
+
|
36
|
+
# Get the key
|
37
|
+
def get(key)
|
38
|
+
# Using the driver itself
|
39
|
+
_driver.get(key)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Set the cache
|
43
|
+
# @param {Any} key The key used on the cache
|
44
|
+
# @param {Boolean} dump If is necessary to dump the Key
|
45
|
+
# @param {Any} value The value of the cache
|
46
|
+
def set(key, value, dump = true, options = {})
|
47
|
+
#
|
48
|
+
key = digest(key) if dump
|
49
|
+
|
50
|
+
# If the driver has any namespace method or fixed value
|
51
|
+
key = "#{namespace}/#{key}" if namespace?
|
52
|
+
|
53
|
+
#
|
54
|
+
_driver.set(key, value, options)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Clear the full cache
|
58
|
+
# @param {Hash} opts Options to use while clear the cache
|
59
|
+
def clear(opts = nil)
|
60
|
+
_driver.clear(opts)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Check if there is namespace
|
64
|
+
def namespace?
|
65
|
+
!@namespace.nil?
|
66
|
+
end
|
67
|
+
|
68
|
+
# Get the namespace
|
69
|
+
def namespace
|
70
|
+
@namespace.respond_to(:call) ? @namespace.call : @namespace
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def _driver
|
76
|
+
# Check if the driver was defined
|
77
|
+
throw 'You have to define your cache driver before start using it' if driver.nil?
|
78
|
+
|
79
|
+
# Return the driver
|
80
|
+
driver
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
data/lib/venduitz/version.rb
CHANGED
data/lib/venduitz/view.rb
CHANGED
@@ -37,8 +37,16 @@ module Venduitz
|
|
37
37
|
# for the specific argument
|
38
38
|
# @param {Object} obj This must contain the declared properies and collections
|
39
39
|
# @param {Array} excluded This array contain the keys that will not be added to the result
|
40
|
+
# @param {Boolean} cache This flag indicates if cache will be performed
|
41
|
+
# @param {Hash} cache_options Cache options
|
40
42
|
# @return {Hash} JSON ready hash containing the specified view fields
|
41
|
-
def generate(obj, excluded = [])
|
43
|
+
def generate(obj, excluded = [], cache = false, cache_options = {})
|
44
|
+
# Generate the cache key if cache is enabled
|
45
|
+
key = cache_key(obj, excluded, :generate) if cache
|
46
|
+
|
47
|
+
# Cache?
|
48
|
+
return Cache.get(key) if cache && Cache.exist?(key)
|
49
|
+
|
42
50
|
# Reset the values to prevent errors
|
43
51
|
@props = {} if @props.nil?
|
44
52
|
@collects = {} if @collects.nil?
|
@@ -60,17 +68,47 @@ module Venduitz
|
|
60
68
|
[collect, values.map {|val| info[:view].generate(val, info[:excluded]) }]
|
61
69
|
end
|
62
70
|
|
63
|
-
#
|
64
|
-
Hash[props].merge(Hash[coll])
|
71
|
+
# Full hash
|
72
|
+
result = Hash[props].merge(Hash[coll])
|
73
|
+
|
74
|
+
# Check if cache has to be created
|
75
|
+
Cache.set(key, result, cache_options) if cache
|
76
|
+
|
77
|
+
# Return the result
|
78
|
+
result
|
65
79
|
end
|
66
80
|
|
67
81
|
# Parse it
|
68
82
|
# @param {Object} obj This must contain the declared properies and collections
|
69
83
|
# @param {Array} excluded This array contain the keys that will not be added to the result
|
84
|
+
# @param {Boolean} cache This flag indicates if cache will be performed
|
85
|
+
# @param {Hash} cache_options Cache options
|
70
86
|
# @return {Hash} The JSON string itself
|
71
|
-
def to_json(obj, excluded = [])
|
87
|
+
def to_json(obj, excluded = [], cache = false, cache_options = {})
|
88
|
+
# Generate the cache key if cache is enabled
|
89
|
+
key = cache_key(obj, excluded, :json) if cache
|
90
|
+
|
91
|
+
# Cache?
|
92
|
+
return Cache.get(key) if cache && Cache.exist?(key)
|
93
|
+
|
72
94
|
# Transform the result into json
|
73
|
-
MultiJson.dump generate(obj, excluded)
|
95
|
+
result = MultiJson.dump generate(obj, excluded)
|
96
|
+
|
97
|
+
# Cache must be performed?
|
98
|
+
Cache.set(key, result, cache_options) if cache
|
99
|
+
|
100
|
+
# Return the result
|
101
|
+
result
|
102
|
+
end
|
103
|
+
|
104
|
+
#
|
105
|
+
private
|
106
|
+
|
107
|
+
# Generate the cache key
|
108
|
+
# CACHE
|
109
|
+
# Key: "{object}/{excluded}/{type}"
|
110
|
+
def cache_key(obj, excluded, type)
|
111
|
+
"#{Cache.digest(obj)}/#{Cache.digest(excluded)}/#{type.to_s}"
|
74
112
|
end
|
75
113
|
end
|
76
114
|
end
|
data/lib/venduitz.rb
CHANGED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Include the helper
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
# Define a simple cache driver
|
5
|
+
class CacheDriver
|
6
|
+
# Define the storage
|
7
|
+
attr_reader :storage
|
8
|
+
|
9
|
+
# Init!
|
10
|
+
def initialize
|
11
|
+
# Initialize the storage
|
12
|
+
@storage = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
#
|
16
|
+
def exist?(key)
|
17
|
+
@storage.key?(key)
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
def set(key, value, options = {})
|
22
|
+
@storage[key] = value
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
def get(key)
|
27
|
+
@storage[key]
|
28
|
+
end
|
29
|
+
|
30
|
+
#
|
31
|
+
def clear(options = nil)
|
32
|
+
@storage = {}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
# Cache object tes
|
37
|
+
class CacheSample < SubSample
|
38
|
+
#
|
39
|
+
attr_reader :other
|
40
|
+
|
41
|
+
# Init!
|
42
|
+
def initialize(time)
|
43
|
+
@other = time
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Set the driver to the Venduitz
|
48
|
+
Venduitz::Cache.driver = CacheDriver.new
|
49
|
+
|
50
|
+
#
|
51
|
+
describe Venduitz::Cache do
|
52
|
+
#
|
53
|
+
before(:each) do
|
54
|
+
# Clear the cache
|
55
|
+
Venduitz::Cache.clear
|
56
|
+
end
|
57
|
+
|
58
|
+
#
|
59
|
+
it 'should set/get a cache withoud digest' do
|
60
|
+
# Set the cache (withoud digest)
|
61
|
+
Venduitz::Cache.set 'hello', 'here', false
|
62
|
+
|
63
|
+
# Assertions
|
64
|
+
expect(Venduitz::Cache.exist?('hello')).to eq(true)
|
65
|
+
expect(Venduitz::Cache.get('hello')).to eq('here')
|
66
|
+
end
|
67
|
+
|
68
|
+
#
|
69
|
+
it 'should set/get a cache withoud digest' do
|
70
|
+
# Set the cache (withoud digest)
|
71
|
+
Venduitz::Cache.set 'hello', 'here'
|
72
|
+
|
73
|
+
# Get the digested key
|
74
|
+
key = Venduitz::Cache.digest 'hello'
|
75
|
+
|
76
|
+
# Get it
|
77
|
+
expect(Venduitz::Cache.get(key)).to eq('here')
|
78
|
+
end
|
79
|
+
|
80
|
+
#
|
81
|
+
it 'should generate a view with cache' do
|
82
|
+
# Get the time now and save it
|
83
|
+
time = Time.now
|
84
|
+
|
85
|
+
# Gen the JSON
|
86
|
+
res = [SubSampleView.to_json(CacheSample.new(time), [], true)]
|
87
|
+
|
88
|
+
# Generate it again
|
89
|
+
res += [SubSampleView.to_json(CacheSample.new(time), [], true)]
|
90
|
+
|
91
|
+
# Check Cache entries
|
92
|
+
expect(Venduitz::Cache.driver.storage.keys.size).to eq(1)
|
93
|
+
expect(Venduitz::Cache.get(Venduitz::Cache.driver.storage.keys[0])).to eq(res[0])
|
94
|
+
end
|
95
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: venduitz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Gabriel Corado
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: multi_json
|
@@ -65,10 +65,12 @@ files:
|
|
65
65
|
- Gemfile.lock
|
66
66
|
- README.md
|
67
67
|
- lib/venduitz.rb
|
68
|
+
- lib/venduitz/cache.rb
|
68
69
|
- lib/venduitz/grape.rb
|
69
70
|
- lib/venduitz/version.rb
|
70
71
|
- lib/venduitz/view.rb
|
71
72
|
- spec/spec_helper.rb
|
73
|
+
- spec/venduitz/cache_spec.rb
|
72
74
|
- spec/venduitz/grape_spec.rb
|
73
75
|
- spec/venduitz/view_spec.rb
|
74
76
|
- venduitz.gemspec
|