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, Validations Classes: AssignmentError, ConfigurationError, ConstructorArgumentForwardingError, Error, InitializationError, InvalidValueError, MissingValueError, Property, PropertyCollection
Constant Summary collapse
- VERSION =
"1.15.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
157 158 159 160 161 162 |
# File 'lib/smart_properties.rb', line 157 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
164 165 166 167 |
# File 'lib/smart_properties.rb', line 164 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.
119 120 121 122 123 124 125 126 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 |
# File 'lib/smart_properties.rb', line 119 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 |