remote_db 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 54d0b659f91bd851c797b2378a5a58a44e727778
4
+ data.tar.gz: 77d11bfeb7c050e54642f3621f58c665d0b06fd3
5
+ SHA512:
6
+ metadata.gz: 0c964ced6b11c7ef08d8e6fed0fa535267778247eeb8274d24bbbe2c5543b62a56ec124a4bda02ebc15cd8ff78d800c9a19c737090ff34535929416611fc8b8a
7
+ data.tar.gz: cdc286bb4cd33772b352dd223818e3514721a3654a68067ad17eacd6a305dd39097555702ad3b31c0986cab42017f224dfbd88669051f72bb47d9efc57fd84ad
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ # Ignore Mac's hidden File
2
+ .DS_Store
3
+
4
+ # Ignore redis file
5
+ dump.rdb
6
+
7
+ # Ignore RubyMine's Metadata
8
+ /.idea
9
+ /.project
10
+ /pmip
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in remote_db.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,7 @@
1
+ ## Remote DB
2
+
3
+ There are cases where you want to easily consume database objects from a specific database. This gem
4
+ makes it very easy to expose a specific set of tables/columns from a remote database, and allow them to
5
+ be consumed in *read only* form within a gem.
6
+
7
+
@@ -0,0 +1,50 @@
1
+ module RemoteDb
2
+ module Concerns
3
+ module Configurable
4
+ extend ActiveSupport::Concern
5
+
6
+ module ClassMethods
7
+ def configure(config_hash = nil)
8
+ if config_hash
9
+ config_hash.each do |k,v|
10
+ setter = "#{k}="
11
+ if configuration.respond_to?(setter )
12
+ configuration.send(setter , v)
13
+ end
14
+ end
15
+ end
16
+
17
+ yield(configuration) if block_given?
18
+ end
19
+
20
+ def configuration
21
+ @configuration ||= RemoteDb::Configuration.new
22
+ end
23
+
24
+ def load_models!
25
+ define_base_record
26
+ send(:require_models) if respond_to?(:require_models)
27
+ end
28
+
29
+ private
30
+
31
+ def define_base_record
32
+ connection_spec = configuration.connection_spec
33
+ klass = Class.new(ActiveRecord::Base) do
34
+ self.abstract_class = true
35
+
36
+ # Connect via custom db settings
37
+ establish_connection(connection_spec)
38
+
39
+ # Force model to be ready-only
40
+ include Concerns::ReadOnlyModel
41
+
42
+ # Scope which columns are exposed
43
+ include Concerns::RestrictedColumns
44
+ end
45
+ self.const_set 'BaseRecord', klass
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,41 @@
1
+ module RemoteDb
2
+ module Concerns
3
+ module ReadOnlyModel
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ attr_readonly(*column_names) unless abstract_class?
8
+ end
9
+
10
+ module ClassMethods
11
+ def delete(id_or_array)
12
+ raise ActiveRecord::ReadOnlyRecord
13
+ end
14
+
15
+ def delete_all(conditions = nil)
16
+ raise ActiveRecord::ReadOnlyRecord
17
+ end
18
+
19
+ def update_all(conditions = nil)
20
+ raise ActiveRecord::ReadOnlyRecord
21
+ end
22
+ end
23
+
24
+ def readonly?
25
+ true
26
+ end
27
+
28
+ def destroy
29
+ raise ActiveRecord::ReadOnlyRecord
30
+ end
31
+
32
+ def delete
33
+ raise ActiveRecord::ReadOnlyRecord
34
+ end
35
+
36
+ def update_column
37
+ raise ActiveRecord::ReadOnlyRecord
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,52 @@
1
+ module RemoteDb
2
+ module Concerns
3
+ module RestrictedColumns
4
+ extend ActiveSupport::Concern
5
+
6
+ class ForbiddenColumnException < Exception; end
7
+
8
+ included do
9
+ def read_attribute_with_safety(name)
10
+ original_db_column = self.class.original_columns.include?(name.to_sym)
11
+ visible_db_column = self.class.visible_columns.include? (name.to_sym)
12
+
13
+ if (original_db_column && visible_db_column) || !original_db_column
14
+ read_attribute_without_safety(name)
15
+ else
16
+ raise ForbiddenColumnException, "Column #{name} is not allowed for access."
17
+ end
18
+ end
19
+ alias_method_chain :read_attribute, :safety
20
+ end
21
+
22
+ module ClassMethods
23
+
24
+ def visible_columns
25
+ @visible_columns ||= []
26
+ end
27
+
28
+ def original_columns
29
+ @original_columns ||= []
30
+ end
31
+
32
+ # Note: This is a hack that relies on ActiveRecord's internals. Most of the
33
+ # logic is originally from: ActiveRecord::ModelSchema#reset_column_information`
34
+
35
+ def table_columns=(visible_columns)
36
+ unless abstract_class?
37
+ @visible_columns = visible_columns
38
+ @original_columns = columns.map(&:name).map(&:to_sym)
39
+ @columns.reject! do |column|
40
+ !visible_columns.include?(column.name.to_sym)
41
+ end
42
+
43
+ @column_names = @content_columns = @column_defaults = @columns_hash = nil
44
+ @dynamic_methods_hash = nil
45
+ @inheritance_column = nil unless defined?(@explicit_inheritance_column) && @explicit_inheritance_column
46
+ @arel_engine = @relation = nil
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,21 @@
1
+ module RemoteDb
2
+ class Configuration
3
+ attr_accessor :adapter
4
+ attr_accessor :pool
5
+ attr_accessor :host
6
+ attr_accessor :username
7
+ attr_accessor :password
8
+ attr_accessor :database
9
+
10
+ def connection_spec
11
+ {
12
+ adapter: adapter ,
13
+ pool: pool,
14
+ host: host,
15
+ username: username,
16
+ password: password,
17
+ database: database
18
+ }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module RemoteDb
2
+ VERSION = '0.0.1'
3
+ end
data/lib/remote_db.rb ADDED
@@ -0,0 +1,8 @@
1
+ require 'active_support/concern'
2
+ require 'active_record'
3
+
4
+ require_relative 'remote_db/configuration'
5
+ require_relative 'remote_db/version'
6
+ require_relative 'remote_db/concerns/configurable'
7
+ require_relative 'remote_db/concerns/read_only_model'
8
+ require_relative 'remote_db/concerns/restricted_columns'
data/remote_db.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'remote_db/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'remote_db'
7
+ spec.version = RemoteDb::VERSION
8
+ spec.summary = 'Easily create a gem for consuming remote database tables/columns.'
9
+ spec.description = ''
10
+ spec.files = `git ls-files`.split($/)
11
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
12
+ spec.require_paths = ['lib']
13
+ spec.authors = ['Omar Skalli']
14
+ spec.email = ['omar@zenpayroll.com']
15
+ spec.homepage = 'https://github.com/ZenPayroll/remote_db'
16
+
17
+ spec.add_runtime_dependency 'activesupport', '>= 3.0'
18
+ spec.add_runtime_dependency 'activerecord' , '>= 3.0'
19
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: remote_db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Omar Skalli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activerecord
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '3.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '3.0'
41
+ description: ''
42
+ email:
43
+ - omar@zenpayroll.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - CHANGELOG.md
50
+ - Gemfile
51
+ - README.md
52
+ - lib/remote_db.rb
53
+ - lib/remote_db/concerns/configurable.rb
54
+ - lib/remote_db/concerns/read_only_model.rb
55
+ - lib/remote_db/concerns/restricted_columns.rb
56
+ - lib/remote_db/configuration.rb
57
+ - lib/remote_db/version.rb
58
+ - remote_db.gemspec
59
+ homepage: https://github.com/ZenPayroll/remote_db
60
+ licenses: []
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.4
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Easily create a gem for consuming remote database tables/columns.
82
+ test_files: []
83
+ has_rdoc: