Class: Jsoning::ForDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/jsoning/dsl/for_dsl.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol) ⇒ ForDsl

Returns a new instance of ForDsl.



4
5
6
# File 'lib/jsoning/dsl/for_dsl.rb', line 4

def initialize(protocol)
  @protocol = protocol
end

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



2
3
4
# File 'lib/jsoning/dsl/for_dsl.rb', line 2

def protocol
  @protocol
end

Instance Method Details

#key(*args) ⇒ Object

args is first specifying the name for variable to be displayed in JSON docs, and then the options. options are optional, if set, it can contains:

  • from

  • null

  • default



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jsoning/dsl/for_dsl.rb', line 13

def key *args
  mapped_to = nil
  mapped_from = nil
  options = {}

  args.each do |arg|
    if arg.is_a?(String) || arg.is_a?(Symbol)
      mapped_to = arg
    elsif arg.is_a?(Hash)
      options = arg
    end
  end

  mapper = Jsoning::Mapper.new
  if block_given?
    raise Jsoning::Error, "Cannot parse block to key"
  else
    mapped_from = options.delete(:from) || options.delete("from") || mapped_to
    mapper.parallel_variable = mapped_from
  end

  mapper.name = mapped_to
  mapper.default_value = options.delete(:default) || options.delete("default")
  mapper.nullable = options.delete(:null)
  mapper.nullable = options.delete("null") if mapper.nullable?.nil?

  options.keys { |key| raise Jsoning::Error, "Undefined option: #{key}" }

  protocol.add_mapper mapper
end