Class: ServiceContractWebmock::Field
- Inherits:
-
Object
- Object
- ServiceContractWebmock::Field
- Defined in:
- lib/service_contract_webmock/field.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #boolean?(t = type) ⇒ Boolean
- #build_value(type) ⇒ Object
- #convert(value) ⇒ Object
-
#initialize(name, type) ⇒ Field
constructor
A new instance of Field.
- #int?(t = type) ⇒ Boolean
Constructor Details
#initialize(name, type) ⇒ Field
Returns a new instance of Field.
5 6 7 8 9 |
# File 'lib/service_contract_webmock/field.rb', line 5 def initialize(name, type) @name = name @type = type @value = build_value(type) end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/service_contract_webmock/field.rb', line 3 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
3 4 5 |
# File 'lib/service_contract_webmock/field.rb', line 3 def type @type end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
3 4 5 |
# File 'lib/service_contract_webmock/field.rb', line 3 def value @value end |
Instance Method Details
#boolean?(t = type) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/service_contract_webmock/field.rb', line 17 def boolean?(t = type) t.type_sym == :boolean || (t.type_sym == :array && t.items.type_sym == :boolean) || (t.type_sym == :union && t.schemas.any? {|sub| boolean?(sub)}) end |
#build_value(type) ⇒ Object
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 |
# File 'lib/service_contract_webmock/field.rb', line 33 def build_value(type) case type.type_sym when :union subtypes = type.schemas.map {|subtype| build_value(subtype)} "(#{subtypes.join("|")})" when :null "" when :int "\\d+" when :array case type.items.type_sym when :int "[,\\d]+" when :string ".+" when :enum "(#{type.items.symbols.map{|symbol| "#{symbol}(\..+)?"}.join("|")})" else raise "unhandled array subtype #{type.items.type_sym}" end when :string ".+" when :boolean "true|false" when :float "\\d+\.\\d+" else raise "unhandled type #{type.type_sym}" end end |
#convert(value) ⇒ Object
23 24 25 26 27 28 29 30 31 |
# File 'lib/service_contract_webmock/field.rb', line 23 def convert(value) if int? value.to_i elsif boolean? value == "true" else value end end |
#int?(t = type) ⇒ Boolean
11 12 13 14 15 |
# File 'lib/service_contract_webmock/field.rb', line 11 def int?(t = type) t.type_sym == :int || (t.type_sym == :array && t.items.type_sym == :int) || (t.type_sym == :union && t.schemas.any? {|sub| int?(sub)}) end |