Class: Yaks::Runner

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Adamantium::Flat, Util
Defined in:
lib/yaks/runner.rb

Instance Method Summary collapse

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Instance Method Details

#callObject



12
13
14
# File 'lib/yaks/runner.rb', line 12

def call
  Pipeline.new(steps).insert_hooks(hooks).call(object, env)
end

#contextObject



28
29
30
31
32
33
34
# File 'lib/yaks/runner.rb', line 28

def context
  {
    policy: policy,
    env: env,
    mapper_stack: []
  }.merge(slice_hash(options, :item_mapper))
end

#envObject



37
38
39
# File 'lib/yaks/runner.rb', line 37

def env
  options.fetch(:env, {})
end

#formatObject



20
21
22
# File 'lib/yaks/runner.rb', line 20

def format
  Pipeline.new([[:format, formatter], [:primitivize, primitivizer]]).insert_hooks(hooks).call(object, env)
end

#format_classClass

Returns:

  • (Class)


43
44
45
46
47
# File 'lib/yaks/runner.rb', line 43

def format_class
  Format.by_accept_header(env['HTTP_ACCEPT']) {
    Format.by_name(options.fetch(:format) { default_format })
  }
end

#formatterObject



65
66
67
# File 'lib/yaks/runner.rb', line 65

def formatter
  format_class.new(format_options_hash[format_name])
end

#hooksObject



86
87
88
# File 'lib/yaks/runner.rb', line 86

def hooks
  config.hooks + options.fetch(:hooks, [])
end

#mapObject



24
25
26
# File 'lib/yaks/runner.rb', line 24

def map
  Pipeline.new([[:map, mapper]]).insert_hooks(hooks).call(object, env)
end

#mapperObject



58
59
60
61
62
# File 'lib/yaks/runner.rb', line 58

def mapper
  options.fetch(:mapper) do
    policy.derive_mapper_from_object(object)
  end.new(context)
end

#primitivizerObject



70
71
72
73
74
75
76
77
78
# File 'lib/yaks/runner.rb', line 70

def primitivizer
  proc do |input|
    if format_class.serializer.equal? :json
      primitivize.call(input)
    else
      input
    end
  end
end

#readObject



16
17
18
# File 'lib/yaks/runner.rb', line 16

def read
  Pipeline.new([[:parse, serializer.inverse], [:read, formatter.inverse]]).insert_hooks(hooks).call(object, env)
end

#serializerObject



81
82
83
# File 'lib/yaks/runner.rb', line 81

def serializer
  serializers.fetch(format_class.serializer)
end

#stepsObject



50
51
52
53
54
55
# File 'lib/yaks/runner.rb', line 50

def steps
  [[ :map, mapper ],
   [ :format, formatter ],
   [ :primitivize, primitivizer],
   [ :serialize, serializer ]]
end