Class: Jsoning::ForDsl
- Inherits:
-
Object
- Object
- Jsoning::ForDsl
- Defined in:
- lib/jsoning/dsl/for_dsl.rb
Instance Attribute Summary collapse
-
#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.
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
#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
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 = {} args.each do |arg| if arg.is_a?(String) || arg.is_a?(Symbol) mapped_to = arg elsif arg.is_a?(Hash) = arg end end mapper = Jsoning::Mapper.new 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? .keys { |key| raise Jsoning::Error, "Undefined option: #{key}" } protocol.add_mapper mapper end |