private_field 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 +7 -0
- data/.gitignore +47 -0
- data/LICENSE +21 -0
- data/README.md +35 -0
- data/lib/private_field.rb +10 -0
- data/lib/private_field/privatizer.rb +32 -0
- data/lib/private_field/version.rb +3 -0
- data/private_field.gemspec +24 -0
- data/test/private_field_spec.rb +38 -0
- data/test/spec_helper.rb +5 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2f6a0be0db478c3b1906b2bea60ee85bc2cf4e40
|
4
|
+
data.tar.gz: d94ae78c64ac28638ef4bd909b6da456dc9cbb30
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 55d551512ff25dab2d8d488182f757d8dfb25f4fdf493121abe81cc7eebcb49e80790dcc78689924786d7b158663deff19bc36564f1453220d6b7819579383f2
|
7
|
+
data.tar.gz: 9e08cbc40f702e3c25559a520a91bb1639af18af3847c36696652e4d67c4a17c1766c624c6bcb462eca381378f56a4888c99a2f5ca09b4179d55d0ac67a1ad33
|
data/.gitignore
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
*.rbc
|
2
|
+
capybara-*.html
|
3
|
+
.rspec
|
4
|
+
/log
|
5
|
+
/tmp
|
6
|
+
/db/*.sqlite3
|
7
|
+
/db/*.sqlite3-journal
|
8
|
+
/public/system
|
9
|
+
/coverage/
|
10
|
+
/spec/tmp
|
11
|
+
*.orig
|
12
|
+
rerun.txt
|
13
|
+
pickle-email-*.html
|
14
|
+
|
15
|
+
# TODO Comment out this rule if you are OK with secrets being uploaded to the repo
|
16
|
+
config/initializers/secret_token.rb
|
17
|
+
|
18
|
+
# Only include if you have production secrets in this file, which is no longer a Rails default
|
19
|
+
# config/secrets.yml
|
20
|
+
|
21
|
+
# dotenv
|
22
|
+
# TODO Comment out this rule if environment variables can be committed
|
23
|
+
.env
|
24
|
+
|
25
|
+
## Environment normalization:
|
26
|
+
/.bundle
|
27
|
+
/vendor/bundle
|
28
|
+
|
29
|
+
# these should all be checked in to normalize the environment:
|
30
|
+
# Gemfile.lock, .ruby-version, .ruby-gemset
|
31
|
+
|
32
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
33
|
+
.rvmrc
|
34
|
+
|
35
|
+
# if using bower-rails ignore default bower_components path bower.json files
|
36
|
+
/vendor/assets/bower_components
|
37
|
+
*.bowerrc
|
38
|
+
bower.json
|
39
|
+
|
40
|
+
# Ignore pow environment settings
|
41
|
+
.powenv
|
42
|
+
|
43
|
+
# Ignore Byebug command history file.
|
44
|
+
.byebug_history
|
45
|
+
|
46
|
+
# IntelliJ crap
|
47
|
+
.idea/**
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Robert Ingrum
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# private_field
|
2
|
+
Make Rails fields private to avoid those lazy temptations.
|
3
|
+
|
4
|
+
## Background
|
5
|
+
It's easy to fall into the temptation of modifying model fields in random places (like your views),
|
6
|
+
but this just makes the world worse. In most cases, it's a better idea to define a method within
|
7
|
+
your model or a service class that's responsible for handling all of your data manipulation.
|
8
|
+
|
9
|
+
This gem enforces that rule by allowing for private field definitions.
|
10
|
+
|
11
|
+
Now you may be thinking, "That's already possible, just override the accessors." And you'd be right,
|
12
|
+
but I'm lazy and think that writing:
|
13
|
+
```ruby
|
14
|
+
private_field :id
|
15
|
+
```
|
16
|
+
is easier than writing:
|
17
|
+
```ruby
|
18
|
+
field :id
|
19
|
+
private attr_accessor :id
|
20
|
+
```
|
21
|
+
|
22
|
+
## Config
|
23
|
+
This is the best (or worst) part: there's no config because I wrote this for one specific use case and
|
24
|
+
don't feel like writing config stuff that I won't use. Feel free to submit a pr if it doesn't suit
|
25
|
+
your needs.
|
26
|
+
|
27
|
+
All this was written for Mongoid (I'm sorry), so it may take a bit of tweaking to make work for other
|
28
|
+
ORMs.
|
29
|
+
|
30
|
+
## How to
|
31
|
+
Just do `private_field :field_name` instead of `field :field_name`. If you're feeling feisty, you can
|
32
|
+
do `readable_field :field_name` to make the reader public and the writer private.
|
33
|
+
|
34
|
+
MAKE SURE THAT YOU DO `include Mongoid::Document` OR WHATEVER ORM YOU'RE USING THIS ISN'T A REPLACEMENT
|
35
|
+
FOR THAT CRAP.
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'private_field'
|
2
|
+
|
3
|
+
module PrivateField
|
4
|
+
module Privatizer
|
5
|
+
def private_field(name, options = {})
|
6
|
+
field name, options
|
7
|
+
override_accessors name
|
8
|
+
end
|
9
|
+
|
10
|
+
def readable_field(name, options = {})
|
11
|
+
field name, options
|
12
|
+
override_writers name
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def override_accessors(name)
|
18
|
+
override_readers name
|
19
|
+
override_writers name
|
20
|
+
end
|
21
|
+
|
22
|
+
def override_readers(name)
|
23
|
+
private :"#{name}"
|
24
|
+
private :"#{name}_before_type_cast"
|
25
|
+
private :"#{name}?"
|
26
|
+
end
|
27
|
+
|
28
|
+
def override_writers(name)
|
29
|
+
private :"#{name}="
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
|
+
require 'private_field/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'private_field'
|
8
|
+
spec.version = PrivateField::VERSION
|
9
|
+
spec.date = '2018-04-16'
|
10
|
+
spec.summary = 'Make Rails fields private to avoid those lazy temptations.'
|
11
|
+
spec.description = 'I\'m honestly not sure what the difference between '\
|
12
|
+
'a summary and description is but this one\'s longer.'
|
13
|
+
spec.authors = ['Robert Ingrum']
|
14
|
+
spec.email = 'robert@forustheliving.com'
|
15
|
+
spec.homepage = 'http://rubygems.org/gems/private_field'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files`.split($/)
|
19
|
+
|
20
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
22
|
+
spec.add_development_dependency 'mongoid', '~> 3.0'
|
23
|
+
spec.add_development_dependency 'pry', '~> 1.0'
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
|
2
|
+
require 'mongoid'
|
3
|
+
|
4
|
+
describe PrivateField do
|
5
|
+
class TestModel
|
6
|
+
include Mongoid::Document
|
7
|
+
include Mongoid::Timestamps
|
8
|
+
include PrivateField
|
9
|
+
|
10
|
+
field :public_field, type: Boolean
|
11
|
+
private_field :private_field, type: Boolean
|
12
|
+
readable_field :readable_field, type: Boolean
|
13
|
+
|
14
|
+
def test_readability(field_name)
|
15
|
+
method(field_name).call
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_writeability(field_name, value)
|
19
|
+
method("#{field_name}=").call(value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:target) { TestModel.new }
|
24
|
+
|
25
|
+
describe 'background' do
|
26
|
+
it 'should allow me to read the standard field' do
|
27
|
+
expect(target.field).to be_nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '.private_field' do
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
describe '.readable_field' do
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
data/test/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: private_field
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert Ingrum
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :development
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.15'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.15'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: mongoid
|
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: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
description: I'm honestly not sure what the difference between a summary and description
|
70
|
+
is but this one's longer.
|
71
|
+
email: robert@forustheliving.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- LICENSE
|
78
|
+
- README.md
|
79
|
+
- lib/private_field.rb
|
80
|
+
- lib/private_field/privatizer.rb
|
81
|
+
- lib/private_field/version.rb
|
82
|
+
- private_field.gemspec
|
83
|
+
- test/private_field_spec.rb
|
84
|
+
- test/spec_helper.rb
|
85
|
+
homepage: http://rubygems.org/gems/private_field
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.6.11
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Make Rails fields private to avoid those lazy temptations.
|
109
|
+
test_files: []
|