Class: Object

Inherits:
BasicObject
Includes:
InstanceExecHelper
Defined in:
lib/spiderfw/utils/monkey/object.rb,
lib/spiderfw/utils/monkey/object.rb

Defined Under Namespace

Modules: InstanceExecHelper

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/spiderfw/utils/monkey/object.rb', line 29

def blank?
    respond_to?(:empty?) ? empty? : !self
end

#convert_object(encoding = 'UTF-8') ⇒ Object

metodo ricorsivo per convertire in utf-8 tutto un oggetto con array o hash all’interno



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/spiderfw/utils/monkey/object.rb', line 34

def convert_object(encoding='UTF-8')
    if self.respond_to?(:each_pair)
        self.each_pair{ |chiave, valore|
            valore.convert_object
        }
    elsif self.respond_to?(:each) && !self.is_a?(String)
        #controllo se sto ciclando su un modello
        if self.class < Spider::Model::BaseModel
            #ritorna le chiavi degli elementi
            
            self.class.elements.each_pair{ |chiave_hash_modello, valore_hash_modello|
                self[chiave_hash_modello].convert_object unless self[chiave_hash_modello].respond_to?(:model)
            }
        else
            #converto i singoli valori
            self.each{ |valore_array|
                valore_array.convert_object
            }
        end
    elsif self.is_a?(String)
        if RUBY_VERSION =~ /1.8/
            require 'iconv'
            self.replace(Iconv.iconv(encoding, encoding, self).first)
        elsif RUBY_VERSION =~ /1.9/
            self_dup = self.dup
            self.replace(self_dup.force_encoding(encoding)) unless self.frozen?
        else
            self_dup = self.dup
            self.replace(self_dup.force_encoding(encoding)) unless self.frozen?
        end
    end
end

#instance_exec(*args, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/spiderfw/utils/monkey/object.rb', line 7

def instance_exec(*args, &block)
    begin
        old_critical, Thread.critical = Thread.critical, true
        n = 0
        n += 1 while respond_to?(mname="__instance_exec#{n}")
        InstanceExecHelper.module_eval{ define_method(mname, &block) }
    ensure
        Thread.critical = old_critical
    end
    begin
        ret = send(mname, *args)
    ensure
        InstanceExecHelper.module_eval{ remove_method(mname) } rescue nil
    end
    ret
end