Class: LightningCSS::Options
- Inherits:
-
Object
- Object
- LightningCSS::Options
- Defined in:
- lib/lightningcss/options.rb,
sig/lightningcss/options.rbs
Overview
The options a call was given, on their way to the native library.
Lightning CSS reads them as JSON, so this is where a Ruby hash becomes one, and where an option nobody knows is refused. Refusing early is the point: an option the native side does not read would otherwise be accepted and quietly do nothing.
LightningCSS::Options.new(minify: true).to_json #=> "{\"minify\":true}"
Constant Summary collapse
- KNOWN =
[ :filename, :minify, :error_recovery, :targets, :css_modules, :scope ].freeze
- STYLE_ATTRIBUTE =
[ :filename, :minify, :error_recovery, :targets ].freeze
Instance Attribute Summary collapse
- #to_h ⇒ Hash[Symbol, untyped] readonly
Class Method Summary collapse
-
.serialize(options, allowed: KNOWN, subject: "a transform") ⇒ String
: (Hash[Symbol, untyped], ?allowed: Array, ?subject: String) -> String.
Instance Method Summary collapse
-
#initialize(allowed: KNOWN, subject: "a transform", **options) ⇒ Options
constructor
: (?allowed: Array, ?subject: String, **untyped) -> void.
-
#inspect ⇒ String
: () -> String.
-
#normalize(options) ⇒ Hash[Symbol, untyped]
: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped].
-
#to_json(state = nil) ⇒ String
: (?untyped) -> String.
- #validate!(names, allowed, subject) ⇒ void
Constructor Details
#initialize(allowed: KNOWN, subject: "a transform", **options) ⇒ Options
: (?allowed: Array, ?subject: String, **untyped) -> void
37 38 39 40 41 42 43 44 45 |
# File 'lib/lightningcss/options.rb', line 37 def initialize(allowed: KNOWN, subject: "a transform", **) given = .transform_keys(&:to_sym) validate!(given.keys, allowed, subject) @to_h = normalize(given).freeze freeze end |
Instance Attribute Details
#to_h ⇒ Hash[Symbol, untyped] (readonly)
29 30 31 |
# File 'lib/lightningcss/options.rb', line 29 def to_h @to_h end |
Class Method Details
.serialize(options, allowed: KNOWN, subject: "a transform") ⇒ String
: (Hash[Symbol, untyped], ?allowed: Array, ?subject: String) -> String
32 33 34 |
# File 'lib/lightningcss/options.rb', line 32 def self.serialize(, allowed: KNOWN, subject: "a transform") new(allowed: allowed, subject: subject, **).to_json end |
Instance Method Details
#inspect ⇒ String
: () -> String
53 54 55 |
# File 'lib/lightningcss/options.rb', line 53 def inspect "#<#{self.class.name} #{to_h.inspect}>" end |
#normalize(options) ⇒ Hash[Symbol, untyped]
: (Hash[Symbol, untyped]) -> Hash[Symbol, untyped]
73 74 75 76 77 78 79 80 |
# File 'lib/lightningcss/options.rb', line 73 def normalize() normalized = .dup normalized[:css_modules] = {} if normalized[:css_modules] == true normalized.delete(:css_modules) if normalized[:css_modules] == false normalized.compact end |
#to_json(state = nil) ⇒ String
: (?untyped) -> String
48 49 50 |
# File 'lib/lightningcss/options.rb', line 48 def to_json(state = nil) JSON.generate(to_h, state) end |
#validate!(names, allowed, subject) ⇒ void
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/lightningcss/options.rb', line 60 def validate!(names, allowed, subject) unknown = names - KNOWN raise OptionError, "Unknown option#{"s" if unknown.length > 1}: #{unknown.join(", ")}" if unknown.any? unsupported = names - allowed return if unsupported.empty? raise OptionError, "#{unsupported.join(", ")} #{unsupported.one? ? "is not an option" : "are not options"} for #{subject}" end |