mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Extract DSL to a class
This commit is contained in:
parent
1c440b513f
commit
cf91146471
@ -2,8 +2,10 @@ require 'active_model/array_serializer'
|
|||||||
require 'active_model/serializable'
|
require 'active_model/serializable'
|
||||||
require 'active_model/serializer/associations'
|
require 'active_model/serializer/associations'
|
||||||
require 'active_model/serializer/config'
|
require 'active_model/serializer/config'
|
||||||
|
require 'active_model/serializer/dsl'
|
||||||
|
|
||||||
require 'thread'
|
require 'thread'
|
||||||
|
require 'forwardable'
|
||||||
|
|
||||||
module ActiveModel
|
module ActiveModel
|
||||||
class Serializer
|
class Serializer
|
||||||
@ -68,36 +70,14 @@ end
|
|||||||
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
name.demodulize.underscore.sub(/_serializer$/, '') if name
|
||||||
end
|
end
|
||||||
|
|
||||||
def attributes(*attrs)
|
extend Forwardable
|
||||||
@_attributes.concat attrs
|
|
||||||
|
|
||||||
attrs.each do |attr|
|
def_delegators :dsl, :attributes, :has_one, :has_many
|
||||||
define_method attr do
|
|
||||||
object.read_attribute_for_serialization attr
|
|
||||||
end unless method_defined?(attr)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def has_one(*attrs)
|
|
||||||
associate(Association::HasOne, *attrs)
|
|
||||||
end
|
|
||||||
|
|
||||||
def has_many(*attrs)
|
|
||||||
associate(Association::HasMany, *attrs)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def associate(klass, *attrs)
|
def dsl
|
||||||
options = attrs.extract_options!
|
@dsl ||= DSL.new self
|
||||||
|
|
||||||
attrs.each do |attr|
|
|
||||||
define_method attr do
|
|
||||||
object.send attr
|
|
||||||
end unless method_defined?(attr)
|
|
||||||
|
|
||||||
@_associations[attr] = klass.new(attr, options)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
43
lib/active_model/serializer/dsl.rb
Normal file
43
lib/active_model/serializer/dsl.rb
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
module ActiveModel
|
||||||
|
class Serializer
|
||||||
|
class DSL
|
||||||
|
attr_reader :serializer_class
|
||||||
|
|
||||||
|
def initialize(serializer_class)
|
||||||
|
@serializer_class = serializer_class
|
||||||
|
end
|
||||||
|
|
||||||
|
def attributes(*names)
|
||||||
|
serializer_class._attributes.concat names
|
||||||
|
|
||||||
|
names.each do |name|
|
||||||
|
serializer_class.send :define_method, name do
|
||||||
|
object.read_attribute_for_serialization name
|
||||||
|
end unless serializer_class.method_defined? name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def has_one(*names)
|
||||||
|
associate Association::HasOne, *names
|
||||||
|
end
|
||||||
|
|
||||||
|
def has_many(*names)
|
||||||
|
associate Association::HasMany, *names
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def associate(klass, *names)
|
||||||
|
options = names.extract_options!
|
||||||
|
|
||||||
|
names.each do |name|
|
||||||
|
serializer_class.send :define_method, name do
|
||||||
|
object.send name
|
||||||
|
end unless serializer_class.method_defined? name
|
||||||
|
|
||||||
|
serializer_class._associations[name] = klass.new name, options
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Reference in New Issue
Block a user