mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 14:38:17 +08:00
c78211cf8d
We decided to make contracts immutable once their validations have run. Indeed, it doesn’t make a lot of sense to modify a contract value outside the contract itself. If processing is needed, then it should happen inside the contract itself.
32 lines
559 B
Ruby
32 lines
559 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Service::ContractBase
|
|
include ActiveModel::API
|
|
include ActiveModel::Attributes
|
|
include ActiveModel::AttributeMethods
|
|
include ActiveModel::Validations::Callbacks
|
|
|
|
delegate :slice, :merge, to: :to_hash
|
|
|
|
def initialize(*args, options: nil, **kwargs)
|
|
@__options__ = options
|
|
super(*args, **kwargs)
|
|
end
|
|
|
|
def options
|
|
@__options__
|
|
end
|
|
|
|
def [](key)
|
|
public_send(key)
|
|
end
|
|
|
|
def to_hash
|
|
attributes.symbolize_keys
|
|
end
|
|
|
|
def raw_attributes
|
|
@attributes.values_before_type_cast
|
|
end
|
|
end
|