Class: CrazyDoll::Config::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/crazy_doll/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, config = {}) ⇒ Instance

Returns a new instance of Instance.



73
74
75
76
77
# File 'lib/crazy_doll/config.rb', line 73

def initialize(owner, config={})
  @config = config.empty? ? {} : config
  @owner  = owner
  @config.keys.each { |k| create_key(k) }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/crazy_doll/config.rb', line 102

def method_missing(name, *args, &block)
  if name.to_s.match(/=$/) and args.size == 1
    key = name.to_s.gsub(/=$/,'').to_sym
    puts "!!! key #{key} missing, but creating !!!"
    @config[key] = CrazyDoll::Config.parse(@owner, key, 'created by method_missing', args[0])
    create_key(key)
    @config[key]
  elsif name.to_s.match(/\?$/) and self.respond_to?((k=name.to_s.gsub(/\?$/,'')))
    puts "!!! method #{name} missing, creating it !!!"
    self.instance_eval <<-EVAL
      def #{name}
        @config[:#{k}]
      end
    EVAL
    send(name)
  else
    puts "!!! method #{name} missing !!!"
    nil
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



72
73
74
# File 'lib/crazy_doll/config.rb', line 72

def config
  @config
end

#ownerObject (readonly)

Returns the value of attribute owner.



72
73
74
# File 'lib/crazy_doll/config.rb', line 72

def owner
  @owner
end

Instance Method Details

#register(owner, key, description, value) ⇒ Object



79
80
81
82
83
84
# File 'lib/crazy_doll/config.rb', line 79

def register(owner, key, description, value)
  k = key.to_sym
  return false if @config[k]
  @config[k] = CrazyDoll::Config.parse(@owner, key, description, value)
  create_key(k)
end