confit_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Confit
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Confit'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+
27
+
28
+ Bundler::GemHelper.install_tasks
29
+
30
+ require 'rake/testtask'
31
+
32
+ Rake::TestTask.new(:test) do |t|
33
+ t.libs << 'lib'
34
+ t.libs << 'test'
35
+ t.pattern = 'test/**/*_test.rb'
36
+ t.verbose = false
37
+ end
38
+
39
+
40
+ task :default => :test
@@ -0,0 +1,81 @@
1
+ module Confit
2
+ class Confit < ActiveRecord::Base
3
+ self.table_name = :confits
4
+
5
+ attr_accessible :description, :name, :value
6
+ after_save :invalidate_cache
7
+
8
+ def self.get(name, default = nil, description = nil)
9
+ obj = _get(name, default, description)
10
+ if obj.bool?
11
+ result = obj.value.to_i == 1
12
+ elsif obj.choose?
13
+ result = obj.value.split('|').map{|s| s.strip }
14
+ else
15
+ result = obj.value
16
+ end
17
+ result
18
+ end
19
+
20
+ def self.set(name, default = nil, description = nil)
21
+ obj = self.obj(name, default, description)
22
+ if obj.value.to_s != default.to_s
23
+ obj.value = default
24
+ obj.save
25
+ end
26
+ obj.value
27
+ end
28
+
29
+ def self.obj(name, default = nil, description = nil)
30
+ _get(name, default, description)
31
+ end
32
+
33
+ def ckeditor?
34
+ name=~/^doc_(.*?)$/
35
+ end
36
+
37
+ def bool?
38
+ name=~/^bool_(.*?)$/
39
+ end
40
+
41
+ def choose?
42
+ name=~/^choose_(.*?)$/
43
+ end
44
+
45
+
46
+ def self.invalidate_cache
47
+ @application_settings = nil
48
+ end
49
+
50
+ private
51
+
52
+ def self.get_all
53
+ @application_settings = {}
54
+ all.each {|i| @application_settings[i.name] = i }
55
+ @application_settings
56
+ end
57
+
58
+ def self._get(name, default = nil, description = nil)
59
+ @application_settings ||= get_all
60
+ if !@application_settings.key?(name)
61
+ self._set(name, default, description)
62
+ get_all
63
+ end
64
+ @application_settings[name]
65
+ end
66
+
67
+ def self._set(name, default = nil, description = nil)
68
+ default = (default == 1 || default == true ? 1 : 0) if name=~/^bool_(.*?)$/
69
+ if name=~/^choose_(.*?)$/
70
+ default = default.join('|') if default.is_a? Array
71
+ default = default.values.join('|') if default.is_a? Hash
72
+ end
73
+
74
+ Confit.create(:name => name, :value => default, :description => description.nil? ? name : description)
75
+ end
76
+
77
+ def invalidate_cache
78
+ Confit.invalidate_cache
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,6 @@
1
+ module Confit
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Confit
4
+ end
5
+
6
+ end
@@ -0,0 +1,3 @@
1
+ module Confit
2
+ VERSION = "0.0.1"
3
+ end
data/lib/confit.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "confit/engine"
2
+
3
+ module Confit
4
+
5
+ end
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class ConfitGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ def self.source_root
8
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
9
+ end
10
+
11
+ def self.next_migration_number(dirname)
12
+ if ActiveRecord::Base.timestamped_migrations
13
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
14
+ else
15
+ "%.3d" % (current_migration_number(dirname) + 1)
16
+ end
17
+ end
18
+
19
+ def create_migration_file
20
+ migration_template 'migration.rb', 'db/migrate/create_confits.rb'
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ class CreateConfits < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :confits do |t|
4
+ t.string :name
5
+ t.string :description
6
+ t.text :value
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :confits
13
+ end
14
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ConfitTest < ActiveSupport::TestCase
4
+ test "truth" do
5
+ assert_kind_of Module, Confit
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require 'test_helper'
2
+
3
+ class NavigationTest < ActionDispatch::IntegrationTest
4
+ fixtures :all
5
+
6
+ # test "the truth" do
7
+ # assert true
8
+ # end
9
+ end
10
+
@@ -0,0 +1,15 @@
1
+ # Configure Rails Environment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ Rails.backtrace_cleaner.remove_silencers!
8
+
9
+ # Load support files
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
11
+
12
+ # Load fixtures from the engine
13
+ if ActiveSupport::TestCase.method_defined?(:fixture_path=)
14
+ ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
15
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: confit_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - TEMKiN
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.12
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.12
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Keeps your configuration in simple way
47
+ email:
48
+ - aa.temkin@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - app/models/confit/confit.rb
54
+ - lib/confit/engine.rb
55
+ - lib/confit/version.rb
56
+ - lib/generators/confit/confit_generator.rb
57
+ - lib/generators/confit/templates/migration.rb
58
+ - lib/confit.rb
59
+ - MIT-LICENSE
60
+ - Rakefile
61
+ - README.rdoc
62
+ - test/integration/navigation_test.rb
63
+ - test/test_helper.rb
64
+ - test/confit_test.rb
65
+ homepage: https://github.com/temkin/confit
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.25
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Simple gem to keep configuration
89
+ test_files:
90
+ - test/integration/navigation_test.rb
91
+ - test/test_helper.rb
92
+ - test/confit_test.rb