partitional 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1fd2f509c71ca5e46903b81b24ac78a5bae441130453c5b8dce4208a3a9e3b3a
4
+ data.tar.gz: b20b06d9749591270b536cd0121ccb642878d3cd40f76f812649681793a945bf
5
+ SHA512:
6
+ metadata.gz: c597de88ee4d116423b411d0c82460116bb2d69007d55d0e19cfcfe9d16299c9b5b336467b09a37818f5f06e48f04477a0fd8deea7082fb71427551d70116e51
7
+ data.tar.gz: d8c0369ac52fc5d8cb6a909929e2df2424fa8e58c636bc6ee1467ffd0016a1054bbdd3889f9fbcc9f41cec191a838af29481cc0269343ce724111c2b00aa5b46
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in partitional.gemspec
6
+ gemspec
7
+
8
+ gem 'coveralls', require: false
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sho Kusano
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,81 @@
1
+ # Partitional
2
+
3
+ [![Gem](https://img.shields.io/gem/v/partitional.svg)](https://rubygems.org/gems/partitional)
4
+ [![Build Status](https://travis-ci.org/rosylilly/partitional.svg?branch=master)](https://travis-ci.org/rosylilly/partitional)
5
+ [![Coverage Status](https://coveralls.io/repos/github/rosylilly/partitional/badge.svg?branch=master)](https://coveralls.io/github/rosylilly/partitional?branch=master)
6
+
7
+ Provides partial model to your Rails. Partial models are like a value object. You can add accessor, domain logics and validation to partial models.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'partitional'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ class ApplicationRecord
25
+ include Partitional # Add this line to `app/models/application_record.rb`
26
+ end
27
+ ```
28
+
29
+ ### Example: E-Mail partition
30
+
31
+ ```ruby
32
+ # app/models/user.rb
33
+ class User < ApplicationRecord
34
+ # `users` table has :email_local_part and :email_domain columns
35
+ parition :email, prefix: :email # or mapping: { local_part: :email_local_part, domain: :email_domain }
36
+ end
37
+
38
+ # app/models/company.rb
39
+ class Company < ApplicationRecord
40
+ # `companies` table has :contact_email_local_part, :contact_email_domain
41
+ # :payment_email_local_part and :payment_email_domain columns
42
+
43
+ partition :contact_email, class_name: 'Email', prefix: :contact_email
44
+ partition :payment_email, class_name: 'Email', prefix: :payment_email
45
+ end
46
+
47
+ # app/models/email.rb
48
+ class Email < Partitional::Model
49
+ attr_accessor :local_part, :domain
50
+
51
+ validates :local_part, format: /\A[a-zA-Z0-9_-]{1,255}\z/
52
+ validates :domain, format: /\A[a-zA-Z0-9]{2,}(?:\.[a-zA-Z0-9]{2,})+\z/
53
+
54
+ def to_s
55
+ "#{local_part}@#{domain}"
56
+ end
57
+ end
58
+
59
+ # in some code
60
+ user = User.find(1)
61
+ user.email.local_part = "test"
62
+ user.email.domain = "example.com"
63
+ user.valid? # => true
64
+
65
+ user.email.domain = "!!!!"
66
+ user.valid? # => false
67
+ ```
68
+
69
+ ## Development
70
+
71
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
72
+
73
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
74
+
75
+ ## Contributing
76
+
77
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rosylilly/partitional.
78
+
79
+ ## License
80
+
81
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'partitional'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,64 @@
1
+ require 'active_support/concern'
2
+ require 'partitional/version'
3
+
4
+ module Partitional
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def partials
9
+ @partials ||= []
10
+ end
11
+
12
+ def partial_options
13
+ @partial_options ||= {}
14
+ end
15
+
16
+ def partition(name, class_name: nil, mapping: {}, prefix: nil, validation: true)
17
+ name = name.to_s.to_sym
18
+ partials.push(name)
19
+
20
+ klass = (class_name || name).to_s.classify.constantize
21
+
22
+ klass.attributes.each do |attr|
23
+ mapping[attr] ||= "#{prefix ? "#{prefix}_" : ''}#{attr}"
24
+ end
25
+
26
+ partial_options[name] = { mapping: mapping }
27
+
28
+ define_partial_accessor(name, klass, mapping)
29
+ define_partial_validator(name, mapping) if validation
30
+ end
31
+
32
+ protected
33
+
34
+ def define_partial_accessor(name, klass, mapping)
35
+ define_method(name) do
36
+ instance_variable_get(:"@#{name}") ||
37
+ instance_variable_set(:"@#{name}", klass.new(record: self, mapping: mapping))
38
+ end
39
+
40
+ define_method(:"#{name}=") do |origin|
41
+ partial = origin.dup
42
+ partial.instance_variable_set(:@record, self)
43
+ partial.instance_variable_set(:@mapping, mapping)
44
+ instance_variable_set(:"@#{name}", partial)
45
+ klass.attributes.each do |attr|
46
+ partial.send(:"#{attr}=", origin.send(attr)) unless mapping[attr].to_s.include?('.')
47
+ end
48
+ end
49
+ end
50
+
51
+ def define_partial_validator(name, mapping)
52
+ validate do
53
+ partial = send(name)
54
+ partial.validate
55
+
56
+ partial.errors.each do |attr, message|
57
+ errors.add(mapping[attr], message)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ require 'partitional/model'
@@ -0,0 +1,75 @@
1
+ require 'active_model'
2
+
3
+ module Partitional
4
+ class Model
5
+ include ActiveModel::Model
6
+
7
+ attr_accessor :record, :mapping
8
+
9
+ def self.attributes
10
+ @attributes ||= []
11
+ end
12
+
13
+ def self.attr_define(*attrs)
14
+ attributes.concat(attrs.map(&:to_sym))
15
+ attributes.uniq!
16
+ end
17
+
18
+ def self.attr_reader(*attrs)
19
+ attr_define(*attrs)
20
+ attrs.each do |attr|
21
+ attr = attr.to_s.to_sym
22
+ define_method(attr) do
23
+ self[attr]
24
+ end
25
+ end
26
+ end
27
+
28
+ def self.attr_writer(*attrs)
29
+ attr_define(*attrs)
30
+ attrs.each do |attr|
31
+ attr = attr.to_s.to_sym
32
+ define_method(:"#{attr}=") do |val|
33
+ self[attr] = val
34
+ end
35
+ end
36
+ end
37
+
38
+ def self.attr_accessor(*attrs)
39
+ attr_reader(*attrs)
40
+ attr_writer(*attrs)
41
+ end
42
+
43
+ def [](attr)
44
+ attr = attr.to_s.to_sym
45
+ reader = (mapping || {}).fetch(attr) { attr }
46
+ record ? record_send(reader) : instance_variable_get(:"@#{attr}")
47
+ end
48
+
49
+ def []=(attr, val)
50
+ attr = attr.to_s.to_sym
51
+ writer = (mapping || {}).fetch(attr) { attr }
52
+ record ? record_send(:"#{writer}=", val) : instance_variable_set(:"@#{attr}", val)
53
+ end
54
+
55
+ def as_json
56
+ attrs = self.class.attributes.map do |key|
57
+ [key, send(key)]
58
+ end
59
+
60
+ Hash[attrs]
61
+ end
62
+
63
+ protected
64
+
65
+ def record_send(method, *args)
66
+ methods = method.to_s.split('.')
67
+ actual = methods.pop
68
+ rec = methods.inject(record) do |receiver, m|
69
+ receiver.send(m)
70
+ end
71
+
72
+ rec.send(actual, *args)
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module Partitional
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'partitional/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'partitional'
8
+ spec.version = Partitional::VERSION
9
+ spec.authors = ['Sho Kusano']
10
+ spec.email = ['rosylilly@aduca.org']
11
+
12
+ spec.summary = 'Provides partial model to your Rails'
13
+ spec.description = spec.summary
14
+ spec.homepage = 'https://github.com/rosylilly/partitional'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.16'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'simplecov'
28
+
29
+ spec.add_dependency 'activemodel'
30
+ spec.add_dependency 'activesupport'
31
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: partitional
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sho Kusano
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-25 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.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
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: activemodel
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
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: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Provides partial model to your Rails
98
+ email:
99
+ - rosylilly@aduca.org
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
+ - bin/console
112
+ - bin/setup
113
+ - lib/partitional.rb
114
+ - lib/partitional/model.rb
115
+ - lib/partitional/version.rb
116
+ - partitional.gemspec
117
+ homepage: https://github.com/rosylilly/partitional
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.7.6
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Provides partial model to your Rails
141
+ test_files: []