Class: Sghtmltopdf::Configuration
- Inherits:
-
Object
- Object
- Sghtmltopdf::Configuration
- 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
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#apply_defaults(defaults) ⇒ Object
既定値を流し込む。Railtieが Rails向けの既定値を入れるのに使う。 明示的に設定された値より弱い(順序に関係なく
[]=が勝つ)。. -
#initialize(options = {}) ⇒ Configuration
constructor
A new instance of Configuration.
-
#method_missing(name, *args) ⇒ Object
c.page_size = "A4"とc.page_sizeを受ける。. - #respond_to_missing?(_name, _include_private = false) ⇒ Boolean
- #to_h(with_defaults: true) ⇒ Object
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)と、Railtieなどが流し込んだ既定値 # (@defaults)は分けて持つ。読み出しは常に@optionsが勝つので、 # イニシャライザの実行順に依存しない。 @defaults = {} .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
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
39 40 41 |
# File 'lib/sghtmltopdf/configuration.rb', line 39 def to_h(with_defaults: true) with_defaults ? @defaults.merge(@options) : @options.dup end |