Class: Confo::Config
- Inherits:
-
Object
- Object
- Confo::Config
- Includes:
- OptionsManager, SubconfigsManager
- Defined in:
- lib/confo/config.rb
Instance Attribute Summary collapse
-
#behaviour_options ⇒ Object
readonly
Returns the value of attribute behaviour_options.
Instance Method Summary collapse
- #configure(*args, &block) ⇒ Object
-
#initialize(behaviour_options = {}, &block) ⇒ Config
constructor
A new instance of Config.
- #method_missing(name, *args, &block) ⇒ Object
- #to_hash ⇒ Object
Methods included from SubconfigsManager
#subconfig, #subconfig_exists?, #subconfigs
Methods included from OptionsManager
#get, #keys, #option, #options, #result_of, #set, #set?, #set_at_first, #unset, #values
Constructor Details
#initialize(behaviour_options = {}, &block) ⇒ Config
Returns a new instance of Config.
10 11 12 13 14 |
# File 'lib/confo/config.rb', line 10 def initialize( = {}, &block) = preconfigure configure(&block) if block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/confo/config.rb', line 54 def method_missing(name, *args, &block) case args.size when 0 if block # Wants to configure subconfig: # object.description { } subconfig(name, &block) else # Wants one of the following: # - access subconfig # - access option subconfig_exists?(name) ? subconfig(name) : option(normalize_option(name)) end when 1 arg = args.first # Wants to access collection: # object.properties :id { } if (arg.is_a?(String) || arg.is_a?(Symbol)) && subconfig_exists?(name) subconfig(name.to_s.pluralize, arg, &block) else # Wants to access option: # object.cache = :none # object.cache :none option(normalize_option(name), arg) end else option(normalize_option(name), *args) end end |
Instance Attribute Details
#behaviour_options ⇒ Object (readonly)
Returns the value of attribute behaviour_options.
8 9 10 |
# File 'lib/confo/config.rb', line 8 def end |
Instance Method Details
#configure(*args, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/confo/config.rb', line 16 def configure(*args, &block) case args.size # Current config configuration: # object.configure { } when 0 instance_eval(&block) if block self when 1, 2, 3 arg1, arg2, arg3 = args arg1_hash = arg1.kind_of?(Hash) # Hash-based collection syntax: # object.configure(property: :id) { } # object.configure(property: :id, {option: :value}) { } # # Full definition syntax: # object.configure(:property, :id) { } # object.configure(:property, :id, {option: :value}) { } if arg1_hash || (args.size == 2 && arg2.kind_of?(Hash) == false) subconfig_name = (arg1_hash ? arg1.keys.first : arg1).to_s.pluralize config_id = arg1_hash ? arg1.values.first : arg2 = arg1_hash ? arg2 : arg3 subconfig(subconfig_name, , fallback_class_name: 'Confo::Collection') .configure(config_id, &block) else # Subconfig configuration: # object.configure(:description) # object.configure(:description, {option: :value}) subconfig(arg1, arg2, &block) end else self end end |
#to_hash ⇒ Object
89 90 91 |
# File 'lib/confo/config.rb', line 89 def to_hash {}.merge!(.to_hash).merge!(subconfigs.to_hash) end |