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
-
#inherits(*args) ⇒ Object
inherits can happen inside version block it allows version to inherit specific key from parents.
-
#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
#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 = [] = {} args.each do |arg| if arg.is_a?(String) || arg.is_a?(Symbol) all_keys << arg elsif arg.is_a?(Hash) = arg end end current_version = self.current_version_object if [:from] || ['from'] parent_version = current_version.protocol.get_version([:from] || ['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 = {} # 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
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 |