mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22: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)
|
||||
@_attributes.concat attrs
|
||||
|
||||
attrs.each do |attr|
|
||||
unless method_defined?(attr)
|
||||
define_method attr do
|
||||
object.read_attribute_for_serialization(attr)
|
||||
end
|
||||
end
|
||||
end
|
||||
attrs.each{|attr| define_attribute_accessor(attr)}
|
||||
end
|
||||
|
||||
def has_one(*attrs)
|
||||
@ -93,15 +86,27 @@ end
|
||||
options = attrs.extract_options!
|
||||
|
||||
attrs.each do |attr|
|
||||
unless method_defined?(attr)
|
||||
define_method attr do
|
||||
object.send attr
|
||||
end
|
||||
end
|
||||
|
||||
define_association_accessor(attr)
|
||||
@_associations[attr] = klass.new(attr, options)
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
def initialize(object, options={})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user