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.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/zmb/settings.rb', line 11 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.
9 10 11 |
# File 'lib/zmb/settings.rb', line 9 def directory @directory end |
Instance Method Details
#get(object, name, default = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/zmb/settings.rb', line 35 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
45 46 47 48 49 50 51 52 53 |
# File 'lib/zmb/settings.rb', line 45 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
27 28 29 30 31 32 33 |
# File 'lib/zmb/settings.rb', line 27 def setting(key) begin JSON.parse(File.read(setting_path(key))) rescue {} end end |
#setting_path(key) ⇒ Object
23 24 25 |
# File 'lib/zmb/settings.rb', line 23 def setting_path(key) File.join(@directory, key.gsub('/', '_') + '.json') end |