ar_lookups 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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd850a54a805b4137aa6719a8fbc85aba98c8421
4
+ data.tar.gz: 590ef6a50e3fea20afa2eaf0ef767f90fd89b17c
5
+ SHA512:
6
+ metadata.gz: 079f96de9b4f4858bf7342391d549e87748ffe6252b7342c5b18bfa076a820ea0f2d6d4055389880c2fa3eccd0ca49931381b50d6e16493a890cc1dce23dc173
7
+ data.tar.gz: 161e657dabaf63874c03ea106cf3a251e3676d5b19cfe548263e8cc41f6f22208469dbab3d23b4a1e166179230c0489a66d190d9446e80d65ebe80518840fac6
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ar_lookups.gemspec
4
+ gemspec
5
+
6
+ group :development, :test do
7
+ gem 'guard-rspec'
8
+ end
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard :rspec, cmd: 'rspec -d .' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Stan Bondi
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,29 @@
1
+ # ArLookups
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ar_lookups'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ar_lookups
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/ar_lookups/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ar_lookups/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ar_lookups"
8
+ spec.version = ArLookups::VERSION
9
+ spec.authors = ["Stan Bondi"]
10
+ spec.email = ["stan@fixate.it"]
11
+ spec.summary = %q{Lookup tables using activerecord and array fields}
12
+ spec.homepage = "https://github.com/fixate/ar_ar_lookups"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'rails', '~> 4.0'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec-rails"
25
+ end
data/lib/ar_lookups.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'active_record'
2
+
3
+ require 'ar_lookups/version'
4
+ require 'ar_lookups/lookup_table_definition'
5
+ require 'ar_lookups/base_ext'
6
+ require 'ar_lookups/railtie' if defined?(Rails)
@@ -0,0 +1,47 @@
1
+ module ArLookups
2
+ module BaseExt
3
+ def ar_lookups
4
+ @ar_lookups ||= superclass.respond_to?(:ar_lookups) ? superclass.ar_lookups.dup : {}
5
+ end
6
+
7
+ def lookup(column, *args)
8
+ options = args.extract_options!
9
+ lookup_model = args.first
10
+
11
+ options.reverse_merge!(key_column: :key, name_column: :name)
12
+
13
+ # Directly override accessors and super to the module below
14
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
15
+ def #{column}; super; end
16
+ def #{column}=(value); super; end
17
+ RUBY
18
+
19
+ ar_lookups[column] = lookup_model || implicit_lookup_class(column)
20
+
21
+ # Mixin this Module so that we can use super in these functions
22
+ mod = Module.new
23
+ include mod
24
+ mod.class_eval <<-RUBY, __FILE__, __LINE__ + 1
25
+ def #{column}
26
+ keys = read_attribute(:#{column})
27
+ return {} if keys.blank?
28
+ Hash[self.class.ar_lookups[:#{column}].where('#{options[:key_column]} IN (?)', keys).pluck(:#{options[:key_column]}, :#{options[:name_column]})]
29
+ end
30
+
31
+ def #{column}_ids
32
+ read_attribute(:#{column})
33
+ end
34
+
35
+ def #{column}=(val)
36
+ write_attribute(:#{column}, val)
37
+ end
38
+ RUBY
39
+ end
40
+
41
+ private
42
+
43
+ def implicit_lookup_class(column)
44
+ column.to_s.camelize.singularize.constantize
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module ArLookups
2
+ module LookupTableDefinition
3
+ def lookup(column, options = {})
4
+ send(:integer, column, options.merge(array: true, default: []))
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ require "rails"
2
+
3
+ module ArLookups
4
+ class Railtie < Rails::Railtie # :nodoc:
5
+ config.eager_load_namespaces << ArLookups
6
+
7
+ initializer "ar_lookups.initialize" do |app|
8
+ ActiveSupport.on_load(:active_record) do
9
+ # Extend migration table definition
10
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include,
11
+ ArLookups::LookupTableDefinition
12
+
13
+ # Extend AR base
14
+ ActiveRecord::Base.send :extend,
15
+ ArLookups::BaseExt
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,3 @@
1
+ module ArLookups
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ class FooBar; end
4
+
5
+ describe ArLookups::BaseExt do
6
+ let(:lookup_relation) { mock_model('Dummy', pluck: []) }
7
+ let(:lookup) do
8
+ mock_model('Lookup', where: lookup_relation)
9
+ end
10
+
11
+ let(:client_class) do
12
+ @class = Class.new
13
+ @class.class_eval do
14
+ def write_attribute(column, val)
15
+ unless respond_to?(column)
16
+ class_eval <<-RUBY, __FILE__, __LINE__+1
17
+ attr_accessor :#{column}
18
+ RUBY
19
+ end
20
+
21
+ instance_variable_set("@#{column}".to_sym, val)
22
+ end
23
+
24
+ def read_attribute(column)
25
+ instance_variable_get("@#{column}".to_sym)
26
+ end
27
+ end
28
+
29
+ @class.send(:extend, ArLookups::BaseExt)
30
+ @class.lookup :foo, lookup
31
+ @class
32
+ end
33
+
34
+ subject { client_class.new }
35
+
36
+ it 'fetches values with keys' do
37
+ expect(lookup).to receive(:where).with('key IN (?)', [1])
38
+ expect(lookup_relation).to receive(:pluck).with(:key, :name)
39
+
40
+ subject.foo = [1]
41
+ subject.foo
42
+ end
43
+
44
+ it 'fetches values with different key name' do
45
+ client_class.lookup :bar, lookup, key_column: :id, name_column: :place
46
+ expect(lookup).to receive(:where).with('id IN (?)', [1])
47
+ expect(lookup_relation).to receive(:pluck).with(:id, :place)
48
+
49
+ subject.bar = [1]
50
+ subject.bar
51
+ end
52
+
53
+ it 'returns ids from lookup' do
54
+ subject.foo = [1,2]
55
+ expect(subject.foo_ids).to eq([1,2])
56
+ end
57
+
58
+ it 'returns an empty hash if ar_lookups empty' do
59
+ expect(subject.foo).to eq({})
60
+ end
61
+
62
+ it 'returns a hash' do
63
+ subject.foo = [1,2]
64
+ expect(subject.foo).to be_kind_of(Hash)
65
+ end
66
+
67
+ it 'implicitly gets the model name from the column' do
68
+ client_class.class_eval do
69
+ lookup :foo_bars
70
+ end
71
+
72
+ expect(client_class.ar_lookups[:foo_bars]).to be(FooBar)
73
+ end
74
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe ArLookups::LookupTableDefinition do
4
+ subject do
5
+ class TableDefinition; include ArLookups::LookupTableDefinition; end.new
6
+ end
7
+
8
+ it 'creates an integer array column with default' do
9
+ expect(subject).to receive(:integer).with(:foo, {array: true, default: []})
10
+ subject.lookup :foo
11
+ end
12
+
13
+ it 'merges additional options in preserving array:true' do
14
+ expect(subject).to receive(:integer).with(:foo, {test: 'foobar', array: true, default: []})
15
+ subject.lookup :foo, test: 'foobar'
16
+ end
17
+
18
+ it 'uses default if given' do
19
+ expect(subject).to receive(:integer).with(:foo, {default: [1, 2], array: true, default: []})
20
+ subject.lookup :foo, default: [1, 2]
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'rails/all'
9
+ require 'rspec'
10
+ require 'rspec/mocks'
11
+ require 'rspec/rails/mocks'
12
+ require 'rspec/autorun'
13
+
14
+ require 'ar_lookups'
15
+
16
+ RSpec.configure do |config|
17
+ config.treat_symbols_as_metadata_keys_with_true_values = true
18
+ config.run_all_when_everything_filtered = true
19
+ config.filter_run :focus
20
+
21
+ # Run specs in random order to surface order dependencies. If you find an
22
+ # order dependency and want to debug it, you can fix the order by providing
23
+ # the seed, which is printed after each run.
24
+ # --seed 1234
25
+ config.order = 'random'
26
+ end
27
+
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ar_lookups
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stan Bondi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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: rspec-rails
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
+ description:
70
+ email:
71
+ - stan@fixate.it
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - Guardfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - ar_lookups.gemspec
84
+ - lib/ar_lookups.rb
85
+ - lib/ar_lookups/base_ext.rb
86
+ - lib/ar_lookups/lookup_table_definition.rb
87
+ - lib/ar_lookups/railtie.rb
88
+ - lib/ar_lookups/version.rb
89
+ - spec/base_ext_spec.rb
90
+ - spec/lookup_table_definition_spec.rb
91
+ - spec/spec_helper.rb
92
+ homepage: https://github.com/fixate/ar_ar_lookups
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.0
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Lookup tables using activerecord and array fields
116
+ test_files:
117
+ - spec/base_ext_spec.rb
118
+ - spec/lookup_table_definition_spec.rb
119
+ - spec/spec_helper.rb