Module: SmartProperties
- Included in:
- Validations::Ancestor
- Defined in:
- lib/smart_properties.rb,
lib/smart_properties/errors.rb,
lib/smart_properties/version.rb,
lib/smart_properties/property.rb,
lib/smart_properties/validations.rb,
lib/smart_properties/property_collection.rb,
lib/smart_properties/validations/ancestor.rb
Overview
SmartProperties can be used to easily build more full-fledged accessors for standard Ruby classes. In contrast to regular accessors, SmartProperties support validation and conversion of input data, as well as, the specification of default values. Additionally, individual SmartProperties can be marked as required. This causes the runtime to throw an ArgumentError
whenever a required property has not been specified.
In order to use SmartProperties, simply include the SmartProperties module and use the ClassMethods#property method to define properties.
Defined Under Namespace
Modules: ClassMethods, ModuleMethods, Validations Classes: AssignmentError, ConfigurationError, ConstructorArgumentForwardingError, Error, InitializationError, InvalidValueError, MissingValueError, Property, PropertyCollection
Constant Summary collapse
- VERSION =
"1.17.0"
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
-
#initialize(*args, &block) ⇒ Object
Implements a key-value enabled constructor that acts as default constructor for all SmartProperties-enabled classes.
Instance Method Details
#[](name) ⇒ Object
165 166 167 168 169 170 |
# File 'lib/smart_properties.rb', line 165 def [](name) return if name.nil? name = name.to_sym reader = self.class.properties[name].reader public_send(reader) if self.class.properties.key?(name) end |
#[]=(name, value) ⇒ Object
172 173 174 175 |
# File 'lib/smart_properties.rb', line 172 def []=(name, value) return if name.nil? public_send(:"#{name.to_sym}=", value) if self.class.properties.key?(name) end |
#initialize(*args, &block) ⇒ Object
Implements a key-value enabled constructor that acts as default constructor for all SmartProperties-enabled classes. Positional arguments or keyword arguments that do not correspond to a property are forwarded to the super class constructor.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/smart_properties.rb', line 127 def initialize(*args, &block) attrs = args.last.is_a?(Hash) ? args.pop.dup : {} properties = self.class.properties # Track missing properties missing_properties = [] # Set values properties.each do |name, property| if attrs.key?(name) property.set(self, attrs.delete(name)) elsif attrs.key?(name.to_s) property.set(self, attrs.delete(name.to_s)) else missing_properties.push(property) end end # Call the super constructor and forward unprocessed arguments begin attrs.empty? ? super(*args) : super(*args.dup.push(attrs)) rescue ArgumentError raise SmartProperties::ConstructorArgumentForwardingError.new(args, attrs) end # Execute configuration block block.call(self) if block # Set default values for missing properties missing_properties.delete_if { |property| property.set_default(self) } # Recheck - cannot be done while assigning default values because # one property might depend on the default value of another property missing_properties.delete_if { |property| property.present?(self) || property.optional?(self) } raise SmartProperties::InitializationError.new(self, missing_properties) unless missing_properties.empty? end |