Module: Datamappify::Entity::Composable::DSL
- Defined in:
- lib/datamappify/entity/composable.rb
Instance Method Summary collapse
- #attributes_from(entity_class, options = {}) ⇒ void
- #excluded_attributes(entity_class) ⇒ Array private
- #prefix_attribute_name(attribute, prefix) ⇒ void private
- #rename_validator_attributes(validator, prefix) ⇒ void private
- #rename_validators(entity_class, prefix) ⇒ void private
- #run_validators ⇒ void private
- #setup_attributes(entity_class, options) ⇒ void private
- #setup_validators(entity_class, options) ⇒ void private
- #tweak_attribute(attribute, options) ⇒ Virtus::Attribute private
Instance Method Details
#attributes_from(entity_class, options = {}) ⇒ void
This method returns an undefined value.
14 15 16 17 18 |
# File 'lib/datamappify/entity/composable.rb', line 14 def attributes_from(entity_class, = {}) setup_attributes(entity_class, ) setup_validators(entity_class, ) run_validators end |
#excluded_attributes(entity_class) ⇒ Array (private)
82 83 84 |
# File 'lib/datamappify/entity/composable.rb', line 82 def excluded_attributes(entity_class) @excluded_attributes ||= entity_class.reference_keys << :id end |
#prefix_attribute_name(attribute, prefix) ⇒ void (private)
This method returns an undefined value.
102 103 104 105 |
# File 'lib/datamappify/entity/composable.rb', line 102 def prefix_attribute_name(attribute, prefix) name = attribute.instance_variable_set :@name, :"#{prefix}_#{attribute.name}" attribute.instance_variable_set :@instance_variable_name, "@#{name}".to_sym end |
#rename_validator_attributes(validator, prefix) ⇒ void (private)
This method returns an undefined value.
75 76 77 |
# File 'lib/datamappify/entity/composable.rb', line 75 def rename_validator_attributes(validator, prefix) validator.instance_variable_set :@attributes, validator.attributes.map { |name| :"#{prefix}_#{name}" } end |
#rename_validators(entity_class, prefix) ⇒ void (private)
This method returns an undefined value.
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/datamappify/entity/composable.rb', line 58 def rename_validators(entity_class, prefix) entity_class._validators.each do |attribute_name, validators| dup_validators = validators.dup dup_validators.each do |validator| rename_validator_attributes(validator, prefix) end self._validators[:"#{prefix}_#{attribute_name}"] = dup_validators end end |
#run_validators ⇒ void (private)
This method returns an undefined value.
45 46 47 48 49 50 51 |
# File 'lib/datamappify/entity/composable.rb', line 45 def run_validators self._validators.each do |_, validators| validators.each do |validator| validate(validator, validator.) end end end |
#setup_attributes(entity_class, options) ⇒ void (private)
This method returns an undefined value.
25 26 27 28 29 30 31 |
# File 'lib/datamappify/entity/composable.rb', line 25 def setup_attributes(entity_class, ) entity_class.attribute_set.dup.each do |attribute| unless excluded_attributes(entity_class).include?(attribute.name) self.attribute_set << tweak_attribute(attribute, ) end end end |
#setup_validators(entity_class, options) ⇒ void (private)
This method returns an undefined value.
36 37 38 39 40 41 42 |
# File 'lib/datamappify/entity/composable.rb', line 36 def setup_validators(entity_class, ) if [:prefix_with] rename_validators(entity_class, [:prefix_with]) else entity_class._validators.each { |k, v| self._validators[k] = v.dup } end end |
#tweak_attribute(attribute, options) ⇒ Virtus::Attribute (private)
91 92 93 94 95 |
# File 'lib/datamappify/entity/composable.rb', line 91 def tweak_attribute(attribute, ) prefix_attribute_name(attribute, [:prefix_with]) if [:prefix_with] attribute end |