app-config 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/app-config.rb +4 -0
- data/lib/app-config/app-config.rb +84 -0
- data/lib/app-config/errors.rb +6 -0
- data/lib/app-config/processor.rb +38 -0
- data/lib/app-config/version.rb +3 -0
- metadata +86 -0
data/lib/app-config.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
module AppConfig
|
2
|
+
extend AppConfig::Processor
|
3
|
+
|
4
|
+
FORMATS = ['string', 'array', 'hash', 'boolean']
|
5
|
+
@@options = {}
|
6
|
+
@@records = {}
|
7
|
+
|
8
|
+
# Configure app config
|
9
|
+
def self.configure(opts={})
|
10
|
+
@@options[:model] = opts[:model] || Setting
|
11
|
+
@@options[:key] = opts[:key] || 'keyname'
|
12
|
+
@@options[:value] = opts[:value] || 'value'
|
13
|
+
@@options[:format] = opts[:format] || 'value_format'
|
14
|
+
end
|
15
|
+
|
16
|
+
# Load and process application settings
|
17
|
+
def self.load
|
18
|
+
@@records = fetch
|
19
|
+
end
|
20
|
+
|
21
|
+
# Delete all settings
|
22
|
+
def self.flush
|
23
|
+
@@records.clear
|
24
|
+
end
|
25
|
+
|
26
|
+
# Safe method to reload new settings
|
27
|
+
def self.reload
|
28
|
+
records = fetch rescue nil
|
29
|
+
@@records = records || {}
|
30
|
+
end
|
31
|
+
|
32
|
+
# Manually set (or add) a key
|
33
|
+
def self.set_key(keyname, value, format='string')
|
34
|
+
raise InvalidKeyName, "Invalid key name: #{keyname}" if self.respond_to?(keyname)
|
35
|
+
@@records[keyname] = process(value, format)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Returns all configuration keys
|
39
|
+
def self.keys
|
40
|
+
@@records.keys
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns true if there are no settings available
|
44
|
+
def self.empty?
|
45
|
+
@@records.empty?
|
46
|
+
end
|
47
|
+
|
48
|
+
# Get configuration option
|
49
|
+
def self.[](key)
|
50
|
+
@@records[key.to_s]
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get configuration option by attribute
|
54
|
+
def self.method_missing(method, *args)
|
55
|
+
@@records[method.to_s]
|
56
|
+
end
|
57
|
+
|
58
|
+
# Returns true if configuration key exists
|
59
|
+
def self.exist?(key)
|
60
|
+
@@records.key?(key)
|
61
|
+
end
|
62
|
+
|
63
|
+
protected
|
64
|
+
|
65
|
+
# Fetch data from model
|
66
|
+
def self.fetch
|
67
|
+
raise InvalidSource, 'Model is not defined!' if @@options[:model].nil?
|
68
|
+
raise InvalidSource, 'Model was not found!' unless @@options[:model].superclass == ActiveRecord::Base
|
69
|
+
|
70
|
+
records = {}
|
71
|
+
|
72
|
+
begin
|
73
|
+
@@options[:model].send(:all).map do |c|
|
74
|
+
records[c.send(@@options[:key].to_sym)] = process(
|
75
|
+
c.send(@@options[:value].to_sym),
|
76
|
+
c.send(@@options[:format].to_sym)
|
77
|
+
)
|
78
|
+
end
|
79
|
+
records
|
80
|
+
rescue ActiveRecord::StatementInvalid => ex
|
81
|
+
raise InvalidSource, ex.message
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module AppConfig
|
2
|
+
module Processor
|
3
|
+
# Process string value
|
4
|
+
def process_string(value)
|
5
|
+
value.strip
|
6
|
+
end
|
7
|
+
|
8
|
+
# Process array of strings
|
9
|
+
def process_array(value)
|
10
|
+
value.split("\n").map { |s| s.to_s.strip }.compact.select { |s| !s.empty? }
|
11
|
+
end
|
12
|
+
|
13
|
+
# Parse boolean string
|
14
|
+
def process_boolean(value)
|
15
|
+
['true', 'on', 'yes', 'y', '1'].include?(value.to_s.downcase)
|
16
|
+
end
|
17
|
+
|
18
|
+
# Parse hash string
|
19
|
+
# value should be in the following format:
|
20
|
+
# "keyname: value, key2: value2"
|
21
|
+
def process_hash(value)
|
22
|
+
result = {}
|
23
|
+
unless value.empty?
|
24
|
+
value.split(",").each do |s|
|
25
|
+
k,v = s.split(':').compact.map { |i| i.to_s.strip }
|
26
|
+
result[k] = v.to_s
|
27
|
+
end
|
28
|
+
end
|
29
|
+
result
|
30
|
+
end
|
31
|
+
|
32
|
+
# Process data value for the format
|
33
|
+
def process(data, type)
|
34
|
+
raise InvalidType, 'Type is invalid!' unless FORMATS.include?(type)
|
35
|
+
send("process_#{type}".to_sym, data.to_s)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: app-config
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Dan Sosedoff
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-02-11 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
version: 3.0.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Flexible and simple configuration settings for your Rails/Sinatra applications
|
38
|
+
email: dan.sosedoff@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- lib/app-config.rb
|
47
|
+
- lib/app-config/version.rb
|
48
|
+
- lib/app-config/errors.rb
|
49
|
+
- lib/app-config/processor.rb
|
50
|
+
- lib/app-config/app-config.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/sosedoff/app-config
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.4.1
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Configurable application settings
|
85
|
+
test_files: []
|
86
|
+
|