Class: BrightSerializer::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/bright_serializer/attribute.rb

Direct Known Subclasses

AttributeRelation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, condition, entity, &block) ⇒ Attribute

Returns a new instance of Attribute.



10
11
12
13
14
15
# File 'lib/bright_serializer/attribute.rb', line 10

def initialize(key, condition, entity, &block)
  @key = key
  @condition = condition
  @block = block
  @entity = entity ? Entity::Base.new(entity) : nil
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/bright_serializer/attribute.rb', line 7

def block
  @block
end

#conditionObject (readonly)

Returns the value of attribute condition.



7
8
9
# File 'lib/bright_serializer/attribute.rb', line 7

def condition
  @condition
end

#entityObject (readonly)

Returns the value of attribute entity.



7
8
9
# File 'lib/bright_serializer/attribute.rb', line 7

def entity
  @entity
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/bright_serializer/attribute.rb', line 7

def key
  @key
end

#transformed_keyObject

Returns the value of attribute transformed_key.



8
9
10
# File 'lib/bright_serializer/attribute.rb', line 8

def transformed_key
  @transformed_key
end

Instance Method Details

#condition?(object, params) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
# File 'lib/bright_serializer/attribute.rb', line 25

def condition?(object, params)
  return true unless @condition

  @condition.call(object, params)
end

#serialize(serializer_instance, object, params) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/bright_serializer/attribute.rb', line 17

def serialize(serializer_instance, object, params)
  return unless object

  value = attribute_value(serializer_instance, object, params)

  value.respond_to?(:serializable_hash) ? value.serializable_hash : value
end