Class: Confo::Config

Inherits:
Object
  • Object
show all
Includes:
OptionsManager, SubconfigsManager
Defined in:
lib/confo/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(settings = {}, &block) ⇒ Config

Returns a new instance of Config.



10
11
12
13
14
# File 'lib/confo/config.rb', line 10

def initialize(settings = {}, &block)
  @settings = settings
  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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/confo/config.rb', line 82

def method_missing(name, *args, &block)
  case args.size
    when 0
      if block
        # Wants to configure subconfig:
        #   object.description {  }
        subconfig(name, &block)

      # Wants to access boolean option:
      #   object.property?
      elsif name =~ /^(\w+)\?$/
        !!get($1)

      else
        # Wants one of the following:
        #   - access subconfig
        #   - access option
        subconfig_exists?(name) ? subconfig(name) : option(strip_assignment(name))
      end

    when 1
      arg = args.first

      # Wants to test option value:
      #   object.property?(:value) => options[:property] == :value
      if name =~ /^(\w+)\?$/
        get($1) == arg

      # Wants to access collection:
      #   object.properties :id {  }
      elsif (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(strip_assignment(name), arg)
      end

    else
      option(strip_assignment(name), *args)
  end
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



8
9
10
# File 'lib/confo/config.rb', line 8

def settings
  @settings
end

Instance Method Details

#configure(*args, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
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
# File 'lib/confo/config.rb', line 44

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
        options         = arg1_hash ? arg2 : arg3
        subconfig(subconfig_name, options, 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

#deep_dup(config_settings = {}) ⇒ Object



40
41
42
# File 'lib/confo/config.rb', line 40

def deep_dup(config_settings = {})
  with_new_settings(config_settings)
end

#dup(config_settings = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/confo/config.rb', line 28

def dup(config_settings = {})
  self.class.new(settings.merge(config_settings)).tap do |new_config|
    if var = @options_storage
      new_config.instance_variable_set(:@options_storage, var)
    end

    if var = @subconfig_instances
      new_config.instance_variable_set(:@subconfig_instances, var)
    end
  end
end

#to_hashObject



127
128
129
# File 'lib/confo/config.rb', line 127

def to_hash
  options.merge!(subconfigs).to_hash
end

#with_new_settings(new_settings) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/confo/config.rb', line 16

def with_new_settings(new_settings)
  self.class.new(settings.merge(new_settings)).tap do |new_config|
    if var = @options_storage
      new_config.instance_variable_set(:@options_storage, var.deep_dup)
    end

    if var = @subconfig_instances
      new_config.instance_variable_set(:@subconfig_instances, var.with_new_settings(new_settings))
    end
  end
end