Class: Jsoning::ForDsl

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

Constant Summary collapse

@@mutex =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(protocol) ⇒ ForDsl

Returns a new instance of ForDsl.



6
7
8
# File 'lib/jsoning/dsl/for_dsl.rb', line 6

def initialize(protocol)
  @protocol = protocol
end

Instance Attribute Details

#current_version_objectObject

Returns the value of attribute current_version_object.



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

def current_version_object
  @current_version_object
end

#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

#inherits(*args) ⇒ Object

inherits can happen inside version block it allows version to inherit specific key from parents



12
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
# File 'lib/jsoning/dsl/for_dsl.rb', line 12

def inherits(*args)
  fail 'Must be inside version block' unless current_version_object
  all_keys = []
  options = {}

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

  current_version = self.current_version_object
  if options[:from] || options['from']
    parent_version = current_version.protocol.get_version(options[:from] || options['from'])
  else
    parent_version = current_version.protocol.get_version(:default)
  end

  all_keys.each do |key_name|
    mapper = parent_version.mapper_for(key_name)
    current_version.add_mapper(mapper)
  end
  all_keys
end

#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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/jsoning/dsl/for_dsl.rb', line 61

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

  # iterate the args given, it contains a string/symbol indicating the key name
  # and options, that specify further about the behaviour/mechanism of the key
  args.each do |arg|
    if arg.is_a?(String) || arg.is_a?(Symbol)
      mapped_to = arg
    elsif arg.is_a?(Hash)
      # extract the options if given
      options = arg
    end
  end

  # get the version instance
  version_instance = if self.current_version_object.nil?
    protocol.get_version!(:default)
  else
    current_version_object
  end 

  mapper = Jsoning::Mapper.new(version_instance)
  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?

  # if value is given, it has a value processor to be executed
  # after value is determined
  if options[:value] || options['value']
    mapper.value_processor = options.delete(:value) || options.delete('value')
  end

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

  # add mapper to the version
  version_instance.add_mapper(mapper)
end

#version(version_name) ⇒ Object

specify the version under which key will be executed



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/jsoning/dsl/for_dsl.rb', line 40

def version(version_name)
  @@mutex.synchronize do
    # todo: make sure version cannot be nester, in order that to be possible
    # version must have its own dsl, dduh
    # fail Jsoning::Error, "Version cannot be nested" if current_version_object

    # retrieve the version, or create a new one if not yet defined
    version = protocol.get_version(version_name)
    version = protocol.add_version(version_name) if version.nil?

    self.current_version_object = version
    yield
    self.current_version_object = nil
  end
end