Module: T::Props::ClassMethods
Overview
CAUTION: This mixin is used in hundreds of classes; we want to keep its surface area as narrow as possible and avoid polluting (and possibly conflicting with) the classes that use it.
It currently has zero instance methods; let’s try to keep it that way. For ClassMethods (below), try to add things to T::Props::Decorator instead unless you are sure it needs to be exposed here.
Constant Summary
Constants included from Helpers
Class Method Summary collapse
-
.prop(name, type, opts = {}) ⇒ void
Define a new property.
Instance Method Summary collapse
- #const(name, cls_or_args, args = {}) ⇒ Object
- #decorator ⇒ Object
- #decorator_class ⇒ Object
- #extended(child) ⇒ Object
- #included(child) ⇒ Object
- #inherited(child) ⇒ Object
-
#plugin(mod) ⇒ Object
Needs to be documented.
- #plugins ⇒ Object
- #prepended(child) ⇒ Object
-
#prop(name, cls, rules = {}) ⇒ void
Define a new property.
- #props ⇒ Object
- #reload_decorator! ⇒ Object
-
#validate_prop_value(propname, value) ⇒ void
Validates the value of the specified prop.
Methods included from Sig
Methods included from Helpers
abstract!, final!, interface!, mixes_in_class_methods
Class Method Details
.prop(name, type, opts = {}) ⇒ void
This method returns an undefined value.
Define a new property. See README for some concrete
examples.
Defining a property defines a method with the same name as the property, that returns the current value, and a ‘prop=` method to set its value. Properties will be inherited by subclasses of a document class.
107 108 109 110 |
# File 'lib/types/props/_props.rb', line 107 def prop(name, cls, rules={}) cls = T::Utils.coerce(cls) if !cls.is_a?(Module) decorator.prop_defined(name, cls, rules) end |
Instance Method Details
#const(name, cls_or_args, args = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/types/props/_props.rb', line 132 def const(name, cls_or_args, args={}) if (cls_or_args.is_a?(Hash) && cls_or_args.key?(:immutable)) || args.key?(:immutable) raise ArgumentError.new("Cannot pass 'immutable' argument when using 'const' keyword to define a prop") end if cls_or_args.is_a?(Hash) self.prop(name, cls_or_args.merge(immutable: true)) else self.prop(name, cls_or_args, args.merge(immutable: true)) end end |
#decorator ⇒ Object
28 |
# File 'lib/types/props/_props.rb', line 28 def decorator; @decorator ||= decorator_class.new(self); end |
#decorator_class ⇒ Object
26 |
# File 'lib/types/props/_props.rb', line 26 def decorator_class; Decorator; end |
#extended(child) ⇒ Object
154 155 156 157 |
# File 'lib/types/props/_props.rb', line 154 def extended(child) decorator.model_inherited(child.singleton_class) super end |
#included(child) ⇒ Object
144 145 146 147 |
# File 'lib/types/props/_props.rb', line 144 def included(child) decorator.model_inherited(child) super end |
#inherited(child) ⇒ Object
159 160 161 162 |
# File 'lib/types/props/_props.rb', line 159 def inherited(child) decorator.model_inherited(child) super end |
#plugin(mod) ⇒ Object
Needs to be documented
126 127 128 |
# File 'lib/types/props/_props.rb', line 126 def plugin(mod) decorator.plugin(mod) end |
#plugins ⇒ Object
24 |
# File 'lib/types/props/_props.rb', line 24 def plugins; @plugins ||= []; end |
#prepended(child) ⇒ Object
149 150 151 152 |
# File 'lib/types/props/_props.rb', line 149 def prepended(child) decorator.model_inherited(child) super end |
#prop(name, cls, rules = {}) ⇒ void
This method returns an undefined value.
Define a new property. See README for some concrete
examples.
Defining a property defines a method with the same name as the property, that returns the current value, and a ‘prop=` method to set its value. Properties will be inherited by subclasses of a document class.
107 108 109 110 |
# File 'lib/types/props/_props.rb', line 107 def prop(name, cls, rules={}) cls = T::Utils.coerce(cls) if !cls.is_a?(Module) decorator.prop_defined(name, cls, rules) end |
#props ⇒ Object
23 |
# File 'lib/types/props/_props.rb', line 23 def props; decorator.props; end |
#reload_decorator! ⇒ Object
29 |
# File 'lib/types/props/_props.rb', line 29 def reload_decorator!; @decorator = decorator_class.new(self); end |
#validate_prop_value(propname, value) ⇒ void
This method returns an undefined value.
Validates the value of the specified prop. This method allows the caller to
validate a value for a prop without having to set the data on the instance.
Throws if invalid.
121 122 123 |
# File 'lib/types/props/_props.rb', line 121 def validate_prop_value(prop, val) decorator.validate_prop_value(prop, val) end |