settings_on_rails 0.1.30

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
+ SHA1:
3
+ metadata.gz: 1902e352d4b86e9b07fb40e0b3364a32ebf810f2
4
+ data.tar.gz: 0ff9fa4d7d46470da596aab09983f07415b58980
5
+ SHA512:
6
+ metadata.gz: a23b884b80b9b6fc85920e42cc5b368a680176bd78e8e6910570ec6cf1b907bb39d42bc17f16523f99024a4994689f47b5b878a4a3a14bae5e13247543f700f8
7
+ data.tar.gz: 49efc61fc8d8f6efabdcfaee240db7f47032084c622fbf75ba79d4899385f6944326cf4de84a1a66d8cfa7d5c68caf77c7846e2555df543647498ee5fecc7a90
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.idea/
2
+ /bin/
3
+ /.bundle/
4
+ /.yardoc
5
+ /Gemfile.lock
6
+ /_yardoc/
7
+ /coverage/
8
+ /doc/
9
+ /pkg/
10
+ /spec/reports/
11
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.0
5
+ - 2.2.1
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in settings_on_rails.gemspec
4
+ gemspec
5
+
6
+ gem 'coveralls', require: false
7
+
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 ALLEN WANG QIANG
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,102 @@
1
+ # SettingsOnRails
2
+ [![Build Status](https://travis-ci.org/allenwq/settings_on_rails.svg?branch=master)](https://travis-ci.org/allenwq/settings_on_rails)
3
+ [![Coverage Status](https://coveralls.io/repos/allenwq/settings_on_rails/badge.svg?branch=master)](https://coveralls.io/r/allenwq/settings_on_rails?branch=master)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'settings_on_rails'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install settings_on_rails
20
+
21
+ ## Getting Started
22
+
23
+ Start off by adding a text field to the model which you want it to have settings
24
+ ```ruby
25
+ rails g migration add_settings_column_to_blogs settings_column:text
26
+
27
+ ```
28
+
29
+ Declare in model
30
+ ```ruby
31
+ class Blog < ActiveRecord::Base
32
+ has_settings_on :settings_column
33
+ end
34
+ ```
35
+
36
+ Set settings
37
+ ```ruby
38
+ @blog.settings.title = 'My Blog'
39
+ @blog.settings(:theme).background_color = 'blue'
40
+
41
+ @blog.save
42
+ ```
43
+
44
+ Get settings
45
+ ```ruby
46
+ @blog.settings(:theme).background_color # returns 'blue'
47
+
48
+ @blog.settings(:post).pagination # returns nil if not set
49
+
50
+ ```
51
+
52
+ ## Default Values
53
+
54
+ ```ruby
55
+ class Blog < ActiveRecord::Base
56
+ has_settings_on :column
57
+
58
+ has_settings do |s|
59
+ s.has_key :theme, defaults:{ background_color: 'red', text_size: 50 }
60
+ end
61
+ end
62
+ ```
63
+ OR
64
+ ```ruby
65
+ class Blog < ActiveRecord::Base
66
+ has_settings_on :column do |s|
67
+ s.define :theme, defaults:{ background_color: 'red', text_size: 50 }
68
+ end
69
+ end
70
+ ```
71
+
72
+ ## Nested/Multiple Keys
73
+
74
+
75
+ ```ruby
76
+ @blog.settings(:theme, :homepage).background_color = 'white'
77
+ #OR
78
+ @blog.settings(:theme).settings(:homepage).background_color = 'white'
79
+
80
+ @blog.settings(:theme, :homepage).background_color # 'white'
81
+ #OR
82
+ @blog.settings(:theme).settings(:homepage).background_color # 'white'
83
+ ```
84
+
85
+ ## Method Name Customization
86
+ You can customize the name of the settings method
87
+ ```ruby
88
+ class Blog < ActiveRecord::Base
89
+ has_settings_on :settings_column, method: :preferences
90
+ end
91
+
92
+ # Then you can do
93
+ @blog.preferences(:theme).background_color
94
+ ```
95
+
96
+ ## Contributing
97
+
98
+ 1. Fork it ( https://github.com/allenwq/settings_on_rails/fork )
99
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
100
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
101
+ 4. Push to the branch (`git push origin my-new-feature`)
102
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -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,23 @@
1
+ module SettingsOnRails
2
+ module Base
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def has_settings_on(column, options = {})
7
+ SettingsColumn.setup(self, column)
8
+
9
+ method_name = options[:method] || :settings
10
+ define_method method_name do |*keys|
11
+ column = SettingsColumn.check!(self)
12
+
13
+ SettingsHandler.new(keys, self, column, method_name)
14
+ end
15
+ end
16
+
17
+ def has_settings
18
+ raise NoSettingsColumnError unless SettingsColumn::NAME
19
+ yield if block_given?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module SettingsOnRails
2
+
3
+ class NoSettingsColumnError < StandardError
4
+ def message
5
+ 'settings column not specified, have you declared has_settings_on before?'
6
+ end
7
+ end
8
+
9
+ class ColumnNotExistError < StandardError
10
+ def message
11
+ 'settings column does not exist, have you added the column in database?'
12
+ end
13
+ end
14
+
15
+ class InvalidColumnTypeError < StandardError
16
+ def message
17
+ 'type of settings column must be text'
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ module SettingsOnRails
2
+ module SettingsColumn
3
+ NAME = :__settings_column
4
+
5
+ def self.setup(klass, column)
6
+ klass.class_eval do
7
+ class << self; attr_accessor SettingsColumn::NAME; end
8
+
9
+ serialize column, Hash
10
+ self.send(SettingsColumn::NAME.to_s + '=', column.to_sym)
11
+ end
12
+ end
13
+
14
+ def self.column(instance)
15
+ instance.class.send(SettingsColumn::NAME)
16
+ end
17
+
18
+ def self.check!(instance)
19
+ settings_column = column(instance)
20
+ raise NoSettingsColumnError unless settings_column
21
+ raise ColumnNotExistError unless instance.has_attribute?(settings_column)
22
+ raise InvalidColumnTypeError if column_type_not_text?(instance, settings_column)
23
+
24
+ settings_column
25
+ end
26
+
27
+ private
28
+ def self.column_type_not_text?(instance, settings_column)
29
+ return true if instance.column_for_attribute(settings_column).try(:sql_type) != 'text'
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,118 @@
1
+ module SettingsOnRails
2
+ class SettingsHandler
3
+ PREFIX = '_'
4
+
5
+ attr_accessor :keys, :parent
6
+ def initialize(keys, target_object, column, method_name, parent = nil)
7
+ @keys = _prefix(keys.dup)
8
+ @target_object = target_object
9
+ @column = column
10
+ @method_name = method_name
11
+ @parent = parent
12
+
13
+ self.class_eval do
14
+ define_method(method_name, instance_method(:_settings))
15
+ end
16
+ end
17
+
18
+ REGEX_SETTER = /\A([a-z]\w*)=\Z/i
19
+ REGEX_GETTER = /\A([a-z]\w*)\Z/i
20
+
21
+ def respond_to?(method_name, include_priv=false)
22
+ super || method_name.to_s =~ REGEX_SETTER
23
+ end
24
+
25
+ def method_missing(method_name, *args, &block)
26
+ if method_name.to_s =~ REGEX_SETTER && args.size == 1
27
+ _set_value($1, args.first)
28
+ elsif method_name.to_s =~ REGEX_GETTER && args.size == 0
29
+ _get_value($1)
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ private
36
+ def _settings(*keys)
37
+ raise ArgumentError, 'wrong number of arguments (0 for 1..n)' if keys.size == 0
38
+
39
+ SettingsHandler.new(keys, @target_object, @column, @method_name, self)
40
+ end
41
+
42
+ def _get_value(name)
43
+ node = _get_key_node
44
+
45
+ if node
46
+ node[name]
47
+ else
48
+ nil
49
+ end
50
+ end
51
+
52
+ def _set_value(name, v)
53
+ return if _get_value(name) == v
54
+
55
+ @target_object.send("#{@column}_will_change!")
56
+ _build_key_tree
57
+ node = _get_key_node
58
+ if v.nil?
59
+ node.delete(name)
60
+ else
61
+ node[name] = v
62
+ end
63
+ end
64
+
65
+ def _key_node_exist?
66
+ value = _target_column
67
+
68
+ for key in _key_chain
69
+ value = value[key]
70
+ return false unless value
71
+ end
72
+
73
+ true
74
+ end
75
+
76
+ def _get_key_node
77
+ ret = _key_node_exist?
78
+ return nil unless ret
79
+
80
+ _key_chain.inject(_target_column) { |h, key| h[key] }
81
+ end
82
+
83
+ def _build_key_tree
84
+ value = _target_column
85
+
86
+ for key in _key_chain
87
+ value[key] = {} unless value[key]
88
+ value = value[key]
89
+ end
90
+ end
91
+
92
+ def _target_column
93
+ @target_object.read_attribute(@column.to_sym)
94
+ end
95
+
96
+ # prefix keys with _, to differentiate `settings(:key_a, :key_b)` and settings(:key_a).key_b
97
+ # thus _ becomes an reserved field
98
+ def _prefix(keys)
99
+ for i in 0..(keys.length - 1)
100
+ keys[i] = (PREFIX + keys[i].to_s).to_sym
101
+ end
102
+ keys
103
+ end
104
+
105
+ # Returns a key chain which includes all parent's keys and self keys
106
+ def _key_chain
107
+ handler = self
108
+ key_chain = []
109
+
110
+ begin
111
+ key_chain = handler.keys + key_chain
112
+ handler = handler.parent
113
+ end while handler
114
+
115
+ key_chain
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,3 @@
1
+ module SettingsOnRails
2
+ VERSION = '0.1.30'
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'settings_on_rails/base'
2
+ require 'settings_on_rails/settings_column'
3
+ require 'settings_on_rails/settings_handler'
4
+ require 'settings_on_rails/exceptions'
5
+ require 'settings_on_rails/version'
6
+
7
+ ActiveRecord::Base.send :include, SettingsOnRails::Base
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'settings_on_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'settings_on_rails'
8
+ spec.version = SettingsOnRails::VERSION
9
+ spec.authors = ['ALLEN WANG QIANG']
10
+ spec.email = ['qwang@comp.nus.edu.sg']
11
+
12
+ spec.summary = %q{Handle Model specific Settings for Rails.}
13
+ spec.description = %q{Ruby Gem help to handle model specific settings for ActiveRecord, settings are stored as hashes. Supports multiple keys and default values.}
14
+ spec.homepage = 'https://github.com/allenwq/settings_on_rails'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = '>= 1.9'
23
+
24
+ spec.add_dependency 'activerecord', '>= 3.1'
25
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
26
+ spec.add_development_dependency 'bundler', '~> 1.6'
27
+ spec.add_development_dependency 'rspec', '~> 3'
28
+ spec.add_development_dependency 'rake', '~> 10'
29
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: settings_on_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.30
5
+ platform: ruby
6
+ authors:
7
+ - ALLEN WANG QIANG
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-09 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: '3.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.6'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.6'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10'
83
+ description: Ruby Gem help to handle model specific settings for ActiveRecord, settings
84
+ are stored as hashes. Supports multiple keys and default values.
85
+ email:
86
+ - qwang@comp.nus.edu.sg
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - CODE_OF_CONDUCT.md
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/settings_on_rails.rb
100
+ - lib/settings_on_rails/base.rb
101
+ - lib/settings_on_rails/exceptions.rb
102
+ - lib/settings_on_rails/settings_column.rb
103
+ - lib/settings_on_rails/settings_handler.rb
104
+ - lib/settings_on_rails/version.rb
105
+ - settings_on_rails.gemspec
106
+ homepage: https://github.com/allenwq/settings_on_rails
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '1.9'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.4.6
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Handle Model specific Settings for Rails.
130
+ test_files: []
131
+ has_rdoc: