Class: SiteSetting

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SiteSetting

Returns a new instance of SiteSetting.



26
27
28
29
# File 'lib/sitesetting.rb', line 26

def initialize(path)
  @match_values = {}
  @yaml_setting = YAML.load_file(path)
end

Class Method Details

.load_file(path) ⇒ Object



10
11
12
# File 'lib/sitesetting.rb', line 10

def self.load_file(path)
  new(path)
end

Instance Method Details

#[](key) ⇒ Object



14
15
16
# File 'lib/sitesetting.rb', line 14

def [](key)
  replace_group_values(key)
end

#[]=(key, value) ⇒ Object



18
19
20
# File 'lib/sitesetting.rb', line 18

def []=(key, value)
  @match_values[key] = value
end

#clearObject



22
23
24
# File 'lib/sitesetting.rb', line 22

def clear
  @match_values.clear
end

#do_replace(dest, option_values) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sitesetting.rb', line 80

def do_replace(dest, option_values)
  return dest unless dest.respond_to?(:gsub)
  values = @yaml_setting.merge(@match_values).merge(option_values)
  dest.gsub(/\\\\k<(.+?)>/) do |match|
    value = values[$1]
    if value
      value.gsub(/\\\\k<(.+?)>/) do
        replace_group_values($1, option_values)
      end
    else
      match
    end
  end
end

#is_container?(value) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/sitesetting.rb', line 64

def is_container?(value)
  value.is_a?(Hash) || value.is_a?(Narou::API)
end

#matched?(key) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sitesetting.rb', line 31

def matched?(key)
  @match_values[key]
end

#multi_match(source, *keys) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/sitesetting.rb', line 35

def multi_match(source, *keys)
  match_data = nil
  keys.each do |key|
    setting_value = self[key] or next
    [*setting_value].each do |value|
      match_data = source.match(/#{value}/m)
      if match_data
        @match_values[key] = value       # yamlのキーでもmatch_valuesに設定しておくが、
        update_match_values(match_data)  # ←ここで同名のグループ名が定義されていたら上書きされるので注意
                                         # 例えば、title: <title>(?<title>.+?)</title> と定義されていた場合、
                                         # @match_values["title"] には (?<title>.+?) 部分の要素が反映される
        break
      end
    end
  end
  match_data
end

#multi_match_once(source, *keys) ⇒ Object



53
54
55
56
# File 'lib/sitesetting.rb', line 53

def multi_match_once(source, *keys)
  clear
  multi_match(source, *keys)
end

#replace_group_values(key, option_values = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sitesetting.rb', line 68

def replace_group_values(key, option_values = {})
  buf = option_values[key] || @match_values[key] || @yaml_setting[key]
  return buf if is_container?(buf)
  if buf.is_a?(Array)
    buf.map do |dest|
      do_replace(dest, option_values)
    end
  else
    do_replace(buf, option_values)
  end
end

#update_match_values(match_data) ⇒ Object



58
59
60
61
62
# File 'lib/sitesetting.rb', line 58

def update_match_values(match_data)
  match_data.names.each do |name|
    @match_values[name] = match_data[name] || ""
  end
end