set_man 1.0.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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ set_name
2
+ *.swo
3
+ *.swn
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Alexey Plutalov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,123 @@
1
+ #SetMan
2
+ Simple settings manager for your application.
3
+
4
+ ##Requirements
5
+ Rails 3 only.
6
+
7
+ ##Install
8
+ Add to your gemfile
9
+
10
+ gem 'set_man'
11
+
12
+ after this execute in console:
13
+
14
+ rails g set_man [ModelName]
15
+
16
+ ##Overview
17
+
18
+ For enable SetMan for model, you can add call `settings`.
19
+
20
+ class Options < ActiveRecord::Base
21
+
22
+ settings
23
+
24
+ end
25
+
26
+ You can set default options:
27
+
28
+ class Options < ActiveRecord::Base
29
+
30
+ self.create do |option|
31
+ option.name = :per_page
32
+ option.value = 20
33
+ option.type = :integer
34
+ end
35
+
36
+ settings do
37
+ default :site_name, "Site Name"
38
+ default :per_page, 30
39
+ end
40
+
41
+ end
42
+
43
+ Options.where(:name => :site_name).first.value #=> "Site Name"
44
+ Options.where(:name => :per_page).first.value #=> 20
45
+
46
+ For getting option, you can use `get_option`:
47
+
48
+ Options.get_option :site_name #=> "Site Name"
49
+
50
+ or shorten variant:
51
+
52
+ Options.get :site_name #=> "Site Name"
53
+
54
+ For setting option, you can use `set_option`:
55
+
56
+ Options.set_option :site_name, "New name" #=> "New Name"
57
+
58
+ or shorten variant:
59
+
60
+ Options.set :site_name, "New Name" #=> "New Name"
61
+
62
+ Methods `set_option` and `set` can given `:to` parameter:
63
+
64
+ Options.set_option :site_name, :to => "New Name" #=> "New Name"
65
+ Options.set :site_name, :to => "New Name" #=> "New Name"
66
+
67
+ If given second argument, and :to, `set_option` will use second argument as new value:
68
+
69
+ Options.set_option :site_name, "New Name", :to => "Not New Name" #=> "New Name"
70
+
71
+ If option doesn't exists, then option will been created:
72
+
73
+ Options.set_option :not_existing_option, "Some value" #=> "Some value"
74
+
75
+ For deleting option, you can use `del_option`:
76
+
77
+ Options.del_option :site_name #=> "New Name"
78
+
79
+ or shorten variant:
80
+
81
+ Options.del :site_name #=> "New Name"
82
+
83
+ You can use methods with names of options for accessing:
84
+
85
+ Options.site_name #=> "Site Name"
86
+ Options.site_name = "New Name" #=> "Site Name"
87
+ Options.site_name? #=> true
88
+ Options.site_name! #=> "Site Name"
89
+
90
+ Postfix `?` using for test of existing option.
91
+ Postfix `!` using for deleting option.
92
+
93
+ This is method not override default methods of `ActiveRecord::Base` methods and other. Example:
94
+
95
+ Options.columns_hash #=> columns list of table
96
+
97
+ If you need returning `ActiveRecord::Base` object, instead value of option, you can use `:as_object => true`:
98
+
99
+ Options.get_option :site_name, :as_object => true #=> #<Options ...>
100
+
101
+ This work for short names too:
102
+
103
+ Options.site_name :as_object => true #=> #<Options ...>
104
+
105
+ ### Multiple options
106
+ You can work with multiple options:
107
+
108
+ Options.get_options [:site_name, :site_description] #=> { "site_name" => "Site Name", "site_description" => "Site description" }
109
+ Options.set_options { :site_name => "New Site Name", :per_page => 20 } #=> { "site_name" => "New Site Name", :per_page => 20 }
110
+ Options.del_options [:site_name, :per_page] #=> { "site_name" => "New Site Name", "per_page" => 20 }
111
+
112
+ If given `:as_object => true`, returning array of objects, instead hash of values.
113
+
114
+ `get_options` and `del_options` return only existing options.
115
+
116
+ ## Ideas for next
117
+
118
+ * Support of cache;
119
+ * Default options;
120
+ * Options group;
121
+ * Callbacks, validations and own types.
122
+
123
+ Copyright (c) 2011 Alexey Plutalov, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rdoc/task'
4
+ require 'bundler/gem_tasks'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the set_man plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the set_man plugin.'
18
+ RDoc::Task.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'SetMan'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'set_man'
@@ -0,0 +1,26 @@
1
+ require 'rails/generators/active_record'
2
+ require 'generators/set_man/orm_helpers'
3
+
4
+ module ActiveRecord
5
+ module Generators
6
+ class SetManGenerator < ActiveRecord::Generators::Base
7
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
8
+
9
+ include SetMan::Generators::OrmHelpers
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def generate_model
13
+ invoke "active_record:model", [name], :migration => false unless model_exists? && behavior == :invoke
14
+ end
15
+
16
+ def copy_set_man_migration
17
+ migration_template "migration.rb", "db/migrate/set_man_create_#{table_name}"
18
+ end
19
+
20
+ def inject_devise_content
21
+ inject_into_class(model_path, class_name, model_contents) if model_exists?
22
+ end
23
+
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,15 @@
1
+ class SetManCreate<%= table_name.camelize %> < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :<%= table_name %> do |t|
4
+ t.string :name
5
+ t.text :value
6
+ t.string :type
7
+ end
8
+
9
+ add_index :<%= table_name %>, :name, :unique => true
10
+ end
11
+
12
+ def self.down
13
+ drop_table :<%= table_name %>
14
+ end
15
+ end
@@ -0,0 +1,20 @@
1
+ module SetMan
2
+ module Generators
3
+ module OrmHelpers
4
+ def model_contents
5
+ <<-CONTENT
6
+ # SetMan initialization for this model.
7
+ settings
8
+ CONTENT
9
+ end
10
+
11
+ def model_exists?
12
+ File.exists?(File.join(destination_root, model_path))
13
+ end
14
+
15
+ def model_path
16
+ @model_path ||= File.join("app", "models", "#{file_path}.rb")
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module SetMan
2
+ module Generators
3
+ class SetManGenerator < Rails::Generators::NamedBase
4
+ namespace "set_man"
5
+ source_root File.expand_path("../templates", __FILE__)
6
+
7
+ desc "Generates a model with the given NAME (if one does not exist) with SetMan " <<
8
+ "configuration plus a migration file."
9
+
10
+ hook_for :orm
11
+
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,195 @@
1
+ module SetMan::ActiveRecord
2
+
3
+ module Extension
4
+
5
+ module ClassMethods
6
+
7
+ # TODO: May be add support of block for *_option and *_options interfaces?
8
+
9
+ def check_option_name(name)
10
+ unless name.class == String or name.class == Symbol
11
+ return nil
12
+ end
13
+ unless /^[a-z][a-z0-9_]*$/ === name.to_s
14
+ return nil
15
+ end
16
+ true
17
+ end
18
+
19
+ # *_option interface
20
+
21
+ # USAGE
22
+ # Model.get_option :some_option #=> return value of some_option
23
+ # Model.get_option :some_option, :as_object => true #=> return some_option as Model instance
24
+ def get_option(*args)
25
+ # Extract function options
26
+ as_object = args.extract_options![:as_object]
27
+ name = args[0]
28
+ # Extract option
29
+ if self.check_option_name(name)
30
+ option = self.where(:name => name).first
31
+ as_object ? option : option.value if option
32
+ end
33
+ end
34
+
35
+ # USAGE
36
+ # Model.set_option :some_option, value
37
+ # Model.set_option :some_option, :to => value
38
+ # Model.set_option :some_option, value, :as_object => true
39
+ # Model.set_option :some_option, :to => value, :as_object => true
40
+ def set_option(*args)
41
+ # Extract function options
42
+ function_options = args.extract_options!
43
+ as_object = function_options[:as_object]
44
+ name = args[0]
45
+ value = args.size == 2 ? args[1] : function_options[:to]
46
+ # Set new value for option
47
+ if self.check_option_name(name)
48
+ option = self.find_or_create_by_name(name)
49
+ option.value = value
50
+ as_object ? option : option.value if option.save
51
+ end
52
+ end
53
+
54
+ # USAGE
55
+ # Model.del_option :some_option #=> value of some_option
56
+ # Model.del_option :some_option, :as_object => true
57
+ def del_option(*args)
58
+ # Extract function options
59
+ as_object = args.extract_options![:as_object]
60
+ name = args[0]
61
+ # Delete option
62
+ if self.check_option_name(name)
63
+ option = self.where(:name => name).first
64
+ as_object ? option.destroy : option.destroy.value if option
65
+ end
66
+ end
67
+
68
+ # *_options interface
69
+
70
+ def get_options(*args)
71
+ # Extract function options
72
+ as_object = args.extract_options![:as_object]
73
+ names = args.delete_if { |name| !self.check_option_name(name) }
74
+ # Extract options values
75
+ unless names.empty?
76
+ options = self.where(:name => names)
77
+ if as_object
78
+ options
79
+ else
80
+ result = Hash.new
81
+ options.each { |option| result[option.name] = option.value }
82
+ result
83
+ end
84
+ end
85
+ end
86
+
87
+ def set_options(*args)
88
+ # TODO: This function is very hard. Optimize it!
89
+ # TODO: If possible, function logic must be rewritting for use multiple updates.
90
+
91
+ # Extract function options
92
+ function_options = args.extract_options!
93
+ as_object = function_options.delete(:as_object)
94
+ updates = function_options.delete_if { |name, value| !self.check_option_name(name) }
95
+ # Update attributes
96
+ if as_object
97
+ result = Array.new
98
+ updates.each { |name, value| result << self.set_option(name, value, :as_object => true) }
99
+ result.delete_if { |item| !item }
100
+ else
101
+ result = Hash.new
102
+ updates.each do |name, value|
103
+ option = self.set_option name, value, :as_object => true
104
+ result[option.name] = option.value if option
105
+ end
106
+ end
107
+ end
108
+
109
+ def del_options(*args)
110
+ # Extract function options
111
+ as_object = args.extract_options![:as_object]
112
+ names = args.delete_if { |arg| !self.check_option_name(arg) }
113
+ # Delete options
114
+ # destroy_all not used, because this don't return list of deleted records, but return count of deleted records
115
+ if as_object
116
+ result = names.map { |name| self.where(:name => name).first.delete }
117
+ result.delete_if { |item| !item }
118
+ else
119
+ result = Hash.new
120
+ names.each do |name|
121
+ option = self.where(:name => name).first
122
+ result[option.name] = option.delete.value if option
123
+ end
124
+ result
125
+ end
126
+ end
127
+
128
+ alias get get_option
129
+ alias set set_option
130
+ alias del del_option
131
+
132
+ def method_missing(method_id, *args, &block)
133
+ begin
134
+ super
135
+ rescue
136
+ case method_id
137
+ when /^[a-z][a-z0-9_]*$/
138
+ self.get_option method_id, *args
139
+ when /^[a-z][a-z0-9_]*=$/
140
+ self.set_option method_id.to_s.chop, *args
141
+ when /^[a-z][a-z0-9_]*!$/
142
+ self.del_option method_id.to_s.chop, *args
143
+ when /^[a-z][a-z0-9_]*\?$/
144
+ self.where(:name => method_id.to_s.chop).exists?
145
+ else raise NoMethodError, "undefined method `#{method_id} for #{self.name}"
146
+ end
147
+ end
148
+ end
149
+
150
+ def default(name, value)
151
+ unless self.exists?(name)
152
+ self.create do |option|
153
+ option.name = name
154
+ option.value = value
155
+ end
156
+ end
157
+ end
158
+
159
+ end
160
+
161
+ module InstanceMethods
162
+
163
+ protected
164
+
165
+ def to_sql
166
+ self[:value], self[:klass] = SetMan::SqlConverter.to_sql(self[:value])
167
+ end
168
+
169
+ def from_sql
170
+ self[:klass] = self[:klass].to_sym
171
+ self[:value] = SetMan::SqlConverter.from_sql(self[:value], self[:klass])
172
+ end
173
+
174
+ end
175
+
176
+ def self.included(klass)
177
+ klass.module_eval do
178
+ include InstanceMethods
179
+ extend ClassMethods
180
+
181
+ # Callbacks
182
+ before_validation :to_sql
183
+ after_save :from_sql
184
+ after_find :from_sql
185
+ end
186
+ klass.class_eval do
187
+ # Validations
188
+ validates_format_of :name, :with => /^[a-z][a-z0-9_]*$/, :message => "Invalid format of name!"
189
+ validates_presence_of :name, :klass
190
+ end
191
+ end
192
+
193
+ end
194
+
195
+ end
@@ -0,0 +1,22 @@
1
+ module SetMan::ActiveRecord
2
+
3
+ module Setup
4
+
5
+ def self.included(klass)
6
+ klass.module_eval do
7
+ extend ClassMethods
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ def settings(&block)
14
+ self.send :include, SetMan::ActiveRecord::Extension
15
+ self.class_eval &block if block
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
22
+ end
@@ -0,0 +1,7 @@
1
+ module SetMan::Plugin
2
+
3
+ def self.setup!
4
+ ActiveRecord::Base.send :include, SetMan::ActiveRecord::Setup if defined?(ActiveRecord::Base)
5
+ end
6
+
7
+ end
@@ -0,0 +1,37 @@
1
+ module SetMan::SqlConverter
2
+
3
+ def self.to_sql(value)
4
+ result = Array.new
5
+ case value.class.name
6
+ when "String" then result[1] = :string
7
+ when "Symbol" then result[1] = :symbol
8
+ when "Fixnum" then result[1] = :integer
9
+ when "Bignum" then result[1] = :integer
10
+ when "Float" then result[1] = :rational
11
+ when "TrueClass" then result[1] = :true
12
+ when "FalseClass" then result[1] = :false
13
+ when "NilClass" then result[1] = :nil
14
+ else result[1] = :unknown
15
+ end
16
+ if value.class == String
17
+ result[0] = value
18
+ else
19
+ result[0] = value.to_s
20
+ end
21
+ result
22
+ end
23
+
24
+ def self.from_sql(value, klass)
25
+ case klass
26
+ when :string then value
27
+ when :symbol then value.to_sym
28
+ when :integer then value.to_i
29
+ when :rational then value.to_r
30
+ when :true then true
31
+ when :false then false
32
+ when :nil then nil
33
+ else value
34
+ end
35
+ end
36
+
37
+ end
@@ -0,0 +1,12 @@
1
+ module SetMan
2
+ module Version
3
+
4
+ MAJOR = 1
5
+ MINOR = 0
6
+ PATCH = 0
7
+
8
+ STRING = [MAJOR,MINOR,PATCH].compact.join('.')
9
+
10
+ end
11
+ end
12
+
data/lib/set_man.rb ADDED
@@ -0,0 +1,14 @@
1
+ $:.push File.dirname(__FILE__)
2
+
3
+ require 'active_support/core_ext/module/attribute_accessors'
4
+
5
+ module SetMan
6
+
7
+ end
8
+
9
+ require 'set_man/plugin'
10
+ require 'set_man/active_record/extension'
11
+ require 'set_man/active_record/setup'
12
+ require 'set_man/sql_converter'
13
+
14
+ SetMan::Plugin.setup!
data/set_man.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require './lib/set_man/version.rb'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "set_man"
6
+ s.version = SetMan::Version::STRING
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["plutalov_alexey"]
9
+ s.email = ["demiazz.py@gmail.com"]
10
+ s.homepage = "https://github.com/demiazz/set_man"
11
+ s.summary = %q{Simple settings manager for your application.}
12
+ s.description = %q{Simple settings manager for your application.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- test/*`.split("\n")
16
+ s.require_paths = ["lib"]
17
+ end
18
+
data/setman_test ADDED
Binary file
@@ -0,0 +1,95 @@
1
+ require 'test_helper'
2
+
3
+ class SetManActiveRecordClassMethodsOptionTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @data = [
7
+ { :name => :string_option, :value => "some_string", :new_value => "new_some_string" },
8
+ { :name => :symbol_option, :value => :some_symbol, :new_value => :some_symbol },
9
+ { :name => :fixnum_option, :value => 112, :new_value => 211 },
10
+ { :name => :bignum_option, :value => 256**40, :new_value => 256**90 },
11
+ { :name => :float_option, :value => 112.2, :new_value => 211.1 },
12
+ { :name => :true_option, :value => true, :new_value => false },
13
+ { :name => :false_option, :value => false, :new_value => true },
14
+ { :name => :nil_option, :value => nil, :new_value => false },
15
+ ]
16
+ Options.delete_all
17
+ @data.each do |item|
18
+ Options.create do |option|
19
+ option.name = item[:name]
20
+ option.value = item[:value]
21
+ end
22
+ end
23
+ end
24
+
25
+ test "Model#get_option should return right value" do
26
+ @data.each do |item|
27
+ assert_equal item[:value], Options.get_option(item[:name])
28
+ end
29
+ end
30
+
31
+ test "Model#get_option should return right record if :as_object => true" do
32
+ @data.each do |item|
33
+ assert_equal Options.where(:name => item[:name]).first, Options.get_option(item[:name], :as_object => true)
34
+ assert_equal item[:name].to_s, Options.get_option(item[:name], :as_object => true).name
35
+ assert_equal item[:value], Options.get_option(item[:name], :as_object => true).value
36
+ end
37
+ end
38
+
39
+ test "Model#get_option should return nil, if option doesn't exists" do
40
+ assert_nil Options.get_option(:not_exists_option)
41
+ assert_nil Options.get_option(:not_exists_option, :as_object => true)
42
+ end
43
+
44
+ test "Model#set_option should return right new value" do
45
+ @data.each do |item|
46
+ assert_equal item[:new_value], Options.set_option(item[:name], item[:new_value])
47
+ end
48
+ end
49
+
50
+ test "Model#set_option should return right record if :as_object => true" do
51
+ @data.each do |item|
52
+ assert_equal Options.set_option(item[:name], item[:new_value], :as_object => true), Options.where(:name => item[:name]).first
53
+ assert_equal item[:new_value], Options.where(:name => item[:name]).first.value
54
+ end
55
+ end
56
+
57
+ test "Model#set_option should return create new option, if option doesn't exists" do
58
+ Options.delete_all
59
+ @data.each do |item|
60
+ assert_equal item[:value], Options.set_option(item[:name], item[:value])
61
+ end
62
+ end
63
+
64
+ test "Model#set_option should right use :to option" do
65
+ @data.each do |item|
66
+ assert_equal item[:new_value], Options.set_option(item[:name], :to => item[:new_value])
67
+ end
68
+ end
69
+
70
+ test "Model#set_option should use arg instead :to option, if given both" do
71
+ @data.each do |item|
72
+ assert_equal item[:new_value], Options.set_option(item[:name], item[:new_value], :to => item[:value])
73
+ end
74
+ end
75
+
76
+ test "Model#del_option should delete option and return value" do
77
+ @data.each do |item|
78
+ assert_equal item[:value], Options.del_option(item[:name])
79
+ assert_nil Options.where(:name => item[:name]).first
80
+ end
81
+ end
82
+
83
+ test "Model#del_option should delete option, and return record if :as_object => true" do
84
+ @data.each do |item|
85
+ assert_equal Options.where(:name => item[:name]).first, Options.del_option(item[:name], :as_object => true)
86
+ assert_nil Options.where(:name => item[:name]).first
87
+ end
88
+ end
89
+
90
+ test "Model#del_option should return nil if option doesn't exists" do
91
+ assert_nil Options.del_option(:not_exists_option)
92
+ assert_nil Options.del_option(:not_exists_option, :as_object => true)
93
+ end
94
+
95
+ end
@@ -0,0 +1,121 @@
1
+ require 'test_helper'
2
+
3
+ class SetManActiveRecordClassMethodsOptionsTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @data = [
7
+ { :name => :string_option, :value => "some_string", :new_value => "new_some_string" },
8
+ { :name => :symbol_option, :value => :some_symbol, :new_value => :some_symbol },
9
+ { :name => :fixnum_option, :value => 112, :new_value => 211 },
10
+ { :name => :bignum_option, :value => 256**40, :new_value => 256**90 },
11
+ { :name => :float_option, :value => 112.2, :new_value => 211.1 },
12
+ { :name => :true_option, :value => true, :new_value => false },
13
+ { :name => :false_option, :value => false, :new_value => true },
14
+ { :name => :nil_option, :value => nil, :new_value => false },
15
+ ]
16
+ Options.delete_all
17
+ @data.each do |item|
18
+ Options.create do |option|
19
+ option.name = item[:name]
20
+ option.value = item[:value]
21
+ end
22
+ end
23
+ @existing_names = [
24
+ :string_option,
25
+ :symbol_option,
26
+ :fixnum_option,
27
+ :bignum_option,
28
+ :float_option,
29
+ :true_option,
30
+ :false_option,
31
+ :nil_option
32
+ ]
33
+ @not_existing_names = @existing_names.map { |name| "not_existing_#{name}".to_sym }
34
+ end
35
+
36
+ test "Model#get_options should return hash of options" do
37
+ should_returning_result = Hash.new
38
+ Options.all.each do |option|
39
+ should_returning_result[option.name] = option.value
40
+ end
41
+ assert_equal should_returning_result, Options.get_options(*@existing_names)
42
+ end
43
+
44
+ test "Model#get_options should return array of records if :as_object => true" do
45
+ assert_equal Options.where(:name => @existing_names), Options.get_options(*@existing_names, :as_object => true)
46
+ end
47
+
48
+ test "Model#get_options should return only existing options" do
49
+ should_returning_result = Hash.new
50
+ Options.all.each do |option|
51
+ should_returning_result[option.name] = option.value
52
+ end
53
+ assert_equal should_returning_result, Options.get_options(*(@existing_names + @not_existing_names))
54
+ assert_equal Hash.new, Options.get_options(*@not_existing_names)
55
+ end
56
+
57
+ test "Model#get_options should return only existing records, if :as_object => true" do
58
+ options = Options.where(:name => @existing_options)
59
+ get_options = Options.get_options(*(@existing_names + @not_existing_names), :as_object => true)
60
+ options.each_index do |index|
61
+ assert_equal options[index], get_options[index]
62
+ end
63
+ assert_equal Array.new, Options.get_options(*@not_existing_names, :as_object => true)
64
+ end
65
+
66
+ test "Model#set_options should update existing options" do
67
+ updates = Hash.new
68
+ @data.each do |option|
69
+ updates[option[:name].to_s] = option[:new_value]
70
+ end
71
+ assert_equal updates, Options.set_options(updates)
72
+ end
73
+
74
+ test "Model#set_options should update existing options, and return records, if :as_object => true" do
75
+ updates = Hash.new
76
+ @data.each do |option|
77
+ updates[option[:name].to_s] = option[:new_value]
78
+ end
79
+ updates[:as_object] = true
80
+ options = Options.set_options updates
81
+ updates.delete(:as_object)
82
+ options.each do |option|
83
+ assert_equal Options, option.class
84
+ assert updates.has_key? option.name
85
+ assert_equal updates[option.name], option.value
86
+ end
87
+ end
88
+
89
+ test "Model#set_options should create not existing options" do
90
+ updates = Hash.new
91
+ @not_existing_names.each_index do |index|
92
+ updates[@not_existing_names[index].to_s] = @data[index][:value]
93
+ end
94
+ assert_equal updates, Options.set_options(updates)
95
+ @not_existing_names.each do |name|
96
+ assert_not_nil Options.where(:name => name).first
97
+ end
98
+ end
99
+
100
+ test "Model#del_options should delete existing options, and return values" do
101
+ right_result = Hash.new
102
+ @data.each do |item|
103
+ right_result[item[:name].to_s] = item[:value]
104
+ end
105
+ assert_equal right_result, Options.del_options(*@existing_names)
106
+ @existing_names.each do |name|
107
+ assert_nil Options.where(:name => name).first
108
+ end
109
+ assert Options.del_options(*@not_existing_names).empty?
110
+ end
111
+
112
+ test "Model#del_options should delete existing options, and return records, if :as_object => true" do
113
+ del_options = Options.del_options(*@existing_names, :as_object => true)
114
+ del_options.each do |option|
115
+ assert @existing_names.include?(option.name.to_sym)
116
+ assert_equal Options, option.class
117
+ assert_nil Options.where(:name => option.name).first
118
+ end
119
+ end
120
+
121
+ end
@@ -0,0 +1,62 @@
1
+ require 'test_helper'
2
+
3
+ class SetManActiveRecordClassMethodsTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @data = [
7
+ { :name => :string_option, :value => "some_string", :new_value => "new_some_string" },
8
+ { :name => :symbol_option, :value => :some_symbol, :new_value => :some_symbol },
9
+ { :name => :fixnum_option, :value => 112, :new_value => 211 },
10
+ { :name => :bignum_option, :value => 256**40, :new_value => 256**90 },
11
+ { :name => :float_option, :value => 112.2, :new_value => 211.1 },
12
+ { :name => :true_option, :value => true, :new_value => false },
13
+ { :name => :false_option, :value => false, :new_value => true },
14
+ { :name => :nil_option, :value => nil, :new_value => false },
15
+ ]
16
+ Options.delete_all
17
+ @data.each do |item|
18
+ Options.create do |option|
19
+ option.name = item[:name]
20
+ option.value = item[:value]
21
+ end
22
+ end
23
+ end
24
+
25
+ test "ActiveRecord::Base inherited classes should have extended methods before calling settings method" do
26
+ methods = [
27
+ :get_option,
28
+ :set_option,
29
+ :del_option,
30
+ :get_options,
31
+ :set_options,
32
+ :del_options,
33
+ :get,
34
+ :set,
35
+ :del
36
+ ]
37
+ methods.each do |method|
38
+ assert Options.respond_to?(method)
39
+ assert !NotOptions.respond_to?(method)
40
+ end
41
+ end
42
+
43
+ test "ActiveRecord::Base#missing_method in inherited classes should correct detect and call methods" do
44
+ @data.each do |item|
45
+ assert_nothing_raised (NoMethodError) { Options.send(item[:name]) }
46
+ assert_nothing_raised (NoMethodError) { Options.send("#{item[:name]}?") }
47
+ assert_nothing_raised (NoMethodError) { Options.send("#{item[:name]}=") }
48
+ assert_nothing_raised (NoMethodError) { Options.send("#{item[:name]}!") }
49
+
50
+ assert_raise (NoMethodError) { NotOptions.send(item[:name]) }
51
+ assert_raise (NoMethodError) { NotOptions.send("#{item[:name]}?") }
52
+ assert_raise (NoMethodError) { NotOptions.send("#{item[:name]}=") }
53
+ assert_raise (NoMethodError) { NotOptions.send("#{item[:name]}!") }
54
+ end
55
+
56
+ assert_nothing_raised (NoMethodError) { Options.send(:not_exists_option) }
57
+ assert_nothing_raised (NoMethodError) { Options.send(:not_exists_option?) }
58
+ assert_nothing_raised (NoMethodError) { Options.send(:not_exists_option!) }
59
+ assert_nothing_raised (NoMethodError) { Options.send(:not_exists_option=) }
60
+ end
61
+
62
+ end
@@ -0,0 +1,13 @@
1
+ require 'test_helper'
2
+
3
+ class SetManActiveRecordDefaultTest < ActiveSupport::TestCase
4
+
5
+ test "should set default option, if not exists" do
6
+ assert_equal "Site Name", DefaultOptions.where(:name => :site_name).first.value
7
+ end
8
+
9
+ test "shouldn't set default option, if exists do" do
10
+ assert_equal 20, DefaultOptions.where(:name => :per_page).first.value
11
+ end
12
+
13
+ end
@@ -0,0 +1,35 @@
1
+ require './test_helper'
2
+
3
+ class SetManActiveRecordValidationTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ Options.delete_all
7
+ end
8
+
9
+ test "should raise exception if name empty" do
10
+ option = Options.new
11
+ option.value = 111
12
+ option.klass = :integer
13
+ assert_raise (ActiveRecord::RecordInvalid) { option.save! }
14
+ assert !option.save
15
+ end
16
+
17
+ test "should raise exception if name format is wrong" do
18
+ option = Options.new
19
+ option.name = "some+name"
20
+ option.value = 111
21
+ option.klass = :integer
22
+ assert_raise (ActiveRecord::RecordInvalid) { option.save! }
23
+ assert !option.save
24
+ end
25
+
26
+ test "shouldn't raise exception if klass is empty" do
27
+ option = Options.new do |option|
28
+ option.name = "some_name"
29
+ option.value = 111
30
+ end
31
+ assert_nothing_raised (ActiveRecord::RecordInvalid) { option.save! }
32
+ assert option.save
33
+ end
34
+
35
+ end
@@ -0,0 +1,14 @@
1
+ require 'test_helper'
2
+
3
+ class SetManPluginTest < ActiveSupport::TestCase
4
+
5
+ test "ActiveRecord::Base should have 'settings' method" do
6
+ assert ActiveRecord::Base.respond_to?(:settings)
7
+ end
8
+
9
+ test "ActiveRecord::Base inherited classes should have 'settings' method" do
10
+ assert Options.respond_to?(:settings)
11
+ assert NotOptions.respond_to?(:settings)
12
+ end
13
+
14
+ end
@@ -0,0 +1,46 @@
1
+ require 'test_helper'
2
+
3
+ class SetManSqlConverterTest < ActiveSupport::TestCase
4
+
5
+ def setup
6
+ @data = [
7
+ { :klass => :string, :from => "some_string", :to => "some_string" },
8
+ { :klass => :symbol, :from => :some_symbol, :to => "some_symbol" },
9
+ { :klass => :integer, :from => 112, :to => "112" },
10
+ { :klass => :integer, :from => 256**40, :to => (256**40).to_s },
11
+ { :klass => :rational, :from => 112.2, :to => "112.2" },
12
+ { :klass => :true, :from => true, :to => "true" },
13
+ { :klass => :false, :from => false, :to => "false" },
14
+ { :klass => :nil, :from => nil, :to => "" },
15
+ { :klass => :unknown, :from => [1, 2, 3], :to => [1, 2, 3].to_s }
16
+ ]
17
+ end
18
+
19
+ test "SetMan::SqlConverter#to_sql should correct convert values" do
20
+ @data.each do |item|
21
+ assert_equal [item[:to], item[:klass]], SetMan::SqlConverter.to_sql(item[:from])
22
+ end
23
+ end
24
+
25
+ test "SetMan::SqlConverter#from_sql should correct convert values" do
26
+ @data.each do |item|
27
+ unless item[:klass] == :unknown
28
+ assert_equal item[:from], SetMan::SqlConverter.from_sql(item[:to], item[:klass])
29
+ else
30
+ assert_equal item[:to], SetMan::SqlConverter.from_sql(item[:to], item[:klass])
31
+ end
32
+ end
33
+ end
34
+
35
+ test "SetMan::SqlConverter#from_sql should correct convert output of SetMan::SqlConverter#to_sql" do
36
+ @data.each do |item|
37
+ converted = SetMan::SqlConverter.to_sql(item[:from])
38
+ unless item[:klass] == :unknown
39
+ assert_equal item[:from], SetMan::SqlConverter.from_sql(converted[0], converted[1])
40
+ else
41
+ assert_equal item[:to], SetMan::SqlConverter.from_sql(converted[0], converted[1])
42
+ end
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class SetManTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
data/test/setman_test ADDED
Binary file
@@ -0,0 +1,62 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_record'
5
+
6
+ require File.join(File.dirname(__FILE__), '..', 'lib', 'set_man.rb')
7
+
8
+ ##### ACTIVERECORD TEST INIT #####
9
+
10
+ # Connect to memory SQLite database
11
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:", :database => 'setman_test')
12
+
13
+ # Create scheme of database
14
+ ActiveRecord::Schema.define(:version => 1) do
15
+
16
+ create_table :options, :force => true do |t|
17
+ t.string :name
18
+ t.text :value
19
+ t.string :klass
20
+ end
21
+
22
+ create_table :not_options, :force => true do |t|
23
+ t.string :name
24
+ t.text :value
25
+ t.string :klass
26
+ end
27
+
28
+ create_table :default_options, :force => true do |t|
29
+ t.string :name
30
+ t.text :value
31
+ t.string :klass
32
+ end
33
+
34
+ end
35
+
36
+ # Model with SetMan extension
37
+ class Options < ActiveRecord::Base
38
+
39
+ settings
40
+
41
+ end
42
+
43
+ # Model without SetMan extension
44
+ class NotOptions < ActiveRecord::Base
45
+
46
+ end
47
+
48
+ # Model for testing default options
49
+ class DefaultOptions < ActiveRecord::Base
50
+
51
+ self.create do |option|
52
+ option.name = :per_page
53
+ option.value = 20
54
+ option.klass = :integer
55
+ end
56
+
57
+ settings do
58
+ default :site_name, "Site Name"
59
+ default :per_page, 30
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: set_man
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - plutalov_alexey
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-19 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: Simple settings manager for your application.
17
+ email:
18
+ - demiazz.py@gmail.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - .gitignore
27
+ - MIT-LICENSE
28
+ - README.md
29
+ - Rakefile
30
+ - init.rb
31
+ - lib/generators/active_record/set_man_generator.rb
32
+ - lib/generators/active_record/templates/migration.rb
33
+ - lib/generators/set_man/orm_helpers.rb
34
+ - lib/generators/set_man/set_man_generator.rb
35
+ - lib/set_man.rb
36
+ - lib/set_man/active_record/extension.rb
37
+ - lib/set_man/active_record/setup.rb
38
+ - lib/set_man/plugin.rb
39
+ - lib/set_man/sql_converter.rb
40
+ - lib/set_man/version.rb
41
+ - set_man.gemspec
42
+ - setman_test
43
+ - test/set_man_active_record_class_methods_option_test.rb
44
+ - test/set_man_active_record_class_methods_options_test.rb
45
+ - test/set_man_active_record_class_methods_test.rb
46
+ - test/set_man_active_record_default_test.rb
47
+ - test/set_man_active_record_validation.rb
48
+ - test/set_man_plugin_test.rb
49
+ - test/set_man_sql_converter_test.rb
50
+ - test/set_man_test.rb
51
+ - test/setman_test
52
+ - test/test_helper.rb
53
+ homepage: https://github.com/demiazz/set_man
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.5
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Simple settings manager for your application.
80
+ test_files:
81
+ - test/set_man_active_record_class_methods_option_test.rb
82
+ - test/set_man_active_record_class_methods_options_test.rb
83
+ - test/set_man_active_record_class_methods_test.rb
84
+ - test/set_man_active_record_default_test.rb
85
+ - test/set_man_active_record_validation.rb
86
+ - test/set_man_plugin_test.rb
87
+ - test/set_man_sql_converter_test.rb
88
+ - test/set_man_test.rb
89
+ - test/setman_test
90
+ - test/test_helper.rb