Module: Poisol::ResponseBodyBuilder

Included in:
StubClassBuilder
Defined in:
lib/poisol/stub/response/array_response_body.rb,
lib/poisol/stub/response/response_body_builder.rb

Instance Method Summary collapse

Instance Method Details

#make_method_to_alter_response_array_objectObject



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
# File 'lib/poisol/stub/response/array_response_body.rb', line 42

def make_method_to_alter_response_array_object
  @response_array_item.each do |field|
    field_name = field[0]
    actual_field_value = field[1]
    is_array = (actual_field_value.class.to_s == "Array")
    actual_field_value = actual_field_value[0] if is_array

    method_name = "with_#{field_name.underscore}"
    define_method(method_name) do |*input_value|
      input_value = input_value[0]
      assignment_value = get_assignment_value actual_field_value,input_value
      @response.body.last[field_name] =  assignment_value
      self
    end

    if is_array
      method_name = "with_#{field_name.classify.underscore}"
      define_method(method_name) do |*input_value|
        input_value = input_value[0]
        assignment_value = get_assignment_value actual_field_value,input_value
        if @called_methods.include? __method__
          @response.body.last[field_name] << assignment_value
        else
          @response.body.last[field_name] = [assignment_value]
        end
        @called_methods << __method__ 
        self
      end
    end
  end
end

#make_method_to_alter_response_field(field_name, actual_field_value) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/poisol/stub/response/response_body_builder.rb', line 58

def make_method_to_alter_response_field field_name,actual_field_value
  method_name = "has_#{field_name.underscore}"
  define_method(method_name) do |*input_value|
    input_value = input_value[0]
    assignment_value = get_assignment_value actual_field_value,input_value
    @response.body[field_name] = assignment_value
    self
  end
end

#make_method_to_alter_response_nested_array(field_name, actual_field_values) ⇒ Object



27
28
29
30
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
# File 'lib/poisol/stub/response/response_body_builder.rb', line 27

def make_method_to_alter_response_nested_array field_name,actual_field_values
  actual_field_value = actual_field_values[0] 

  method_name = "has_#{field_name.underscore}" 
  define_method(method_name) do |*input_value|
    @response.body[field_name]  = []
    input_hashes = input_value[0]
    input_hashes.each do |input_hash|
      assignment_value = get_assignment_value actual_field_value.deep_dup,input_hash
      @response.body[field_name] << assignment_value
    end
    self
  end


  method_name = "has_#{field_name.classify.underscore}" 
  define_method(method_name) do |*input_value|
    input_value = input_value[0]
    assignment_value = get_assignment_value actual_field_value,input_value
    @response.body[field_name] = @called_methods.include?(__method__) ?  @response.body[field_name] << assignment_value  :[assignment_value] 
    @called_methods << __method__
    self
  end

  method_name = "has_no_#{field_name.classify.underscore}" 
  define_method(method_name) do 
    @response.body[field_name] = [] 
    self
  end
end

#make_method_to_append_response_arrayObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/poisol/stub/response/array_response_body.rb', line 11

def make_method_to_append_response_array
  class_name = self.name.classify.underscore
  method_name = "has_#{class_name}"
  define_method(method_name) do |*input_value|
    assignment_value = input_value.blank? ? 
      stub_config.response.body.deep_dup : 
      (stub_config.response.body.deep_dup).deep_merge!(input_value[0].stringify_keys)
    if @called_methods.include? __method__
      @response.body << assignment_value
    else
      @response.body = [assignment_value]
    end
    @called_methods << __method__ 
    remove_array_field_calls
    self
  end
end

#make_method_to_append_response_array_as_hash_paramsObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/poisol/stub/response/array_response_body.rb', line 29

def make_method_to_append_response_array_as_hash_params
  class_name = self.name.underscore
  method_name = "has_#{class_name}"
  define_method(method_name) do |*input_value|
    @response.body = []
    input_hashes = input_value[0]
    input_hashes.each do |input_hash|
      @response.body << (stub_config.response.body.deep_dup).deep_merge!(input_hash.stringify_keys)
    end
    self
  end
end

#make_methods_to_alter_response_arrayObject



4
5
6
7
8
9
# File 'lib/poisol/stub/response/array_response_body.rb', line 4

def make_methods_to_alter_response_array
  @response_array_item = @stub_config.response.body
  make_method_to_append_response_array
  make_method_to_append_response_array_as_hash_params
  make_method_to_alter_response_array_object
end

#make_methods_to_alter_response_objectObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/poisol/stub/response/response_body_builder.rb', line 13

def make_methods_to_alter_response_object
  response_body = @stub_config.response.body.clone
  response_body.each do |field|
    field_name = field[0]
    actual_field_value = field[1]
    is_array = (actual_field_value.class.to_s == "Array")
    if is_array 
      make_method_to_alter_response_nested_array field_name,actual_field_value
    else 
      make_method_to_alter_response_field field_name,actual_field_value
    end
  end
end

#prepare_response_bodyObject



4
5
6
7
8
9
10
11
# File 'lib/poisol/stub/response/response_body_builder.rb', line 4

def prepare_response_body
  return if @stub_config.response.body.blank?
  if  @stub_config.response.is_column_array or @stub_config.response.is_row_array
    make_methods_to_alter_response_array
  else
    make_methods_to_alter_response_object 
  end
end