Skip to content

Commit

Permalink
Factor out ancestor class Field of Attribute and Reflection.
Browse files Browse the repository at this point in the history
  • Loading branch information
beauby committed Jan 4, 2016
1 parent 0641f25 commit 5bc8eea
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 72 deletions.
55 changes: 20 additions & 35 deletions lib/active_model/serializer/attribute.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,25 @@
require 'active_model/serializer/field'

module ActiveModel
class Serializer
Attribute = Struct.new(:name, :options, :block) do
def value(serializer)
if block
serializer.instance_eval(&block)
else
serializer.read_attribute_for_serialization(name)
end
end

def included?(serializer)
case condition
when :if
serializer.public_send(condition)
when :unless
!serializer.public_send(condition)
else
true
end
end

private

def condition_type
if options.key?(:if)
:if
elsif options.key?(:unless)
:unless
else
:none
end
end

def condition
options[condition_type]
end
# Holds all the meta-data about an attribute as it was specified in the
# ActiveModel::Serializer class.
#
# @example
# class PostSerializer < ActiveModel::Serializer
# attribute :content
# attribute :name, key: :title
# attribute :email, key: :author_email, if: :user_logged_in?
# attribute :preview do
# truncate(object.content)
# end
#
# def user_logged_in?
# current_user.logged_in?
# end
# end
#
class Attribute < Field
end
end
end
55 changes: 55 additions & 0 deletions lib/active_model/serializer/field.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module ActiveModel
class Serializer
# Holds all the meta-data about a field (i.e. attribute or association) as it was
# specified in the ActiveModel::Serializer class.
# Notice that the field block is evaluated in the context of the serializer.
Field = Struct.new(:name, :options, :block) do
# Compute the actual value of a field for a given serializer instance.
# @param [Serializer] The serializer instance for which the value is computed.
# @return [Object] value
#
# @api private
#
def value(serializer)
if block
serializer.instance_eval(&block)
else
serializer.read_attribute_for_serialization(name)
end
end

# Decide whether the field should be serialized by the given serializer instance.
# @param [Serializer] The serializer instance
# @return [Bool]
#
# @api private
#
def included?(serializer)
case condition
when :if
serializer.public_send(condition)
when :unless
!serializer.public_send(condition)
else
true
end
end

private

def condition_type
if options.key?(:if)
:if
elsif options.key?(:unless)
:unless
else
:none
end
end

def condition
options[condition_type]
end
end
end
end
40 changes: 3 additions & 37 deletions lib/active_model/serializer/reflection.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'active_model/serializer/field'

module ActiveModel
class Serializer
# Holds all the meta-data about an association as it was specified in the
Expand All @@ -12,7 +14,6 @@ class Serializer
# end
# end
#
# Notice that the association block is evaluated in the context of the serializer.
# Specifically, the association 'comments' is evaluated two different ways:
# 1) as 'comments' and named 'comments'.
# 2) as 'object.comments.last(1)' and named 'last_comments'.
Expand All @@ -25,28 +26,7 @@ class Serializer
#
# So you can inspect reflections in your Adapters.
#
Reflection = Struct.new(:name, :options, :block) do
# @api private
def value(instance)
if block
instance.instance_eval(&block)
else
instance.read_attribute_for_serialization(name)
end
end

# @api private
def included?(serializer)
case condition_type
when :if
serializer.public_send(condition)
when :unless
!serializer.public_send(condition)
else
true
end
end

class Reflection < Field
# Build association. This method is used internally to
# build serializer's association by its reflection.
#
Expand Down Expand Up @@ -91,20 +71,6 @@ def build_association(subject, parent_serializer_options)

private

def condition_type
if options.key?(:if)
:if
elsif options.key?(:unless)
:unless
else
:none
end
end

def condition
options[condition_type]
end

def serializer_options(subject, parent_serializer_options, reflection_options)
serializer = reflection_options.fetch(:serializer, nil)

Expand Down

0 comments on commit 5bc8eea

Please sign in to comment.