public_attributes 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/lib/public_attributes.rb +77 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a03f830869d3d96336db10de1b0dcb843e659a7b
|
4
|
+
data.tar.gz: a77f0073157f8c258533b88f5d5d343d937d06fd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2659ed187c79cfa009b31ba62ccf019c46b2c917ed36c103346d6c1dc2471b05b7ef8592a0de4d4060aec2fabfaef935b9a15194d0fd479867316642e6cdfd32
|
7
|
+
data.tar.gz: 4f6bf6da9fcf0b7c366002f710379079e93694458cf66fb6f47531d744b219582981fc2dd5961616ce8bde4fc8e014861e413b2b1a3f7877647b28d53203cce0
|
@@ -0,0 +1,77 @@
|
|
1
|
+
#
|
2
|
+
# Provides a to_public instance method for filtering out sensative data.
|
3
|
+
#
|
4
|
+
# Public attributes are defined with the public_attributes class method, which
|
5
|
+
# whitelists attributes that are ok to send to the client, JSON endpoints, etc.
|
6
|
+
#
|
7
|
+
|
8
|
+
require 'json'
|
9
|
+
|
10
|
+
module PublicAttributes
|
11
|
+
@_public_attributes ||= {}
|
12
|
+
|
13
|
+
def self.included(klass)
|
14
|
+
@_public_attributes[klass.name] ||= []
|
15
|
+
klass.extend(ClassMethods)
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.add(class_name, attr_list)
|
19
|
+
@_public_attributes[class_name].push(*attr_list)
|
20
|
+
@_public_attributes[class_name].uniq!
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.for(klass)
|
24
|
+
self.instance_variable_get(:@_public_attributes)[klass.name]
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.reset
|
28
|
+
self.instance_variable_set(:@_public_attributes, {})
|
29
|
+
end
|
30
|
+
|
31
|
+
module ClassMethods
|
32
|
+
def public_attributes(*attr_list)
|
33
|
+
PublicAttributes.add(name, attr_list)
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_public
|
37
|
+
if defined?(ActiveRecord) && (self < ActiveRecord::Base)
|
38
|
+
where(nil).map do |instance|
|
39
|
+
instance.to_public
|
40
|
+
end
|
41
|
+
else
|
42
|
+
raise Error 'This method is only available for ActiveRecord classes. '\
|
43
|
+
'Please re-implement if you\'d like to call to_public on '\
|
44
|
+
'a custom collection.'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_public
|
50
|
+
hash = {}
|
51
|
+
all_public_attributes.each do |attr|
|
52
|
+
value = self.send(attr)
|
53
|
+
if value.respond_to? :to_public
|
54
|
+
hash[attr] = value.to_public
|
55
|
+
else
|
56
|
+
hash[attr] = value
|
57
|
+
end
|
58
|
+
end
|
59
|
+
return hash
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_json
|
63
|
+
JSON.generate self.to_public
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def all_public_attributes
|
69
|
+
attributes = PublicAttributes.for(self.class)
|
70
|
+
if defined?(ActiveRecord) && self.is_a?(ActiveRecord::Base)
|
71
|
+
attributes += [:created_at, :updated_at]
|
72
|
+
attributes.push(:errors) unless self.errors.keys.empty?
|
73
|
+
end
|
74
|
+
return attributes
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: public_attributes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nathan Hackley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-26 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rb-fsevent
|
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
|
+
description: Simple public vs private attribute filtering for Ruby objects.
|
56
|
+
email: nathanhackley@gmail.com
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- lib/public_attributes.rb
|
62
|
+
homepage: http://rubygems.org/gems/public_attributes
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.0.14.1
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Public Attributes
|
86
|
+
test_files: []
|