Class: Asetus
- Inherits:
-
Object
- Object
- Asetus
- Defined in:
- lib/asetus.rb,
lib/asetus/adapter/json.rb,
lib/asetus/adapter/toml.rb,
lib/asetus/adapter/yaml.rb,
lib/asetus/configstruct.rb
Overview
Defined Under Namespace
Classes: Adapter, ConfigStruct
Constant Summary collapse
- CONFIG_FILE =
'config'
Instance Attribute Summary collapse
-
#cfg ⇒ Object
readonly
Returns the value of attribute cfg.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#system ⇒ Object
Returns the value of attribute system.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
-
#create(opts = {}) ⇒ boolean
True if config didn’t exist and was created, false if config already exists.
- #from_json(json) ⇒ Object
- #from_toml(toml) ⇒ Object
- #from_yaml(yaml) ⇒ Object
-
#load(level = :all) ⇒ void
When this is called, by default :system and :user are loaded from filesystem and merged with defefault, so that user overrides system which overrides default.
- #save(level = :user) ⇒ void
- #to_json(config) ⇒ Object
- #to_toml(config) ⇒ Object
- #to_yaml(config) ⇒ Object
Instance Attribute Details
#cfg ⇒ Object (readonly)
Returns the value of attribute cfg.
27 28 29 |
# File 'lib/asetus.rb', line 27 def cfg @cfg end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
27 28 29 |
# File 'lib/asetus.rb', line 27 def default @default end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
27 28 29 |
# File 'lib/asetus.rb', line 27 def file @file end |
#system ⇒ Object
Returns the value of attribute system.
28 29 30 |
# File 'lib/asetus.rb', line 28 def system @system end |
#user ⇒ Object
Returns the value of attribute user.
28 29 30 |
# File 'lib/asetus.rb', line 28 def user @user end |
Class Method Details
.cfg(*args) ⇒ Object
31 32 33 |
# File 'lib/asetus.rb', line 31 def cfg *args new(*args).cfg end |
Instance Method Details
#create(opts = {}) ⇒ boolean
Returns true if config didn’t exist and was created, false if config already exists.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/asetus.rb', line 73 def create opts={} src = opts.delete :source src ||= :default dst = opts.delete :destination dst ||= :user no_config = false no_config = true if @system.empty? and @user.empty? if no_config src = instance_variable_get '@' + src.to_s instance_variable_set('@'+dst.to_s, src.dup) save dst load if opts.delete :load end no_config end |
#from_json(json) ⇒ Object
7 8 9 |
# File 'lib/asetus/adapter/json.rb', line 7 def from_json json Adapter::JSON.from json end |
#from_toml(toml) ⇒ Object
7 8 9 |
# File 'lib/asetus/adapter/toml.rb', line 7 def from_toml toml Adapter::TOML.from toml end |
#from_yaml(yaml) ⇒ Object
7 8 9 |
# File 'lib/asetus/adapter/yaml.rb', line 7 def from_yaml yaml Adapter::YAML.from yaml end |
#load(level = :all) ⇒ void
This method returns an undefined value.
When this is called, by default :system and :user are loaded from filesystem and merged with defefault, so that user overrides system which overrides default
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/asetus.rb', line 42 def load level=:all if level == :default or level == :all @cfg = merge @cfg, @default end if level == :system or level == :all @system = load_cfg @sysdir @cfg = merge @cfg, @system end if level == :user or level == :all @user = load_cfg @usrdir @cfg = merge @cfg, @user end end |
#save(level = :user) ⇒ void
This method returns an undefined value.
58 59 60 61 62 63 64 |
# File 'lib/asetus.rb', line 58 def save level=:user if level == :user save_cfg @usrdir, @user elsif level == :system save_cfg @sysdir, @system end end |
#to_json(config) ⇒ Object
3 4 5 |
# File 'lib/asetus/adapter/json.rb', line 3 def to_json config Adapter::JSON.to config._asetus_to_hash end |