Module: WellFormed::AttributeAssignment

Included in:
SimpleAction
Defined in:
lib/well_formed/attribute_assignment.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
# File 'lib/well_formed/attribute_assignment.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#assign_attributes_to(resource) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/well_formed/attribute_assignment.rb', line 21

def assign_attributes_to(resource)
  matched = attributes.select { |attr, _| resource.respond_to?("#{attr}=") }
  unmatched_keys = attributes.keys - matched.keys

  if unmatched_keys.any?
    case self.class.unmatched_attributes_policy
    when :warn
      warn "#{self.class} has attributes with no setter on resource: #{unmatched_keys.join(", ")}"
    when :raise
      raise UnmatchedAttributesError,
        "#{self.class} has attributes with no setter on resource: #{unmatched_keys.join(", ")}"
    end
  end

  if resource.respond_to?(:assign_attributes)
    resource.assign_attributes(matched)
  else
    matched.each { |attr, value| resource.public_send(:"#{attr}=", value) }
  end
end