easy_settings 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +163 -3
- data/lib/easy_settings.rb +8 -0
- data/lib/easy_settings_version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b576c4cc8f7df5949f577776925dd2cf736f25b9
|
4
|
+
data.tar.gz: f752df3e59269a8fc30ab5461452d236ecc0537a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a07f2b0934754767fea381bc86e9ee33e75be14395cf87ed1fa98bde1931f0cbfa3000ea3910f0c19512e3f66737537cd7c152251efaf804580b9278f8b27b4d
|
7
|
+
data.tar.gz: aef00e04a6612a1055ef9c494fe1b49a04a1c24a2a90288f183271545c3277c29ccf404d69ea659a0cd8373ea89939d61b0f2b939a7ed1cea14aefa50a954052
|
data/README.md
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
# EasySettings
|
2
|
-
|
3
|
-
TODO: Write a gem description
|
2
|
+
EasySettings is a simple manager of setting constants.
|
4
3
|
|
5
4
|
## Installation
|
6
5
|
|
@@ -19,8 +18,169 @@ Or install it yourself as:
|
|
19
18
|
$ gem install easy_settings
|
20
19
|
|
21
20
|
## Usage
|
21
|
+
EasySettings can load YAML file and understand ERB format.
|
22
|
+
|
23
|
+
### Basic usage
|
24
|
+
Most simple usage is to put your settings file `settings.yml` to your application's root directory.
|
25
|
+
|
26
|
+
`settings.yml` or `config/settings.yml`:
|
27
|
+
|
28
|
+
```yaml
|
29
|
+
app_name: EasySettingsSample
|
30
|
+
users:
|
31
|
+
nownabe:
|
32
|
+
gender: male
|
33
|
+
like: ["Angra", "Helloween", "SEX MACHINEGUNS", "μ's"]
|
34
|
+
timestamp: <%= Time.now.to_s %>
|
35
|
+
```
|
36
|
+
|
37
|
+
And your application can use EasySettings:
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
require "easy_settings"
|
41
|
+
|
42
|
+
EasySettings.app_name #=> "EasySettingsSample"
|
43
|
+
EasySettings["app_name"] #=> "EasySettingsSample"
|
44
|
+
EasySettings[:app_name] #=> "EasySettingsSample"
|
45
|
+
|
46
|
+
EasySettings.users
|
47
|
+
#=> {"nownabe"=>{"gender"=>"male", "like"=>["Angra", "Helloween", "SEX MACHINEGUNS", "μ's"]}}
|
48
|
+
EasySettings.users[:nownabe][:gender]
|
49
|
+
#=> "male"
|
50
|
+
EasySettings.users.nownabe.like
|
51
|
+
#=> ["Angra", "Helloween", "SEX MACHINEGUNS", "μ's"]
|
52
|
+
|
53
|
+
EasySettings.timestamp
|
54
|
+
#=> 2015-03-07 00:11:28 +0900
|
55
|
+
```
|
56
|
+
|
57
|
+
You can set value to any key.
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
EasySettings.app_name = "MyApp" #=> "MyApp"
|
61
|
+
EasySettings["app_name"] #=> "MyApp"
|
62
|
+
EasySettings[:app_name] #=> "MyApp"
|
63
|
+
|
64
|
+
EasySettings["app_name"] = "MyApp2" #=> "MyApp2"
|
65
|
+
EasySettings.app_name #=> "MyApp2"
|
66
|
+
EasySettings[:app_name] #=> "MyApp2"
|
67
|
+
```
|
68
|
+
|
69
|
+
If you want to use other settings file, you can use any other file with `EasySettings.source_file =`.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
EasySettings.source_file = Rails.root.join("config/application.yml").to_s
|
73
|
+
```
|
74
|
+
|
75
|
+
### Namespace
|
76
|
+
You can use namespace.
|
77
|
+
|
78
|
+
Settings YAML file:
|
79
|
+
|
80
|
+
```yaml
|
81
|
+
defaults: &defaults
|
82
|
+
app_name: NamespaceSample
|
83
|
+
endpoint: http://backend-dev/api/v1
|
84
|
+
|
85
|
+
development:
|
86
|
+
<<: *defaults
|
87
|
+
apikey: DEVFOOBAR123
|
88
|
+
secret: DEVBAZQUX789
|
89
|
+
|
90
|
+
production:
|
91
|
+
<<: *defaults
|
92
|
+
endpoint: http://backend/api/v1
|
93
|
+
apikey: PRDFOOBAR123
|
94
|
+
secret: PRDBAZQUX789
|
95
|
+
```
|
96
|
+
|
97
|
+
Using EasySettings with `namespace =`:
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
Rails.env
|
101
|
+
#=> "production"
|
102
|
+
|
103
|
+
EasySettings.namespace = Rails.env
|
104
|
+
#=> "production"
|
105
|
+
|
106
|
+
EasySettings.app_name
|
107
|
+
#=> "NamespaceSample"
|
22
108
|
|
23
|
-
|
109
|
+
EasySettings.endpoint
|
110
|
+
#=> "http://backend/api/v1"
|
111
|
+
```
|
112
|
+
|
113
|
+
### Default value
|
114
|
+
You can set default value.
|
115
|
+
Default value will be set with key when EasySettings doesn't have key.
|
116
|
+
|
117
|
+
```ruby
|
118
|
+
EasySettings.has_key?(:foo)
|
119
|
+
#=> false
|
120
|
+
|
121
|
+
EasySettings.foo
|
122
|
+
#=> nil
|
123
|
+
|
124
|
+
EasySettings.foo("bar")
|
125
|
+
#=> "bar"
|
126
|
+
|
127
|
+
EasySettings.foo
|
128
|
+
#=> "bar"
|
129
|
+
|
130
|
+
EasySettings.has_key?(:foo)
|
131
|
+
#=> true
|
132
|
+
|
133
|
+
EasySettings.foo("baz")
|
134
|
+
#=> "bar"
|
135
|
+
```
|
136
|
+
|
137
|
+
Using nil option, you can set nil:
|
138
|
+
|
139
|
+
```ruby
|
140
|
+
EasySettings.app_name(nil)
|
141
|
+
#=> nil
|
142
|
+
EasySettings.has_key?(:app_name)
|
143
|
+
#=> false
|
144
|
+
|
145
|
+
EasySettings.app_name(nil, nil: true)
|
146
|
+
#=> nil
|
147
|
+
EasySettings.has_key?(:app_name)
|
148
|
+
#=> true
|
149
|
+
```
|
150
|
+
|
151
|
+
And EasySettings can treat nested hash:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
EasySettings.users({}).nownabe = {gender: :male}
|
155
|
+
#=> {:gender=>:male}
|
156
|
+
EasySettings.to_h
|
157
|
+
#=> {"users"=>{"nownabe"=>{"gender"=>:male}}}
|
158
|
+
```
|
159
|
+
|
160
|
+
### Reset EasySettings
|
161
|
+
```ruby
|
162
|
+
EasySettings.source_file = "new_settings.yml"
|
163
|
+
EasySettings.reload!
|
164
|
+
|
165
|
+
EasySettings.source_hash = {}
|
166
|
+
EasySettings.reload!
|
167
|
+
```
|
168
|
+
|
169
|
+
### Alias
|
170
|
+
```ruby
|
171
|
+
Config = EasySettings
|
172
|
+
Config.app_name = "AliasSample"
|
173
|
+
```
|
174
|
+
|
175
|
+
Or using class that inherit EasySettings:
|
176
|
+
|
177
|
+
```ruby
|
178
|
+
class Config < EasySettings
|
179
|
+
self.namespace = Rails.env
|
180
|
+
end
|
181
|
+
|
182
|
+
Config.endpoint
|
183
|
+
```
|
24
184
|
|
25
185
|
## Contributing
|
26
186
|
|
data/lib/easy_settings.rb
CHANGED