Top Level Namespace

Defined Under Namespace

Modules: CucumberOpenERP, MemoizerUtils, OoorUtils, SequelUtils Classes: BasicObject, OoorProxy, ScenarioUtils, SequelProxy

Instance Method Summary collapse

Instance Method Details

#_item_value(relations, field_name, input_value, value_type) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cucumber/openerp/dsl.rb', line 63

def _item_value(relations, field_name, input_value, value_type)
  return input_value if %w(char text).include?(value_type)

  # we want to empty the data
  return false if input_value.match(/^(false|0|no|f|n|nil)$/i)

  # the field is a relation
  if relations[field_name]
    rel_item = _manage_col_search(relations[field_name], input_value)
    return rel_item.id
  end

  # other data types
  value = case value_type
          when 'integer' then Integer(input_value)
          when 'float' then Float(input_value)
          when 'boolean' then true  # false values are already trapped
          when 'date', 'datetime' then
            input_value.include?('%') ? Time.new.strftime(input_value) : input_value
          else
            input_value
          end
  value
end

#_manage_col_search(field_def, value) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cucumber/openerp/dsl.rb', line 43

def _manage_col_search(field_def, value)
  oclass = Object.const_get(field_def['relation'].split('.').collect {|s| s.capitalize}.join) # TODO use Scenario helper
  oid = nil
  if value.start_with? 'by '
    comm_ext, arguments = prepare_args(value.gsub('by ', ''))
    arguments.push({:fields=>['id']})
    oid = oclass.send(('find_by_'+comm_ext).to_sym, *arguments)
  elsif value.start_with? 'all by'
    comm_ext, arguments = prepare_args(value.gsub('all by', ''))
    arguments.push({:fields=>['id']})
    oid = oclass.send(('find_all_by_'+comm_ext).to_sym, *arguments)
  else
    eval "oid = oclass.#{value}"
  end
  unless oid
    raise "Can not find #{value}"
  end
  return oid
end

#get_items(action, qty, model, args) ⇒ Object



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
# File 'lib/cucumber/openerp/dsl.rb', line 14

def get_items(action, qty, model, args)
  @found_items = nil
  @found_item = nil
  oclass =  Object.const_get(model.split('.').collect {|s| s.capitalize}.join)
  unique_mode = ['create', 'need', 'find or create']
  if qty == 'all'
    if unique_mode.include? action
      raise "#{unique_mode.inspect} does not allows 'all' keyword"
    end
    qty = 'all_'
  else
    qty = ''
  end
  command_map = {'create' => 'new', 'need' => 'find_or_initialize_by_', 'should have' => "find_#{qty}by_", 'find' => "find_#{qty}by_", 'find or create' => 'find_or_initialize_by_'}
  comm_ext, arguments = prepare_args(args)
  arguments.push({:fields=>['id']})
  if command_map[action] == 'create'
    if oclass.send(('find_by_'+comm_ext).to_sym, *arguments)
      raise "Error. This object already exists, if you want to update or use it, you have to use the sentence starting with \"I need...\" or \"I should have\""
    end
  end
  res = oclass.send((command_map[action]+comm_ext).to_sym, *arguments) # I know in case or create we do 2 search but as there should be nul it cost almost nothing
  if @found_items.is_a? Array
    @found_items = res
  else
    @found_item = res
  end
end

#manage_item_table(item, table) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cucumber/openerp/dsl.rb', line 88

def manage_item_table(item, table)
  relations = {}
  relations.merge! item.class.many2one_associations
  relations.merge! item.class.one2many_associations
  relations.merge! item.class.many2many_associations

  table.hashes.each do |dict|
    field_name = dict['name']
    value = dict['value']
    value_type = item.class.fields.fetch(field_name, {}).fetch('type', '')
    r_val = _item_value(relations, field_name, value, value_type)
    if item.class.one2many_associations.include? field_name or item.class.many2many_associations.include? field_name
      if r_val
        unless r_val.is_a? Array
          r_val = [r_val]
        end
      end
    end
    item.send("#{field_name}=".to_sym, r_val)
  end
end

#prepare_args(args) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
# File 'lib/cucumber/openerp/dsl.rb', line 1

def prepare_args(args)
  mixed = args.strip.split(' and ')
  attrs = []
  arguments = []
  mixed.each do |set|
    tmp = set.split(':')
    attrs.push(tmp[0].strip)
    arguments.push(tmp[1].strip)
  end
  attrs_string = attrs.join('_and_')
  return attrs_string, arguments
end