Module: Bukovina::Importers::FindInit

Included in:
Common, Memo
Defined in:
lib/bukovina/importers/find_init.rb

Instance Method Summary collapse

Instance Method Details

#foreign_class(base, attr) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bukovina/importers/find_init.rb', line 2

def foreign_class base, attr
a= if not base.reflections[attr.to_s]
   'array'
elsif base.reflections[attr.to_s].source_reflection
   if base.reflections[attr.to_s].options[:class_name].present?
      base.reflections[attr.to_s].options[:class_name]
   elsif base.reflections[attr.to_s].options[:source].present?
      base.reflections[attr.to_s].options[:source]
   elsif base.reflections[attr.to_s].options[:as].present?
      attr
   else
#            base.reflections[attr.to_s].foreign_key.to_s.gsub( "_id",'' )
      base.reflections[attr.to_s].name
   end
else
   through = base.reflections[attr.to_s].through_reflection
   through_class = through.class_name.constantize
#         binding.pry
   if class_name = base.reflections[attr.to_s].options[:class_name]
      class_name
   else
      foreign_key = base.reflections[attr.to_s].options[ :foreign_key ]
      kind = foreign_key.to_s.gsub( "_id",'' )
      through_class.reflections[ kind ].name ;end ;end
#      binding.pry if attr == :memory_names
#      binding.pry if attr == :descriptions


a.to_s.classify.constantize
   rescue
binding.pry ;end

#parse_array(base, array) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bukovina/importers/find_init.rb', line 71

def parse_array base, array
#      binding.pry
      array.map do |value|
         case value
         when Hash
binding.pry if ! base.respond_to?(:reflections)#.reflections[attr.to_s]
parse_hash( base, value )
         when /^\*(.*)/
$1.present? && value || "^#{@short_name}"
         else
value ;end ;end ;end

#parse_hash(base, hash) ⇒ Object



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
63
64
65
# File 'lib/bukovina/importers/find_init.rb', line 34

def parse_hash base, hash
hash.map do |(attr, value)|
   case value
   when Array
      binding.pry if ! base.respond_to?(:reflections)#.reflections[attr.to_s]
      new_base = foreign_class( base, attr )
#            binding.pry
#            binding.pry if ! new_base.respond_to?(:reflections)#.reflections[attr.to_s]
      new_array = parse_array( new_base, value )
      if new_array.any? {|v| v.is_a?( Hash ) }
         [ :"#{attr}_attributes", new_array ]
      else
         [ attr, new_array ] ;end
   when Hash
#            binding.pry if ! base.reflections[attr.to_s]
      binding.pry if ! base.respond_to?(:reflections)#.reflections[attr.to_s]
      new_base = foreign_class( base, attr )
#            binding.pry if ! new_base.respond_to?(:reflections)#.reflections[attr.to_s]
      new_value = parse_hash( new_base, value )
      if new_value.is_a?( Hash )
         [ :"#{attr}_attributes", new_value ]
      else
         [ attr, new_value ] ;end
#         when /\A\*(?<newvalue>.*)/
#            newvalue = $1
#            raise "To be fixed so: attr #{attr} => #{value}"
#            [ attr, value ]
   else
      [ attr, value ] ;end;end
.compact.to_h
   rescue
binding.pry ;end

#separate_hash(hash) ⇒ Object



67
68
69
# File 'lib/bukovina/importers/find_init.rb', line 67

def separate_hash hash
search_hash = hash.select { |(attr, _)| /_attributes\z/ !~ attr }.to_h
[ search_hash, hash ] ;end