Class: FastSerializer::JsonModel::Attribute

Inherits:
Node
  • Object
show all
Defined in:
lib/fast_serializer/json_model/attribute.rb

Direct Known Subclasses

Relationship

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, method, opts = {}) ⇒ Attribute

Returns a new instance of Attribute.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fast_serializer/json_model/attribute.rb', line 18

def initialize(key, method, opts = {})
  super()

  @opts = opts || {}
  @injected_methods = Hash.new { |h, method_name| h[method_name] = true }
  @key = key.to_sym
  @method = !method ? key : method

  @mixin = Module.new
  @injected = false

  @method_name = @method && !@method.is_a?(Proc) ? @method : nil
  @method_arity = @method.is_a?(Proc) ? [@method.arity, 0].max : nil
  @cond = @opts[:if] || @opts[:unless]
  @cond_method_name = @cond && !@cond.is_a?(Proc) ? @cond : nil
  @cond_arity = @cond.is_a?(Proc) ? [@cond.arity, 0].max : nil

  init_with_proc if @method.is_a?(Proc)
  init_with_cond if @cond && @cond.is_a?(Proc)
end

Instance Attribute Details

#condObject

Returns the value of attribute cond.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def cond
  @cond
end

#cond_arityObject

Returns the value of attribute cond_arity.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def cond_arity
  @cond_arity
end

#cond_method_nameObject

Returns the value of attribute cond_method_name.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def cond_method_name
  @cond_method_name
end

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def context
  @context
end

#injectedObject

Returns the value of attribute injected.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def injected
  @injected
end

#keyObject

Returns the value of attribute key.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def key
  @key
end

#methodObject

Returns the value of attribute method.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def method
  @method
end

#method_arityObject

Returns the value of attribute method_arity.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def method_arity
  @method_arity
end

#method_nameObject

Returns the value of attribute method_name.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def method_name
  @method_name
end

#mixinObject

Returns the value of attribute mixin.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def mixin
  @mixin
end

#optsObject

Returns the value of attribute opts.



6
7
8
# File 'lib/fast_serializer/json_model/attribute.rb', line 6

def opts
  @opts
end

Instance Method Details

#included?(resource, params, context) ⇒ Boolean

Parameters:

  • resource (Object)
  • params (Hash)
  • context (Hash)

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/fast_serializer/json_model/attribute.rb', line 72

def included?(resource, params, context)
  return true if !cond

  can_execute_on_mixin = !!(injected && cond_method_name && @injected_methods.key?(cond_method_name) && context)

  res = if can_execute_on_mixin
          call_method_on_context(context, cond_method_name, cond_arity, resource, params)
        elsif cond.is_a?(Proc)
          call_proc_binding_to_context(context, cond, cond_arity, resource, params)
        else
          context.public_send(cond)
        end

  res = !res unless @opts[:unless].nil?

  res
end

#inject(context) ⇒ Object



43
44
45
46
# File 'lib/fast_serializer/json_model/attribute.rb', line 43

def inject(context)
  context.include(mixin)
  self.injected = true
end

#injectable?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/fast_serializer/json_model/attribute.rb', line 39

def injectable?
  !mixin.nil?
end

#serialize(resource, params, context) ⇒ Object

Parameters:

  • resource (Object)
  • params (Hash)
  • context (Hash)

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fast_serializer/json_model/attribute.rb', line 52

def serialize(resource, params, context)
  can_execute_on_mixin = !!(injected && method_name && @injected_methods.key?(method_name) && context)

  val = if can_execute_on_mixin
          call_method_on_context(context, method_name, method_arity, resource, params)
        elsif method.is_a?(Proc)
          call_proc_binding_to_context(context, method, method_arity, resource, params)
        else
          resource.public_send(method_name)
        end

  val.freeze

  val
end