mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 23:06:50 +00:00
Reduce complexity and duplication; make accessor definition more explicit
This commit is contained in:
parent
74374ae5af
commit
5ac6e626b5
@ -69,14 +69,7 @@ end
|
|||||||
|
|
||||||
def attributes(*attrs)
|
def attributes(*attrs)
|
||||||
@_attributes.concat attrs
|
@_attributes.concat attrs
|
||||||
|
attrs.each{|attr| define_attribute_accessor(attr)}
|
||||||
attrs.each do |attr|
|
|
||||||
unless method_defined?(attr)
|
|
||||||
define_method attr do
|
|
||||||
object.read_attribute_for_serialization(attr)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def has_one(*attrs)
|
def has_one(*attrs)
|
||||||
@ -93,13 +86,25 @@ end
|
|||||||
options = attrs.extract_options!
|
options = attrs.extract_options!
|
||||||
|
|
||||||
attrs.each do |attr|
|
attrs.each do |attr|
|
||||||
unless method_defined?(attr)
|
define_association_accessor(attr)
|
||||||
define_method attr do
|
@_associations[attr] = klass.new(attr, options)
|
||||||
object.send attr
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@_associations[attr] = klass.new(attr, options)
|
def define_accessor_unless_defined(attr, &block)
|
||||||
|
return if method_defined?(attr)
|
||||||
|
define_method(attr, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def define_attribute_accessor(attr)
|
||||||
|
define_accessor_unless_defined attr do
|
||||||
|
object.read_attribute_for_serialization(attr)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def define_association_accessor(attr)
|
||||||
|
define_accessor_unless_defined attr do
|
||||||
|
object.send attr
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user