user_prefs 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc4765f05bbab5c99f1ed18b00a42f573179ae5c
4
+ data.tar.gz: 8d4fb0fb1ec6e8837a025c2921173a869befce98
5
+ SHA512:
6
+ metadata.gz: 206f57111bc14730b5ed6fdfee79aa1ec41304c44f8223ac69321d0c58fbd8a30e8e9817ab139ae4c9f70510409022d66612323a221d7cc29d12e95342901028
7
+ data.tar.gz: e9465626a7c93c437537d02bd1458c42267365101395d69f42d5dcf1095cb32acae2702910268783878495760bd16becec0fcad33a886700f123c25719912769
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,101 @@
1
+ Style/MultilineBlockChain:
2
+ Description: 'Avoid multi-line chains of blocks.'
3
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#single-line-blocks'
4
+ Enabled: false
5
+
6
+ Style/Documentation:
7
+ Description: 'Document classes and non-namespace modules.'
8
+ Enabled: false
9
+ Exclude:
10
+ - 'spec/**/*'
11
+ - 'test/**/*'
12
+
13
+ # Multi-line method chaining should be done with trailing dots.
14
+ Style/DotPosition:
15
+ EnforcedStyle: leading
16
+ SupportedStyles:
17
+ - leading
18
+ - trailing
19
+
20
+ Style/MutableConstant:
21
+ Description: 'Do not assign mutable objects to constants.'
22
+ Enabled: false
23
+
24
+ Metrics/AbcSize:
25
+ # The ABC size is a calculated magnitude, so this number can be a Fixnum or
26
+ # a Float.
27
+ Max: 20
28
+
29
+ Metrics/LineLength:
30
+ Max: 100
31
+
32
+ Style/BlockDelimiters:
33
+ EnforcedStyle: line_count_based
34
+ SupportedStyles:
35
+ # The `line_count_based` style enforces braces around single line blocks and
36
+ # do..end around multi-line blocks.
37
+ - line_count_based
38
+ # The `semantic` style enforces braces around functional blocks, where the
39
+ # primary purpose of the block is to return a value and do..end for
40
+ # procedural blocks, where the primary purpose of the block is its
41
+ # side-effects.
42
+ #
43
+ # This looks at the usage of a block's method to determine its type (e.g. is
44
+ # the result of a `map` assigned to a variable or passed to another
45
+ # method) but exceptions are permitted in the `ProceduralMethods`,
46
+ # `FunctionalMethods` and `IgnoredMethods` sections below.
47
+ - semantic
48
+ # The `braces_for_chaining` style enforces braces around single line blocks
49
+ # and do..end around multi-line blocks, except for multi-line blocks whose
50
+ # return value is being chained with another method (in which case braces
51
+ # are enforced).
52
+ - braces_for_chaining
53
+ ProceduralMethods:
54
+ # Methods that are known to be procedural in nature but look functional from
55
+ # their usage, e.g.
56
+ #
57
+ # time = Benchmark.realtime do
58
+ # foo.bar
59
+ # end
60
+ #
61
+ # Here, the return value of the block is discarded but the return value of
62
+ # `Benchmark.realtime` is used.
63
+ - benchmark
64
+ - bm
65
+ - bmbm
66
+ - create
67
+ - each_with_object
68
+ - measure
69
+ - new
70
+ - realtime
71
+ - tap
72
+ - with_object
73
+ FunctionalMethods:
74
+ # Methods that are known to be functional in nature but look procedural from
75
+ # their usage, e.g.
76
+ #
77
+ # let(:foo) { Foo.new }
78
+ #
79
+ # Here, the return value of `Foo.new` is used to define a `foo` helper but
80
+ # doesn't appear to be used from the return value of `let`.
81
+ - let
82
+ - let!
83
+ - subject
84
+ - watch
85
+ IgnoredMethods:
86
+ # Methods that can be either procedural or functional and cannot be
87
+ # categorised from their usage alone, e.g.
88
+ #
89
+ # foo = lambda do |x|
90
+ # puts "Hello, #{x}"
91
+ # end
92
+ #
93
+ # foo = lambda do |x|
94
+ # x * 100
95
+ # end
96
+ #
97
+ # Here, it is impossible to tell from the return value of `lambda` whether
98
+ # the inner block's return value is significant.
99
+ - lambda
100
+ - proc
101
+ - it
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at adamgeorge.31@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in user_prefs.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Adam George
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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # UserPrefs
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/user_prefs`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'user_prefs'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install user_prefs
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/user_prefs. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "user_prefs"
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
data/bin/setup ADDED
@@ -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
data/lib/user_prefs.rb ADDED
@@ -0,0 +1,103 @@
1
+ require 'user_prefs/class_methods'
2
+ require 'user_prefs/macro_methods'
3
+ require 'user_prefs/version'
4
+
5
+ module UserPrefs
6
+ class ColumnError < StandardError; end
7
+ class PreferenceError < StandardError; end
8
+
9
+ class << self
10
+ def included(base)
11
+ validate_column_and_type(base) unless RUBY_ENGINE == :opal
12
+
13
+ base.class_eval do
14
+ class_attribute :defined_prefs, :default_prefs
15
+
16
+ self.defined_prefs ||= []
17
+ self.default_prefs ||= {}
18
+
19
+ serialize(prefs_column.to_sym, RUBY_ENGINE == 'opal' ? Hash : HashWithIndifferentAccess)
20
+ end
21
+
22
+ base.extend(ClassMethods)
23
+ end
24
+
25
+ private
26
+
27
+ def no_column?(base)
28
+ !base.columns_hash[base.prefs_column]
29
+ end
30
+
31
+ def name_conflict?(base)
32
+ base.prefs_column == 'prefs'
33
+ end
34
+
35
+ def wrong_type?(base)
36
+ base.columns_hash[base.prefs_column] &&
37
+ base.columns_hash[base.prefs_column].type.to_s != 'text'
38
+ end
39
+
40
+ def validate_column_and_type(base)
41
+ raise ColumnError, "Preference column name cannot be named 'prefs'." if name_conflict?(base)
42
+ raise ColumnError, "#{base.name} must have column '#{base.prefs_column}'." if no_column?(base)
43
+ raise ColumnError, "#{base.prefs_column} must be of type 'text'." if wrong_type?(base)
44
+ end
45
+ end
46
+
47
+ def prefs
48
+ prefs_attr.merge(Hash[self.class.defined_prefs.map { |k| [k, send("#{k}_pref")] }])
49
+ end
50
+
51
+ def add_pref(key, value)
52
+ self.prefs_attr = prefs_attr.merge(Hash[key, value])
53
+ end
54
+
55
+ def delete_pref(key)
56
+ self.prefs_attr = prefs_attr.reject { |k, _v| k.to_s == key.to_s }
57
+ end
58
+
59
+ def method_missing(method_name, *args, &block)
60
+ if (match_data = method_name.to_s.match(/(\w+)_pref(=|\?)?/))
61
+ preference_method(match_data[1], match_data[2], args.first)
62
+ else
63
+ super
64
+ end
65
+ end
66
+
67
+ def respond_to_missing?(method_name, include_private = false)
68
+ method_name =~ /(\w+)_pref(=|\?)?/ || super
69
+ end
70
+
71
+ private
72
+
73
+ def prefs_attr
74
+ send(self.class.prefs_column)
75
+ end
76
+
77
+ def prefs_attr=(value)
78
+ send("#{self.class.prefs_column}=", value)
79
+ end
80
+
81
+ def preference_method(key, method = nil, new_value = nil)
82
+ case method
83
+ when '?' then prefs_attr.key?(key)
84
+ when '=' then prefs_attr[key] = new_value
85
+ else prefs_attr[key]
86
+ end
87
+ end
88
+ end
89
+
90
+ ActiveRecord::Base.class_eval do
91
+ extend UserPrefs::MacroMethods
92
+ end
93
+
94
+ unless RUBY_ENGINE == 'opal'
95
+ # Now if we are NOT running inside of opal, set things up so opal can find
96
+ # the files. The whole thing is rescued in case the opal gem is not available.
97
+ # This would happen if the gem is being used server side ONLY.
98
+ begin
99
+ require 'opal'
100
+ Opal.append_path File.expand_path('..', __FILE__).untaint
101
+ rescue LoadError # rubocop:disable Lint/HandleExceptions
102
+ end
103
+ end
@@ -0,0 +1,29 @@
1
+ module UserPrefs
2
+ module ClassMethods
3
+ def preference(name = nil, opts = {})
4
+ validate_name(name)
5
+
6
+ defined_prefs << name
7
+ default_prefs[name.to_s] ||= opts[:default]
8
+
9
+ define_method("#{name}_pref") do
10
+ prefs_attr[name] || opts[:default]
11
+ end
12
+
13
+ define_method("#{name}_pref=") do |new_value|
14
+ self.prefs_attr = prefs_attr.merge(Hash[name, new_value])
15
+ end
16
+
17
+ define_method("#{name}_pref?") do
18
+ prefs_attr.key?(name)
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def validate_name(name)
25
+ raise PreferenceError, 'Preference name must be specified.' unless name
26
+ raise PreferenceError, "#{name} has already been specified." if defined_prefs.include?(name)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module UserPrefs
2
+ module MacroMethods
3
+ # I think this is a good name for the macro, but Rubocop disagrees...
4
+ def has_preferences(column_name = 'preferences') # rubocop:disable Style/PredicateName
5
+ class_attribute :prefs_column
6
+
7
+ self.prefs_column = column_name.to_s
8
+ include UserPrefs
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module UserPrefs
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'user_prefs/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'user_prefs'
8
+ spec.version = UserPrefs::VERSION
9
+ spec.authors = ['Adam George']
10
+ spec.email = ['adamgeorge.31@gmail.com']
11
+
12
+ spec.summary = 'Quick and easy model preferences for Rails.'
13
+ spec.description =
14
+ 'Add preferences to any Rails model easily by adding a text column and adding a couple macros'
15
+ spec.homepage = 'https://github.com/adamcreekroad/user_prefs'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files =
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
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.12'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rubocop'
27
+ spec.add_development_dependency 'rspec'
28
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: user_prefs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam George
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-17 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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: rubocop
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
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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
+ description: Add preferences to any Rails model easily by adding a text column and
70
+ adding a couple macros
71
+ email:
72
+ - adamgeorge.31@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rubocop.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/user_prefs.rb
87
+ - lib/user_prefs/class_methods.rb
88
+ - lib/user_prefs/macro_methods.rb
89
+ - lib/user_prefs/version.rb
90
+ - user_prefs.gemspec
91
+ homepage: https://github.com/adamcreekroad/user_prefs
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.5.1
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Quick and easy model preferences for Rails.
115
+ test_files: []