Class: Jsoning::ForDsl
- Inherits:
-
Object
- Object
- Jsoning::ForDsl
- Defined in:
- lib/jsoning/dsl/for_dsl.rb
Constant Summary collapse
- @@mutex =
Mutex.new
Instance Attribute Summary collapse
-
#current_version_object ⇒ Object
Returns the value of attribute current_version_object.
-
#protocol ⇒ Object
readonly
Returns the value of attribute protocol.
Instance Method Summary collapse
-
#initialize(protocol) ⇒ ForDsl
constructor
A new instance of ForDsl.
-
#key(*args) ⇒ Object
args is first specifying the name for variable to be displayed in JSON docs, and then the options.
-
#version(version_name) ⇒ Object
specify the version under which key will be executed.
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_object ⇒ Object
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 |
#protocol ⇒ Object (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
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/jsoning/dsl/for_dsl.rb', line 31 def key *args mapped_to = nil mapped_from = nil = {} # 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 = 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 = .delete(:from) || .delete("from") || mapped_to mapper.parallel_variable = mapped_from end mapper.name = mapped_to mapper.default_value = .delete(:default) || .delete("default") mapper.nullable = .delete(:null) mapper.nullable = .delete("null") if mapper.nullable?.nil? # if value is given, it has a value processor to be executed # after value is determined if [:value] || ['value'] mapper.value_processor = .delete(:value) || .delete('value') end .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
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jsoning/dsl/for_dsl.rb', line 11 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 end end |