Class: ServiceContractWebmock::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/service_contract_webmock/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/service_contract_webmock/field.rb', line 3

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/service_contract_webmock/field.rb', line 3

def type
  @type
end

#valueObject (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

Returns:

  • (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
# 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]+"
    else
      raise "unhandled array subtype #{type.items.type_sym}"
    end
  when :string
    ".+"
  when :boolean
    "true|false"
  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

Returns:

  • (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