looksist 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +7 -0
- data/lib/looksist/version.rb +3 -0
- data/lib/looksist.rb +61 -0
- data/looksist.gemspec +32 -0
- data/spec/looksist_spec.rb +109 -0
- data/spec/spec_helper.rb +26 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 71664ddcb9ae9f2c46b91072a8483ccc450b5570
|
4
|
+
data.tar.gz: 5ccb029a61016e3885c2b4b728c8e6d0b2a5a781
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a926878fba12379bbbdddef54c6547e816338d5105cb03e3c64f75ce69e4a29ac503ebb8dfa8ecf8dd988b937cd3a8ac75ceb83f162340056d7464f163f0269
|
7
|
+
data.tar.gz: 113af2b37ef8237d31d491a9f898144f6adb5ce1d4dd8b52acbef52d0c4dd7ece390e43d17747513d2a305a6386cf5be36bcfaa4137cf2ad6bfd9f85a595ac26
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.idea
|
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
lookist
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.2
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Simon
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Herdis
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/jpsimonroy/herdis.png?branch=master)](https://travis-ci.org/jpsimonroy/herdis)
|
4
|
+
|
5
|
+
TODO: Write a gem description
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'herdis'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install herdis
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/herdis/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/looksist.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'looksist/version'
|
2
|
+
|
3
|
+
module Looksist
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
class << self;
|
6
|
+
attr_accessor :lookup_store_client, :driver
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def bucket_name(entity_id)
|
12
|
+
entity = entity_id.to_s.gsub('_id', '')
|
13
|
+
entity.pluralize
|
14
|
+
end
|
15
|
+
|
16
|
+
def lookup(what, using, bucket = bucket_name(using))
|
17
|
+
self.lookup_attributes ||= []
|
18
|
+
if what.is_a? Array
|
19
|
+
what.each do |method_name|
|
20
|
+
define_method(method_name) do
|
21
|
+
key = [bucket, '/', self.send(using).try(:to_s)].join('')
|
22
|
+
JSON.parse(send(:memoized, key))[method_name.to_s]
|
23
|
+
end
|
24
|
+
self.lookup_attributes << method_name
|
25
|
+
end
|
26
|
+
else
|
27
|
+
define_method(what) do
|
28
|
+
key = [bucket, '/', self.send(using).try(:to_s)].join('')
|
29
|
+
send(:memoized, key)
|
30
|
+
end
|
31
|
+
self.lookup_attributes << what.to_sym
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def as_json(opts)
|
37
|
+
Looksist.driver.json_opts(self, opts)
|
38
|
+
end
|
39
|
+
|
40
|
+
included do |base|
|
41
|
+
base.class_attribute :lookup_attributes
|
42
|
+
end
|
43
|
+
|
44
|
+
module Serializers
|
45
|
+
class Her
|
46
|
+
class << self
|
47
|
+
def json_opts(obj, opts)
|
48
|
+
obj.class.lookup_attributes ||= []
|
49
|
+
obj.attributes.merge(obj.class.lookup_attributes.each_with_object({}) { |a, acc| acc[a] = obj.send(a) })
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def memoized(key)
|
58
|
+
@storage = @storage || OpenStruct.new
|
59
|
+
@storage[key] = @storage[key] || Looksist.lookup_store_client.get(key)
|
60
|
+
end
|
61
|
+
end
|
data/looksist.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'looksist/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'looksist'
|
8
|
+
spec.version = Lookist::VERSION
|
9
|
+
spec.authors = %w(RC Simon)
|
10
|
+
spec.email = %w(rmchandru@thoughtworks.com simonroy@thoughtworks.com)
|
11
|
+
spec.summary = %q{Redis backed lookup for your her models}
|
12
|
+
spec.description = %q{Redis backed lookup for your her models}
|
13
|
+
spec.homepage = 'https://github.com/jpsimonroy/herdis'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'pry'
|
23
|
+
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'activesupport'
|
26
|
+
spec.add_development_dependency 'activemodel'
|
27
|
+
spec.add_development_dependency 'her'
|
28
|
+
spec.add_development_dependency 'faraday'
|
29
|
+
spec.add_development_dependency 'faraday_middleware'
|
30
|
+
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
describe Looksist do
|
5
|
+
before :each do
|
6
|
+
Looksist.lookup_store_client = double('store_lookup_client')
|
7
|
+
Looksist.driver = Looksist::Serializers::Her
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'Serialization Support' do
|
11
|
+
it 'should decorate for her models' do
|
12
|
+
module Her
|
13
|
+
class Employee
|
14
|
+
include Her::Model
|
15
|
+
use_api TEST_API
|
16
|
+
include Looksist
|
17
|
+
|
18
|
+
lookup :name, using = :employee_id
|
19
|
+
|
20
|
+
def as_json(opts)
|
21
|
+
super(opts).merge(another_attr: 'Hello World')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
expect(Looksist.lookup_store_client).to receive(:get).with('employees/1').and_return('Employee Name')
|
26
|
+
e = Her::Employee.new({employee_id: 1})
|
27
|
+
expect(e.name).to eq('Employee Name')
|
28
|
+
expect(e.to_json).to eq({:employee_id => 1, :name => 'Employee Name', :another_attr => 'Hello World'}.to_json)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'Store Lookup' do
|
33
|
+
it 'should consider bucket key when provided' do
|
34
|
+
module ExplicitBucket
|
35
|
+
class Employee
|
36
|
+
include Looksist
|
37
|
+
attr_accessor :id
|
38
|
+
lookup :name, using = :id, bucket_name = 'employees'
|
39
|
+
|
40
|
+
def initialize(id)
|
41
|
+
@id = id
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
expect(Looksist.lookup_store_client).to receive(:get).with('employees/1').and_return('Employee Name')
|
46
|
+
e = ExplicitBucket::Employee.new(1)
|
47
|
+
expect(e.name).to eq('Employee Name')
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'Lazy Evaluation' do
|
52
|
+
module LazyEval
|
53
|
+
class Employee
|
54
|
+
include Looksist
|
55
|
+
attr_accessor :id
|
56
|
+
lookup :name, using = :id, bucket_name = 'employees'
|
57
|
+
|
58
|
+
def initialize(id)
|
59
|
+
@id = id
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
it 'should not eager evaluate' do
|
64
|
+
expect(Looksist.lookup_store_client).to_not receive(:get)
|
65
|
+
LazyEval::Employee.new(1)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context 'lookup attributes' do
|
70
|
+
it 'should generate declarative attributes on the model with simple lookup value' do
|
71
|
+
module SimpleLookup
|
72
|
+
class Employee
|
73
|
+
include Looksist
|
74
|
+
attr_accessor :id
|
75
|
+
lookup :name, using= :id
|
76
|
+
|
77
|
+
def initialize(id)
|
78
|
+
@id = id
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
expect(Looksist.lookup_store_client).to receive(:get).with('ids/1').and_return('Employee Name')
|
84
|
+
e = SimpleLookup::Employee.new(1)
|
85
|
+
expect(e.name).to eq('Employee Name')
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should generate declarative attributes on the model with object based lookup value' do
|
89
|
+
module CompositeLookup
|
90
|
+
class Employee
|
91
|
+
include Looksist
|
92
|
+
attr_accessor :id
|
93
|
+
|
94
|
+
lookup [:name, :location], using=:id
|
95
|
+
|
96
|
+
def initialize(id)
|
97
|
+
@id = id
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
expect(Looksist.lookup_store_client).to receive(:get).with('ids/1')
|
103
|
+
.and_return({name: 'Employee Name', location: 'Chennai'}.to_json)
|
104
|
+
e = CompositeLookup::Employee.new(1)
|
105
|
+
expect(e.name).to eq('Employee Name')
|
106
|
+
expect(e.location).to eq('Chennai')
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'faraday_middleware'
|
3
|
+
require 'her'
|
4
|
+
require 'looksist'
|
5
|
+
require 'pry'
|
6
|
+
|
7
|
+
module Her
|
8
|
+
module Model
|
9
|
+
def as_json(opts={})
|
10
|
+
attributes
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
TEST_API = Her::API.new
|
16
|
+
|
17
|
+
config = Proc.new do |conn|
|
18
|
+
conn.use FaradayMiddleware::EncodeJson
|
19
|
+
conn.use Faraday::Request::UrlEncoded
|
20
|
+
conn.use Her::Middleware::DefaultParseJSON
|
21
|
+
conn.use Faraday::Adapter::NetHttp
|
22
|
+
conn.use Faraday::Response::RaiseError
|
23
|
+
end
|
24
|
+
|
25
|
+
TEST_API.setup url: 'http://dummy.com', &config
|
26
|
+
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: looksist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- RC
|
8
|
+
- Simon
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-10-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.6'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.6'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: pry
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rake
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: activesupport
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: activemodel
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: her
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: faraday
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: faraday_middleware
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
description: Redis backed lookup for your her models
|
141
|
+
email:
|
142
|
+
- rmchandru@thoughtworks.com
|
143
|
+
- simonroy@thoughtworks.com
|
144
|
+
executables: []
|
145
|
+
extensions: []
|
146
|
+
extra_rdoc_files: []
|
147
|
+
files:
|
148
|
+
- ".gitignore"
|
149
|
+
- ".rspec"
|
150
|
+
- ".ruby-gemset"
|
151
|
+
- ".ruby-version"
|
152
|
+
- ".travis.yml"
|
153
|
+
- Gemfile
|
154
|
+
- LICENSE.txt
|
155
|
+
- README.md
|
156
|
+
- Rakefile
|
157
|
+
- lib/looksist.rb
|
158
|
+
- lib/looksist/version.rb
|
159
|
+
- looksist.gemspec
|
160
|
+
- spec/looksist_spec.rb
|
161
|
+
- spec/spec_helper.rb
|
162
|
+
homepage: https://github.com/jpsimonroy/herdis
|
163
|
+
licenses:
|
164
|
+
- MIT
|
165
|
+
metadata: {}
|
166
|
+
post_install_message:
|
167
|
+
rdoc_options: []
|
168
|
+
require_paths:
|
169
|
+
- lib
|
170
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
|
+
requirements:
|
177
|
+
- - ">="
|
178
|
+
- !ruby/object:Gem::Version
|
179
|
+
version: '0'
|
180
|
+
requirements: []
|
181
|
+
rubyforge_project:
|
182
|
+
rubygems_version: 2.2.2
|
183
|
+
signing_key:
|
184
|
+
specification_version: 4
|
185
|
+
summary: Redis backed lookup for your her models
|
186
|
+
test_files:
|
187
|
+
- spec/looksist_spec.rb
|
188
|
+
- spec/spec_helper.rb
|