Class: BCDD::Value::Properties
- Inherits:
-
Object
- Object
- BCDD::Value::Properties
- Defined in:
- lib/bcdd/ext/value.rb
Defined Under Namespace
Modules: Contract
Constant Summary collapse
- Default =
->() do value = [:default] return value unless value.is_a?(Proc) return value if value.lambda? && value.arity.zero? raise ArgumentError, 'Default value must be a lambda with zero arity' end
- Normalize =
->() do value = [:normalize] return value if value.is_a?(Proc) raise ArgumentError, 'normalize value must be a lambda' end
- Type =
->() do type = [:type] return type if type.is_a?(::Module) raise ArgumentError, 'type must be a Module or a Class' end
Instance Attribute Summary collapse
-
#contract ⇒ Object
readonly
Returns the value of attribute contract.
-
#spec ⇒ Object
readonly
Returns the value of attribute spec.
Instance Method Summary collapse
- #contract? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize(options) ⇒ Properties
constructor
A new instance of Properties.
- #map(value) ⇒ Object
Constructor Details
#initialize(options) ⇒ Properties
Returns a new instance of Properties.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/bcdd/ext/value.rb', line 68 def initialize() @contract = false contract = Contract[] @spec = {} @spec[:type] = Type[] if .key?(:type) @spec[:default] = Default[] if .key?(:default) @spec[:contract] = contract if contract @spec[:normalize] = Normalize[] if .key?(:normalize) end |
Instance Attribute Details
#contract ⇒ Object (readonly)
Returns the value of attribute contract.
66 67 68 |
# File 'lib/bcdd/ext/value.rb', line 66 def contract @contract end |
#spec ⇒ Object (readonly)
Returns the value of attribute spec.
66 67 68 |
# File 'lib/bcdd/ext/value.rb', line 66 def spec @spec end |
Instance Method Details
#contract? ⇒ Boolean
88 89 90 |
# File 'lib/bcdd/ext/value.rb', line 88 def contract? contract end |
#freeze ⇒ Object
80 81 82 83 84 85 86 |
# File 'lib/bcdd/ext/value.rb', line 80 def freeze @contract = spec.key?(:contract) spec.freeze super end |
#map(value) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bcdd/ext/value.rb', line 92 def map(value) if !value && spec.key?(:default) default = spec[:default] value = default.is_a?(::Proc) ? default.call : default end type = spec[:type] value = spec[:normalize].call(value) if spec.key?(:normalize) && (!type || type === value) spec.key?(:contract) ? spec[:contract][value] : Contract.null(value) end |