hid 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NjQ5MGI4NzQ4YzJlMmEwMTgxZDdlYjljMTU1ODNkNDk1ZGNmNmQ5MA==
5
+ data.tar.gz: !binary |-
6
+ YzZlNTQ1OTk4ZGYwMjBjMzI3Y2UyZWIwMzc3ODRmMWQwOWYyNmJjOA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ NGVmMmRhZmNmYzQ5MGM4MWZiOTAyOGVmOTVjNGQwOTZkNmM5N2QzZjdhOTRk
10
+ MzM1ZjYzZWUwODRlNjAxN2QwZjdjOTNmMTgwZTEyMDhiYmUyMjg0MTZhZjg0
11
+ YzlmNTIyNmQwMjNkYzk3MWIwMzUxZjcxZGE0NDY1NWRlNTFhZTU=
12
+ data.tar.gz: !binary |-
13
+ OTdjYjIwNGZjYzVjZWExYTNhM2VlMGI5YTI5NDk0MjA1NDJjYjBmNDYzN2E2
14
+ NTVkY2QxMmU2NjlkODNhMjkwNWU1YzgxOTliMGZhZjA0Mzc2MTA1NmNlMzIx
15
+ ZWE0ZTJjNDM0OTZhYWE3NmQ4YWE2MmExNDlkY2ZkY2VjNWQxNDY=
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hid.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Benjamin Cavileer
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,68 @@
1
+ # HID
2
+
3
+ [![Code Climate](https://codeclimate.com/github/bcavileer/hid/badges/gpa.svg)](https://codeclimate.com/github/bcavileer/hid)
4
+
5
+ TODO: Write a gem description
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'hid'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install hid
20
+
21
+ ## Usage
22
+
23
+ Configure (optional):
24
+
25
+ HID.configure do |config|
26
+ config.identifier = SomeIdentifier
27
+ config.recorder = SomeRecorder
28
+ end
29
+
30
+ Examples: (these were executed **in order** in a REPL)
31
+
32
+ HID.identify 'foo'
33
+ => nil
34
+
35
+ HID.map 'bar', 'baz'
36
+ => nil
37
+
38
+ HID.identify 'bar'
39
+ => "baz"
40
+
41
+ HID.recorded
42
+ => {nil=>["foo"]}
43
+
44
+ HID.map 'persistance', 'persistence', 'typos'
45
+ => nil
46
+
47
+ HID.identify 'persistance', 'typos'
48
+ => "persistence"
49
+
50
+ HID.identify 'persistance'
51
+ => nil
52
+
53
+ HID.recorded
54
+ => {nil=>["foo", "persistance"]}
55
+
56
+ HID.identify 'fizz', 'buzz'
57
+ => nil
58
+
59
+ HID.recorded
60
+ => {nil=>["foo", "persistance"], "fizz" => ["buzz"]}
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it ( https://github.com/[my-github-username]/hid/fork )
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/hid.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'hid'
8
+ spec.version = HID::VERSION
9
+ spec.authors = ['Benjamin Cavileer']
10
+ spec.email = ['bcavileer@holmanauto.com']
11
+ spec.summary = %q{Identity Mapping}
12
+ spec.description = %q{Maps string to other strings?}
13
+ spec.homepage = ''
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 'rake'
23
+ spec.add_development_dependency 'rspec'
24
+ spec.add_development_dependency 'activerecord'
25
+ spec.add_development_dependency 'sqlite3'
26
+ spec.add_development_dependency 'database_cleaner'
27
+ end
data/lib/hid.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'hid/version'
2
+ require 'hid/configuration'
3
+
4
+ module HID
5
+ class << self
6
+ attr_accessor :configuration
7
+
8
+ def identify(unknown, type=nil)
9
+ identity = find unknown, type
10
+ return identity if identity
11
+ record unknown, type
12
+ end
13
+
14
+ def map(unknown, identity, type=nil)
15
+ configuration.identifier.map unknown, identity, type
16
+ end
17
+
18
+ def recorded(type=nil)
19
+ configuration.recorder.recorded type
20
+ end
21
+
22
+ def configure
23
+ yield configuration
24
+ end
25
+
26
+ private
27
+
28
+ def find(unknown, type)
29
+ configuration.identifier.find unknown, type
30
+ end
31
+
32
+ def record(unknown, type)
33
+ configuration.recorder.record unknown, type
34
+ end
35
+ end
36
+
37
+ self.configuration ||= Configuration.new
38
+ end
@@ -0,0 +1,25 @@
1
+ module HID
2
+ class Configuration
3
+ attr_accessor :identifier, :recorder
4
+
5
+ def initialize(persistence=:memory)
6
+ case persistence
7
+ when :memory
8
+ require 'hid/persistence/memory/identifier'
9
+ require 'hid/persistence/memory/recorder'
10
+ @identifier = HID::Persistence::Memory::Identifier
11
+ @recorder = HID::Persistence::Memory::Recorder
12
+ @identifier.store = Hash.new
13
+ @recorder.store = Hash.new
14
+ when :active_record
15
+ raise 'activerecord gem is required to use ActiveRecord persistence' unless defined?(::ActiveRecord)
16
+ require 'hid/persistence/active_record/identifier'
17
+ require 'hid/persistence/active_record/recorder'
18
+ @identifier = HID::Persistence::ActiveRecord::Identifier
19
+ @recorder = HID::Persistence::ActiveRecord::Recorder
20
+ else
21
+ raise "unknown internal persistence type #{persistence}"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,24 @@
1
+ module HID
2
+ module Persistence
3
+ module ActiveRecord
4
+ class Identifier
5
+ class Identities < ::ActiveRecord::Base; end
6
+ @identities_class = Identities
7
+
8
+ class << self
9
+ attr_accessor :identities_class
10
+
11
+ def find(input, type)
12
+ identity = identities_class.where(input: input, id_type: type).first
13
+ identity && identity.identity
14
+ end
15
+
16
+ def map(input, identity, type)
17
+ identities_class.create input: input, identity: identity, id_type: type
18
+ nil
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,41 @@
1
+ module HID
2
+ module Persistence
3
+ module ActiveRecord
4
+ class Recorder
5
+ class Records < ::ActiveRecord::Base; end
6
+ @records_class = Records
7
+
8
+ class << self
9
+ attr_accessor :records_class
10
+
11
+ def record(input, type)
12
+ records_class.create input: input, id_type: type
13
+ nil
14
+ end
15
+
16
+ def recorded(type)
17
+ return typed_records(type) if type
18
+ untyped_records
19
+ end
20
+
21
+ private
22
+
23
+ def typed_records(type)
24
+ records_class.where(id_type: type).pluck(:input)
25
+ end
26
+
27
+ def untyped_records
28
+ record_groups = records_class.all.group_by(&:id_type)
29
+ hash = Hash.new
30
+
31
+ record_groups.each do |type, records|
32
+ hash[type] = records.map(&:input) if records
33
+ end
34
+
35
+ hash
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ module HID
2
+ module Persistence
3
+ module Memory
4
+ class Identifier
5
+ class << self
6
+ attr_accessor :store
7
+
8
+ def find(input, type)
9
+ (type_hash = store[type]) && type_hash[input]
10
+ end
11
+
12
+ def map(input, identity, type)
13
+ type_hash = store[type] ||= Hash.new
14
+ type_hash[input] = identity
15
+ nil
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,23 @@
1
+ module HID
2
+ module Persistence
3
+ module Memory
4
+ class Recorder
5
+ class << self
6
+ attr_accessor :store
7
+
8
+ def record(input, type)
9
+ type_array = store[type] ||= []
10
+ return if type_array.include? input
11
+ type_array << input
12
+ nil
13
+ end
14
+
15
+ def recorded(type)
16
+ return store unless type
17
+ store[type] || []
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module HID
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ require 'support/active_record'
3
+
4
+ ACTIVE_RECORD_CONFIG =
5
+
6
+ describe 'ActiveRecord HID' do
7
+ before(:each) do
8
+ HID.configuration = HID::Configuration.new :active_record
9
+ DatabaseCleaner.start
10
+ end
11
+
12
+ after(:each) do
13
+ DatabaseCleaner.clean
14
+ end
15
+
16
+ include_examples 'identifier'
17
+ include_examples 'recorder'
18
+ end
@@ -0,0 +1,73 @@
1
+ RSpec.shared_examples 'identifier' do
2
+ describe 'identified Inputs' do
3
+ context 'without Type argument' do
4
+ context 'nothing mapped' do
5
+ it 'returns nil' do
6
+ expect(
7
+ HID.identify 'input'
8
+ ).to be_nil
9
+ end
10
+ end
11
+
12
+ context 'other mapped' do
13
+ it 'returns nil' do
14
+ HID.map 'input2', 'identity'
15
+
16
+ expect(
17
+ HID.identify 'input'
18
+ ).to be_nil
19
+ end
20
+ end
21
+
22
+ context 'Input mapped with Type' do
23
+ it 'returns nil' do
24
+ HID.map 'input', 'identity', 'type'
25
+
26
+ expect(
27
+ HID.identify 'input'
28
+ ).to be_nil
29
+ end
30
+ end
31
+
32
+ context 'Input mapped without Type' do
33
+ it 'returns Identity' do
34
+ HID.map 'input', 'identity'
35
+
36
+ expect(
37
+ HID.identify 'input'
38
+ ).to eq 'identity'
39
+ end
40
+ end
41
+ end
42
+
43
+ context 'with Type argument' do
44
+ context 'nothing mapped' do
45
+ it 'returns nil' do
46
+ expect(
47
+ HID.identify 'input', 'type'
48
+ ).to be_nil
49
+ end
50
+ end
51
+
52
+ context 'other mapped Type' do
53
+ it 'returns nil' do
54
+ HID.map 'input', 'identity', 'type'
55
+
56
+ expect(
57
+ HID.identify 'input', 'type2'
58
+ ).to be_nil
59
+ end
60
+ end
61
+
62
+ context 'Input mapped' do
63
+ it 'returns Identity' do
64
+ HID.map 'input', 'identity', 'type'
65
+
66
+ expect(
67
+ HID.identify 'input', 'type'
68
+ ).to eq 'identity'
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,85 @@
1
+ RSpec.shared_examples 'recorder' do
2
+ describe 'not identified Inputs' do
3
+ context 'without Type argument' do
4
+ context 'nothing recorded' do
5
+ it 'returns an empty Hash' do
6
+ expect(
7
+ HID.recorded
8
+ ).to eq({})
9
+ end
10
+ end
11
+
12
+ context 'no Types' do
13
+ it 'returns a Hash with key nil and value Array with item Input' do
14
+ HID.identify 'input'
15
+
16
+ expect(
17
+ HID.recorded
18
+ ).to eq nil => ['input']
19
+ end
20
+ end
21
+
22
+ context 'with Types' do
23
+ it 'returns a Hash with key Type and value Array with item Input' do
24
+ HID.identify 'input', 'type'
25
+
26
+ expect(
27
+ HID.recorded
28
+ ).to eq 'type' => ['input']
29
+ end
30
+ end
31
+
32
+ context 'with and without Types' do
33
+ it 'returns full Hash' do
34
+ HID.identify 'input'
35
+ HID.identify 'input2', 'type'
36
+
37
+ expect(
38
+ HID.recorded
39
+ ).to eq nil => ['input'], 'type' => ['input2']
40
+ end
41
+ end
42
+ end
43
+
44
+ context 'with Type argument' do
45
+ context 'nothing recorded' do
46
+ it 'returns an empty Array' do
47
+ expect(
48
+ HID.recorded 'type'
49
+ ).to eq []
50
+ end
51
+ end
52
+
53
+ context 'no Types' do
54
+ it 'returns an empty Array' do
55
+ HID.identify 'input'
56
+
57
+ expect(
58
+ HID.recorded 'type'
59
+ ).to eq []
60
+ end
61
+ end
62
+
63
+ context 'with Types' do
64
+ it 'returns Array with Input' do
65
+ HID.identify 'input', 'type'
66
+
67
+ expect(
68
+ HID.recorded 'type'
69
+ ).to eq ['input']
70
+ end
71
+ end
72
+
73
+ context 'with multiple Types' do
74
+ it 'returns Array with Input' do
75
+ HID.identify 'input', 'type'
76
+ HID.identify 'input2', 'type2'
77
+
78
+ expect(
79
+ HID.recorded 'type'
80
+ ).to eq ['input']
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'In-Memory HID' do
4
+ # create new in-memory stores before each test
5
+ before(:each) do
6
+ HID.configuration = HID::Configuration.new :memory
7
+ end
8
+
9
+ include_examples 'identifier'
10
+ include_examples 'recorder'
11
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'hid'
3
+ require 'examples/identifier'
4
+ require 'examples/recorder'
@@ -0,0 +1,16 @@
1
+ require 'sqlite3'
2
+ require 'active_record'
3
+ require 'database_cleaner'
4
+
5
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
6
+
7
+ ActiveRecord::Migration.create_table :identities do |t|
8
+ t.string :input, null: false
9
+ t.string :identity, null: false
10
+ t.string :id_type
11
+ end
12
+
13
+ ActiveRecord::Migration.create_table :records do |t|
14
+ t.string :input, null: false
15
+ t.string :id_type
16
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Benjamin Cavileer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: database_cleaner
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Maps string to other strings?
98
+ email:
99
+ - bcavileer@holmanauto.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - .rspec
106
+ - .travis.yml
107
+ - Gemfile
108
+ - LICENSE.txt
109
+ - README.md
110
+ - Rakefile
111
+ - hid.gemspec
112
+ - lib/hid.rb
113
+ - lib/hid/configuration.rb
114
+ - lib/hid/persistence/active_record/identifier.rb
115
+ - lib/hid/persistence/active_record/recorder.rb
116
+ - lib/hid/persistence/memory/identifier.rb
117
+ - lib/hid/persistence/memory/recorder.rb
118
+ - lib/hid/version.rb
119
+ - spec/active_record_spec.rb
120
+ - spec/examples/identifier.rb
121
+ - spec/examples/recorder.rb
122
+ - spec/memory_spec.rb
123
+ - spec/spec_helper.rb
124
+ - spec/support/active_record.rb
125
+ homepage: ''
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ! '>='
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ! '>='
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.2.1
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Identity Mapping
149
+ test_files:
150
+ - spec/active_record_spec.rb
151
+ - spec/examples/identifier.rb
152
+ - spec/examples/recorder.rb
153
+ - spec/memory_spec.rb
154
+ - spec/spec_helper.rb
155
+ - spec/support/active_record.rb