Class: Strada::ConfigStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/strada/config_struct.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

方法反射



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/strada/config_struct.rb', line 45

def method_missing(name, *args, &block)
  name = name.to_s
  name = args.shift if name[0..1] == "[]" # strada.cfg['foo']
  arg  = args.first

  if name[-1..-1] == "?" # strada.cfg.foo.bar?
    if @cfg.has_key? name[0..-2]
      @cfg[name[0..-2]]
    else
      nil
    end
  elsif name[-1..-1] == "=" # strada.cfg.foo.bar = 'quux'
    _config_set name[0..-2], arg
  else
    _config_get name, arg # strada.cfg.foo.bar
  end
end

Instance Method Details

#_config_to_hashObject

将配置信息转换为 HASH 对象



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/strada/config_struct.rb', line 6

def _config_to_hash
  hash = {}
  @cfg.each do |key, value|
    if value.class == ConfigStruct
      value = value._config_to_hash
    end
    key       = key.to_s if @key_to_s
    hash[key] = value
  end
  hash
end

#each(&block) ⇒ Object

调用配置 each 方法执行代码块



24
25
26
# File 'lib/strada/config_struct.rb', line 24

def each(&block)
  @cfg.each(&block)
end

#empty?Boolean

是否为空配置

Returns:

  • (Boolean)


19
20
21
# File 'lib/strada/config_struct.rb', line 19

def empty?
  @cfg.empty?
end

#has_key?(key) ⇒ Boolean

判断配置是否包含某个属性

Returns:

  • (Boolean)


34
35
36
# File 'lib/strada/config_struct.rb', line 34

def has_key?(key)
  @cfg.has_key? key
end

#keysObject

配置的相关属性



29
30
31
# File 'lib/strada/config_struct.rb', line 29

def keys
  @cfg.keys
end