Class: Settings
- Inherits:
-
Object
- Object
- Settings
- Defined in:
- lib/zmb/settings.rb
Instance Attribute Summary collapse
-
#directory ⇒ Object
Returns the value of attribute directory.
Instance Method Summary collapse
- #get(object, name, default = nil) ⇒ Object
-
#initialize(directory) ⇒ Settings
constructor
A new instance of Settings.
- #save(key, instance) ⇒ Object
- #setting(key) ⇒ Object
- #setting_path(key) ⇒ Object
Constructor Details
#initialize(directory) ⇒ Settings
Returns a new instance of Settings.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/zmb/settings.rb', line 13 def initialize(directory) if not File.exist?(directory) then FileUtils.makedirs(directory) end if not File.directory?(directory) and not File.owned?(directory) then raise end @directory = directory end |
Instance Attribute Details
#directory ⇒ Object
Returns the value of attribute directory.
11 12 13 |
# File 'lib/zmb/settings.rb', line 11 def directory @directory end |
Instance Method Details
#get(object, name, default = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/zmb/settings.rb', line 37 def get(object, name, default=nil) s = setting(object) if s.respond_to?('has_key?') and s.has_key?(name) then s[name] else default end end |
#save(key, instance) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/zmb/settings.rb', line 47 def save(key, instance) f = File.open setting_path(key), 'w' s = instance s = {} if instance.class != Hash s = instance.settings if instance.respond_to?('settings') s['plugin'] = instance.plugin if instance.respond_to?('plugin') f.write s.to_json f.close end |
#setting(key) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/zmb/settings.rb', line 29 def setting(key) begin JSON.parse(File.read(setting_path(key))) rescue {} end end |
#setting_path(key) ⇒ Object
25 26 27 |
# File 'lib/zmb/settings.rb', line 25 def setting_path(key) File.join(@directory, key.gsub('/', '_') + '.json') end |