Module: AskAwesomely::JsonBuilder

Included in:
Choice, Field::Field, LogicJump
Defined in:
lib/ask_awesomely/json_builder.rb

Instance Method Summary collapse

Instance Method Details

#build_json(context = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ask_awesomely/json_builder.rb', line 4

def build_json(context = nil)
  super if defined?(super) 

  state.to_h.reduce({}) do |json, (k, v)|

    next if k == :skip && v.call(context) == true
    
    json[k] = case 
              when v.respond_to?(:to_ary)
                v.map {|f| f.respond_to?(:build_json) ? f.build_json(context) : f }
                 .compact
              when v.respond_to?(:call)
                v.call(context)
              when v.respond_to?(:build_json)
                v.build_json(context)
              else
                v
              end
    json
  end
end