Module: Narou::ServerHelpers

Defined in:
lib/web/server_helpers.rb

Overview

rubocop:disable Style/ClassAndModuleChildren

Constant Summary collapse

RELOAD_TIMING_DEFAULT =
"every"

Instance Method Summary collapse

Instance Method Details

#build_ruby_versionObject

Rubyバージョンを構築



33
34
35
36
37
38
39
40
# File 'lib/web/server_helpers.rb', line 33

def build_ruby_version
  begin
    `"#{RbConfig.ruby}" -v`.strip
  rescue
    config = RbConfig::CONFIG
    "ruby #{RUBY_VERSION}p#{config["PATCHLEVEL"]} [#{RUBY_PLATFORM}]"
  end
end

#concurrency_push(&block) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/web/server_helpers.rb', line 143

def concurrency_push(&block)
  if Narou.concurrency_enabled?
    yield
  else
    Narou::WebWorker.push(&block)
  end
end

#convert_boolean_to_on_off(bool) ⇒ Object

nil true false を nil on off という文字列に変換



71
72
73
74
75
76
77
78
79
80
# File 'lib/web/server_helpers.rb', line 71

def convert_boolean_to_on_off(bool)
  case bool
  when TrueClass
    "on"
  when FalseClass
    "off"
  else
    "nil"
  end
end

#convert_on_off_to_boolean(str) ⇒ Object

フォーム情報の真偽値データを実際のデータに変換



57
58
59
60
61
62
63
64
65
66
# File 'lib/web/server_helpers.rb', line 57

def convert_on_off_to_boolean(str)
  case str
  when "on"
    true
  when "off"
    false
  else
    nil
  end
end

#decorate_exclusion_tags(tags) ⇒ Object

タグをHTMLで装飾する(除外タグ指定用)



24
25
26
27
28
# File 'lib/web/server_helpers.rb', line 24

def decorate_exclusion_tags(tags)
  tags.sort.map do |tag|
    %!<span class="tag label label-#{Command::Tag.get_color(tag)}" data-exclusion-tag="#{escape_html(tag)}">^tag:#{escape_html(tag)}</span>!
  end.join(" ")
end

#decorate_tags(tags) ⇒ Object

タグをHTMLで装飾する



15
16
17
18
19
# File 'lib/web/server_helpers.rb', line 15

def decorate_tags(tags)
  tags.sort.map do |tag|
    %!<span class="tag label label-#{Command::Tag.get_color(tag)}" data-tag="#{escape_html(tag)}">#{escape_html(tag)}</span>!
  end.join(" ")
end

#embed_concurrency_enabledObject



137
138
139
140
141
# File 'lib/web/server_helpers.rb', line 137

def embed_concurrency_enabled
  <<~HTML
    <input type="hidden" id="concurrency-enabled" value="#{Narou.concurrency_enabled?}">
  HTML
end

#h(text) ⇒ Object

HTMLエスケープヘルパー



85
86
87
# File 'lib/web/server_helpers.rb', line 85

def h(text)
  Rack::Utils.escape_html(text)
end

#notepad_text_pathObject



104
105
106
# File 'lib/web/server_helpers.rb', line 104

def notepad_text_path
  File.join(Narou.local_setting_dir, "notepad.txt")
end

#partial(template, *args) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/web/server_helpers.rb', line 123

def partial(template, *args)
  template_file_name = "_#{template}".intern
  options = args.last.is_a?(Hash) ? args.pop : {}
  options[:layout] = false
  collection = options.delete(:collection)
  if collection
    collection.inject([]) do |buffer, member|
      buffer << haml(template_file_name, options.merge(locals: { template => member }))
    end.join("\n")
  else
    haml(template_file_name, options)
  end
end

#query_to_boolean(value, default: false) ⇒ Object



108
109
110
111
112
113
114
115
116
117
# File 'lib/web/server_helpers.rb', line 108

def query_to_boolean(value, default: false)
  case value
  when "1", 1, "true", true
    true
  when "0", 0, "false", false
    false
  else
    default
  end
end

#select_valid_novel_ids(ids) ⇒ Object

有効な novel ID だけの配列を生成する ID が指定されなかったか、1件も存在しない場合は nil を返す



46
47
48
49
50
51
52
# File 'lib/web/server_helpers.rb', line 46

def select_valid_novel_ids(ids)
  return nil unless ids.kind_of?(Array)
  result = ids.select do |id|
    id =~ /^\d+$/
  end
  result.empty? ? nil : result
end

#table_reload_timingObject



119
120
121
# File 'lib/web/server_helpers.rb', line 119

def table_reload_timing
  Inventory.load("local_setting")["webui.table.reload-timing"] || RELOAD_TIMING_DEFAULT
end

#value_to_msg(value) ⇒ Object

与えられたデータが真偽値だった場合、設定画面用に「はい」「いいえ」に変換する 真偽値ではなかった場合、そのまま返す



93
94
95
96
97
98
99
100
101
102
# File 'lib/web/server_helpers.rb', line 93

def value_to_msg(value)
  case value
  when TrueClass
    "はい"
  when FalseClass
    "いいえ"
  else
    value
  end
end