Class: Sghtmltopdf::Configuration

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

Overview

グローバルな既定オプション。

Sghtmltopdf.configure do |c|
c.page_size   = "A4"
c.gothic_font = "/path/to/NotoSansJP-Regular.ttf"
end

ここで設定した値はrender/render_to_fileの引数で上書きできる (マージ順はグローバル → 呼び出し時)。

キー名の妥当性は検査しない。オプション定義はRust側(cli/options.rs)の 1箇所に集約する方針のため、未知のキーはレンダリング時にclapがUsageErrorをraiseする。

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Configuration

Returns a new instance of Configuration.



17
18
19
20
21
22
23
24
# File 'lib/sghtmltopdf/configuration.rb', line 17

def initialize(options = {})
  @options = {}
  # 明示的に設定した値(@options)と、Railtieなどが流し込んだ既定値
  # (@defaults)は分けて持つ。読み出しは常に@optionsが勝つので、
  # イニシャライザの実行順に依存しない。
  @defaults = {}
  options.each { |key, value| self[key] = value }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

c.page_size = "A4"c.page_sizeを受ける。



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/sghtmltopdf/configuration.rb', line 51

def method_missing(name, *args)
  key = name.to_s
  if key.end_with?("=")
    raise ArgumentError, "#{name}は引数1つを取ります" unless args.size == 1

    self[key.chomp("=")] = args.first
  else
    raise ArgumentError, "#{name}は引数を取りません" unless args.empty?

    self[key]
  end
end

Instance Method Details

#[](key) ⇒ Object



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

def [](key)
  key = key.to_sym
  @options.key?(key) ? @options[key] : @defaults[key]
end

#[]=(key, value) ⇒ Object



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

def []=(key, value)
  @options[key.to_sym] = value
end

#apply_defaults(defaults) ⇒ Object

既定値を流し込む。Railtieが Rails向けの既定値を入れるのに使う。 明示的に設定された値より弱い(順序に関係なく[]=が勝つ)。



45
46
47
48
# File 'lib/sghtmltopdf/configuration.rb', line 45

def apply_defaults(defaults)
  defaults.each { |key, value| @defaults[key.to_sym] = value }
  self
end

#respond_to_missing?(_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(_name, _include_private = false)
  true
end

#to_h(with_defaults: true) ⇒ Object

Parameters:

  • with_defaults (Boolean) (defaults to: true)

    流し込まれた既定値を含めるか。 HTTPサーバへ委譲するときはfalseにする。Rails向けの既定値 (base_urlallow)はローカルのファイル解決のためのもので、 サーバモードではリクエストから指定できないキーだから



39
40
41
# File 'lib/sghtmltopdf/configuration.rb', line 39

def to_h(with_defaults: true)
  with_defaults ? @defaults.merge(@options) : @options.dup
end