mappd 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
+ SHA256:
3
+ metadata.gz: 473cb92d742f731112c8443bacddf1e2dc92592940133fa9eaaa7de99a7754ca
4
+ data.tar.gz: c995d4dbef38bae61681a966c6ffacf057f5f09705d497bbb97423e6881e63a2
5
+ SHA512:
6
+ metadata.gz: c2abed12e72d3894bb419fd10f814426d4d029c6d2e28ef78dfad2ce2ab6d267865cd3b3dc31e57ac6e3291bf87df61b7c725d35dbc05600ccde96a23376acb3
7
+ data.tar.gz: 5e6dec9fbafabb3fab482cc5a856289718625acac1022c9bdad9aaa9658b9314a034c6623e2c6870d13ca758ee7d2ff8f4ec3f6280415cbbe93bd9e3bba90076
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ tmp/
2
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,14 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ Exclude:
4
+ - 'Guardfile'
5
+ Style/Documentation:
6
+ Enabled: false
7
+ Style/FrozenStringLiteralComment:
8
+ Enabled: false
9
+ Metrics/ModuleLength:
10
+ Exclude:
11
+ - "**/*_spec.rb"
12
+ Metrics/BlockLength:
13
+ Exclude:
14
+ - "**/*_spec.rb"
data/Dockerfile ADDED
@@ -0,0 +1,31 @@
1
+ FROM ruby:2.5-slim
2
+ MAINTAINER dan@paz.am
3
+ ENV REFRESHED_AT 2018-05-14
4
+
5
+ # Install packages
6
+ RUN apt-get update && apt-get install -y --no-install-recommends \
7
+ build-essential \
8
+ git \
9
+ ruby-dev \
10
+ libgdbm-dev \
11
+ libncurses5-dev \
12
+ libffi-dev \
13
+ libyaml-dev \
14
+ libreadline-dev \
15
+ sqlite3 \
16
+ libsqlite3-dev \
17
+ curl \
18
+ libpq-dev \
19
+ default-libmysqlclient-dev \
20
+ && rm -rf /var/lib/apt/lists/*
21
+
22
+ # Setup app location
23
+ RUN mkdir -p /app
24
+ WORKDIR /app
25
+
26
+ # Install gems
27
+ ADD mappd.gemspec /app/mappd.gemspec
28
+ ADD Gemfile /app/Gemfile
29
+ ADD Gemfile.lock /app/Gemfile.lock
30
+ ADD lib/ /app/lib
31
+ RUN bundle install --jobs 4
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'activerecord', '>= 5.2.0'
4
+ gem 'rake'
5
+ gem 'sqlite3'
6
+
7
+ group :development do
8
+ gem 'byebug'
9
+ gem 'guard-rspec', require: false
10
+ gem 'guard-rubocop', require: false
11
+ gem 'rubocop', require: false
12
+ end
13
+
14
+ group :test do
15
+ gem 'rspec'
16
+ gem 'shoulda-matchers'
17
+ end
18
+
19
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,119 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mappd (0.0.1.beta)
5
+ activerecord (>= 5.2.0)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.0)
11
+ activesupport (= 5.2.0)
12
+ activerecord (5.2.0)
13
+ activemodel (= 5.2.0)
14
+ activesupport (= 5.2.0)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.0)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ ast (2.4.0)
23
+ byebug (10.0.2)
24
+ coderay (1.1.2)
25
+ concurrent-ruby (1.0.5)
26
+ diff-lcs (1.3)
27
+ ffi (1.9.23)
28
+ formatador (0.2.5)
29
+ guard (2.14.2)
30
+ formatador (>= 0.2.4)
31
+ listen (>= 2.7, < 4.0)
32
+ lumberjack (>= 1.0.12, < 2.0)
33
+ nenv (~> 0.1)
34
+ notiffany (~> 0.0)
35
+ pry (>= 0.9.12)
36
+ shellany (~> 0.0)
37
+ thor (>= 0.18.1)
38
+ guard-compat (1.2.1)
39
+ guard-rspec (4.7.3)
40
+ guard (~> 2.1)
41
+ guard-compat (~> 1.1)
42
+ rspec (>= 2.99.0, < 4.0)
43
+ guard-rubocop (1.3.0)
44
+ guard (~> 2.0)
45
+ rubocop (~> 0.20)
46
+ i18n (1.0.1)
47
+ concurrent-ruby (~> 1.0)
48
+ listen (3.1.5)
49
+ rb-fsevent (~> 0.9, >= 0.9.4)
50
+ rb-inotify (~> 0.9, >= 0.9.7)
51
+ ruby_dep (~> 1.2)
52
+ lumberjack (1.0.13)
53
+ method_source (0.9.0)
54
+ minitest (5.11.3)
55
+ nenv (0.3.0)
56
+ notiffany (0.1.1)
57
+ nenv (~> 0.1)
58
+ shellany (~> 0.0)
59
+ parallel (1.12.1)
60
+ parser (2.5.1.0)
61
+ ast (~> 2.4.0)
62
+ powerpack (0.1.1)
63
+ pry (0.11.3)
64
+ coderay (~> 1.1.0)
65
+ method_source (~> 0.9.0)
66
+ rainbow (3.0.0)
67
+ rake (12.3.1)
68
+ rb-fsevent (0.10.3)
69
+ rb-inotify (0.9.10)
70
+ ffi (>= 0.5.0, < 2)
71
+ rspec (3.7.0)
72
+ rspec-core (~> 3.7.0)
73
+ rspec-expectations (~> 3.7.0)
74
+ rspec-mocks (~> 3.7.0)
75
+ rspec-core (3.7.1)
76
+ rspec-support (~> 3.7.0)
77
+ rspec-expectations (3.7.0)
78
+ diff-lcs (>= 1.2.0, < 2.0)
79
+ rspec-support (~> 3.7.0)
80
+ rspec-mocks (3.7.0)
81
+ diff-lcs (>= 1.2.0, < 2.0)
82
+ rspec-support (~> 3.7.0)
83
+ rspec-support (3.7.1)
84
+ rubocop (0.55.0)
85
+ parallel (~> 1.10)
86
+ parser (>= 2.5)
87
+ powerpack (~> 0.1)
88
+ rainbow (>= 2.2.2, < 4.0)
89
+ ruby-progressbar (~> 1.7)
90
+ unicode-display_width (~> 1.0, >= 1.0.1)
91
+ ruby-progressbar (1.9.0)
92
+ ruby_dep (1.5.0)
93
+ shellany (0.0.1)
94
+ shoulda-matchers (3.1.2)
95
+ activesupport (>= 4.0.0)
96
+ sqlite3 (1.3.13)
97
+ thor (0.20.0)
98
+ thread_safe (0.3.6)
99
+ tzinfo (1.2.5)
100
+ thread_safe (~> 0.1)
101
+ unicode-display_width (1.3.2)
102
+
103
+ PLATFORMS
104
+ ruby
105
+
106
+ DEPENDENCIES
107
+ activerecord (>= 5.2.0)
108
+ byebug
109
+ guard-rspec
110
+ guard-rubocop
111
+ mappd!
112
+ rake
113
+ rspec
114
+ rubocop
115
+ shoulda-matchers
116
+ sqlite3
117
+
118
+ BUNDLED WITH
119
+ 1.16.1
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard :rubocop do
2
+ watch(%r{.+\.rb$})
3
+ watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
4
+ end
5
+
6
+ guard :rspec, cmd: 'rspec' do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
data/Makefile ADDED
@@ -0,0 +1,10 @@
1
+ build:
2
+ docker-compose build
3
+ guard:
4
+ docker-compose run ruby guard
5
+ rspec:
6
+ docker-compose run ruby rspec
7
+ lint:
8
+ docker-compose run ruby rubocop
9
+ release:
10
+
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ # README
2
+
3
+ Mappd is a replacement gem for [mini_record](https://github.com/DAddYE/mini_record). Mini record is great and I have been using it for a number of years. Unfortunately the mini_record report has not been updated in some time. With recent rails upgrades mini_record is not working well.
4
+
5
+ Mappd is designed to work with ActiveRecord 5.2 >.
6
+
7
+ It's simple to use.
8
+
9
+ ```ruby
10
+ class Person < ActiveRecord::Base
11
+ field :name, :string, null: false, default: ''
12
+ field :age, :integer
13
+ field :score, :decimal, precision: 10, scale: 10, null: true
14
+
15
+ # Rename a field - the old field and rename could be removed after
16
+ # migrate! is called
17
+ field :external_id, :string, length: 10
18
+ rename :external_id, :e_id
19
+ field :e_id, :string, length: 10
20
+
21
+ # Hooks into belongs_to and creates a country_id column
22
+ # with an index
23
+ belongs_to :country
24
+
25
+ # Creates a join table people_roles
26
+ has_and_belongs_to_many :roles
27
+
28
+ # Creates an index
29
+ index :e_id
30
+
31
+ # Creates created_at and updated_at columns
32
+ timestamps
33
+ end
34
+
35
+ class Country < ActiveRecord::Base
36
+ field :name, :string
37
+ end
38
+
39
+ class Role < ActiveRecord::Base
40
+ field :name
41
+ has_and_belongs_to_many :people
42
+ end
43
+
44
+ Person.migrate!
45
+ Country.migrate!
46
+ Role.migrate!
47
+ ```
48
+
49
+ Hey presto your database has been created with the correct schema.
50
+
51
+ # Types & Options - Field
52
+
53
+ ```field :name, :type, options = {}```
54
+
55
+ The type parameter is normally one of the migrations native types, which is one of the following:
56
+
57
+ ```:primary_key```
58
+ ```:string```
59
+ ```:text```
60
+ ```:integer```
61
+ ```:bigint```
62
+ ```:float```
63
+ ```:decimal```
64
+ ```:numeric```
65
+ ```:datetime```
66
+ ```:time```
67
+ ```:date```
68
+ ```:binary```
69
+ ```:boolean```
70
+
71
+ You may use a type not in this list as long as it is supported by your database (for example, “polygon” in MySQL), but this will not be database agnostic and should usually be avoided.
72
+
73
+ Available options are (none of these exists by default):
74
+
75
+ ```:limit``` - Requests a maximum column length. This is the number of characters for a :string column and number of bytes for :text, :binary and :integer columns. This option is ignored by some backends.
76
+
77
+ ```:default``` - The column's default value. Use nil for NULL.
78
+
79
+ ```:null``` - Allows or disallows NULL values in the column.
80
+
81
+ ```:precision``` - Specifies the precision for the :decimal and :numeric columns.
82
+
83
+ ```:scale``` - Specifies the scale for the :decimal and :numeric columns.
84
+
85
+ ```:comment``` - Specifies the comment for the column. This option is ignored by some backends.
86
+
87
+
88
+
@@ -0,0 +1,9 @@
1
+ version: '2'
2
+ services:
3
+ ruby:
4
+ build:
5
+ context: .
6
+ dockerfile: Dockerfile
7
+ volumes:
8
+ - .:/app
9
+ command: ''
data/lib/mappd.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'rubygems' unless defined?(Gem)
2
+ require 'active_record'
3
+ require 'mappd/mappd'
4
+
5
+ ActiveRecord::Base.send(:include, Mappd)
@@ -0,0 +1,80 @@
1
+ module Mappd
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ def index(name, options = {})
8
+ commands << [:add_index, [table_name, name, options]]
9
+ end
10
+
11
+ def rename(from, to)
12
+ commands << [:rename_column, [table_name, from, to]]
13
+ end
14
+
15
+ def field(name, type = :string, options = {})
16
+ commands << [:add_column, [table_name, name, type, options]]
17
+ end
18
+
19
+ def timestamps
20
+ commands << [:add_timestamps, [table_name, null: true]]
21
+ end
22
+
23
+ def associations!
24
+ reflect_on_all_associations.each do |association|
25
+ case association.macro
26
+ when :belongs_to
27
+ commands << [:add_reference, [table_name, association.name, {}]]
28
+ when :has_and_belongs_to_many
29
+ commands << [:create_join_table,
30
+ [table_name.to_sym, association.name]]
31
+ end
32
+ end
33
+ end
34
+
35
+ def migrate!
36
+ create_table!
37
+ associations!
38
+ execute_commands!
39
+ end
40
+
41
+ def commands
42
+ @commands ||= []
43
+ end
44
+
45
+ private
46
+
47
+ def create_table!
48
+ connection.create_table(table_name) unless
49
+ connection.table_exists?(table_name)
50
+ end
51
+
52
+ def execute_commands!
53
+ commands.each do |command|
54
+ if column_exists?(command)
55
+ connection.send(:change_column, *command[1])
56
+ else
57
+ connection.send(command[0], *command[1])
58
+ end
59
+ rescue ActiveRecord::StatementInvalid => e
60
+ warn(e)
61
+ end
62
+ reset!
63
+ end
64
+
65
+ def reset!
66
+ reset_column_information
67
+ @commands = []
68
+ end
69
+
70
+ def column_exists?(command)
71
+ command[0] == :add_column &&
72
+ connection.column_exists?(table_name, command[1][1])
73
+ end
74
+
75
+ def warn(message)
76
+ @logger ||= Logger.new(nil)
77
+ @logger.warn(message)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Mappd
2
+ VERSION = '0.0.1'.freeze
3
+ end
data/mappd.gemspec ADDED
@@ -0,0 +1,29 @@
1
+
2
+ $LOAD_PATH.push File.expand_path('lib', __dir__)
3
+ require 'mappd/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'mappd'
7
+ s.version = Mappd::VERSION
8
+ s.authors = ['Dan Watson']
9
+ s.email = ['dan@paz.am']
10
+ s.homepage = 'https://github.com/dan-watson/mappd'
11
+ s.summary = 'Mappd is a micro gem that allow you to write schema
12
+ inside your model as you can do in DataMapper.'
13
+ s.description = 'With it you can add the ability to create columns
14
+ outside the default schema, directly in your model in a similar way t
15
+ at you just know in others projects like DataMapper or MongoMapper.
16
+ '.gsub(/^ {4}/, '')
17
+
18
+ s.rubyforge_project = 'mappd'
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`
23
+ .split("\n").map { |f| File.basename(f) }
24
+ s.require_paths = ['lib']
25
+
26
+ # specify any dependencies here; for example:
27
+ # s.add_development_dependency "rspec"
28
+ s.add_dependency 'activerecord', '>=5.2.0'
29
+ end
data/spec/boot.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'rubygems' unless defined?(Gem)
2
+ require 'bundler/setup'
3
+ require 'mappd'
4
+
5
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
6
+ database: ':memory:')
@@ -0,0 +1,39 @@
1
+ require 'byebug'
2
+ require File.expand_path('boot.rb', __dir__)
3
+ require File.expand_path('spec_helper.rb', __dir__)
4
+ require File.expand_path('models.rb', __dir__)
5
+
6
+ context '#create table' do
7
+ it 'is nil when a table already exists' do
8
+ expect(Person.send(:create_table!)).to be_nil
9
+ end
10
+ end
11
+
12
+ RSpec.describe Person, type: :model do
13
+ it { should have_db_column(:name).of_type(:string) }
14
+ it { should have_db_column(:age).of_type(:integer) }
15
+ it {
16
+ should have_db_column(:score).of_type(:decimal)
17
+ .with_options(precision: 10,
18
+ scale: 10,
19
+ null: true)
20
+ }
21
+ it { should_not have_db_column(:external_id) }
22
+ it {
23
+ should have_db_column(:e_id).of_type(:string)
24
+ .with_options(length: 10)
25
+ }
26
+
27
+ it { should have_db_column(:country_id).of_type(:integer) }
28
+ it { should have_db_index(:country_id) }
29
+ it { should have_db_index(:e_id) }
30
+
31
+ context '#change column' do
32
+ before(:all) do
33
+ Person.field(:age, :string)
34
+ Person.migrate!
35
+ end
36
+
37
+ it { should have_db_column(:age).of_type(:string) }
38
+ end
39
+ end
data/spec/models.rb ADDED
@@ -0,0 +1,31 @@
1
+ require 'active_record'
2
+
3
+ class Person < ActiveRecord::Base
4
+ field :name, :string, null: false, default: ''
5
+ field :age, :integer
6
+ field :score, :decimal, precision: 10, scale: 10, null: true
7
+
8
+ field :external_id, :string, length: 10
9
+ rename :external_id, :e_id
10
+ field :e_id, :string, length: 10
11
+
12
+ belongs_to :country
13
+ has_and_belongs_to_many :roles
14
+
15
+ index :e_id
16
+
17
+ timestamps
18
+ end
19
+
20
+ class Country < ActiveRecord::Base
21
+ field :name, :string
22
+ end
23
+
24
+ class Role < ActiveRecord::Base
25
+ field :name
26
+ has_and_belongs_to_many :people
27
+ end
28
+
29
+ Person.migrate!
30
+ Country.migrate!
31
+ Role.migrate!
@@ -0,0 +1,23 @@
1
+ require 'shoulda-matchers'
2
+ require File.expand_path('boot.rb', __dir__)
3
+ require File.expand_path('models.rb', __dir__)
4
+
5
+ RSpec.configure do |config|
6
+ config.expect_with :rspec do |expectations|
7
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
8
+ end
9
+
10
+ config.mock_with :rspec do |mocks|
11
+ mocks.verify_partial_doubles = true
12
+ end
13
+
14
+ config.shared_context_metadata_behavior = :apply_to_host_groups
15
+ end
16
+
17
+ Shoulda::Matchers.configure do |config|
18
+ config.integrate do |with|
19
+ with.test_framework :rspec
20
+ with.library :active_record
21
+ with.library :active_model
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mappd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Watson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.0
27
+ description: "With it you can add the ability to create columns\noutside the default
28
+ schema, directly in your model in a similar way t\nat you just know in others projects
29
+ like DataMapper or MongoMapper.\n "
30
+ email:
31
+ - dan@paz.am
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".gitignore"
37
+ - ".rspec"
38
+ - ".rubocop.yml"
39
+ - Dockerfile
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - Guardfile
43
+ - Makefile
44
+ - README.md
45
+ - docker-compose.yml
46
+ - lib/mappd.rb
47
+ - lib/mappd/mappd.rb
48
+ - lib/mappd/version.rb
49
+ - mappd.gemspec
50
+ - spec/boot.rb
51
+ - spec/mappd_spec.rb
52
+ - spec/models.rb
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/dan-watson/mappd
55
+ licenses: []
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project: mappd
73
+ rubygems_version: 2.7.6
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Mappd is a micro gem that allow you to write schema inside your model as
77
+ you can do in DataMapper.
78
+ test_files: []