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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5e7a4cd755414c8b9ac73537415a9354c216cfbe
4
- data.tar.gz: bb8c676c3225d497595cc0524566c268e97fb444
3
+ metadata.gz: b576c4cc8f7df5949f577776925dd2cf736f25b9
4
+ data.tar.gz: f752df3e59269a8fc30ab5461452d236ecc0537a
5
5
  SHA512:
6
- metadata.gz: 4e84cede6e0deb01912732e031d493cba0fb2f33cbfc45d77ee9a36496e20e969fe5d6172780d760599b04e9b47bc77606310b2488fa724a9ede9b904121646f
7
- data.tar.gz: 5c84841fb7e8c092a80ebaac569fbc7c3079e9d960b03a77773941b8baeefee7e6dcdccc47c4e4920a013250d11101270ff57b59f8af2273504227467d03982d
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
- TODO: Write usage instructions here
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
@@ -28,6 +28,14 @@ class EasySettings < Hashie::Mash
28
28
  load!
29
29
  end
30
30
 
31
+ def [](key)
32
+ instance[key]
33
+ end
34
+
35
+ def []=(key, val)
36
+ instance[key] = val
37
+ end
38
+
31
39
  private
32
40
 
33
41
  def _source
@@ -1,3 +1,3 @@
1
1
  module EasySettingsVersion
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_settings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - nownabe