Module: RorHack::ObjectHack

Included in:
Object
Defined in:
lib/ror_hack/object_hack.rb

Instance Method Summary collapse

Instance Method Details

#exist_and_eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
# File 'lib/ror_hack/object_hack.rb', line 3

def exist_and_eql?(other)
  self && (self == other)
end

#instance_variable_fetch(name, value = nil) ⇒ Object

实例变量获取,如果不存在,则设为某个值



8
9
10
11
12
13
14
15
# File 'lib/ror_hack/object_hack.rb', line 8

def instance_variable_fetch(name, value = nil)
  instance_variable_get("@#{ name }").tap do |i|
    return i unless i.is?(nil)
  end
  block_given? && my_value = yield
  value && my_value = value
  instance_variable_set("@#{ name }", my_value)
end

#is?(other) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/ror_hack/object_hack.rb', line 17

def is?(other)
  self == other
end

#is_not?(other) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/ror_hack/object_hack.rb', line 21

def is_not?(other)
  !is?(other)
end

#is_not_a?(arg) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/ror_hack/object_hack.rb', line 42

def is_not_a?(arg)
  !self.is_a?(arg)
end

#not_in?(arg) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ror_hack/object_hack.rb', line 46

def not_in?(arg)
  !self.in?(arg)
end

#p2aObject



68
69
70
71
72
# File 'lib/ror_hack/object_hack.rb', line 68

def p2a
  result = presence
  result = [] if result.nil?
  result
end

#p2hObject



62
63
64
65
66
# File 'lib/ror_hack/object_hack.rb', line 62

def p2h
  result = presence
  result = {} if result.nil?
  result
end

#p2nObject



50
51
52
53
54
# File 'lib/ror_hack/object_hack.rb', line 50

def p2n
  result = presence
  result = 0 if result.nil?
  result
end

#p2rosObject



74
75
76
77
# File 'lib/ror_hack/object_hack.rb', line 74

def p2ros
  result = presence
  result = OpenStruct.new if result.nil?
end

#p2sObject



56
57
58
59
60
# File 'lib/ror_hack/object_hack.rb', line 56

def p2s
  result = presence
  result = '' if result.nil?
  result
end

#passport_block(object, variable, value) ⇒ Object

可能想给has_one传递参数,用此可以设定对象的属性,来获取一些特殊关联。



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ror_hack/object_hack.rb', line 26

def passport_block(object, variable, value)
  new = false
  unless object.instance_variable_defined?("@#{variable}")
    object.instance_variable_set("@#{variable}", nil)
    new = true
  end
  tmp_value = object.instance_variable_get("@#{variable}")
  begin
    object.instance_variable_set("@#{variable}", value)
    yield
  ensure
    object.instance_variable_set("@#{variable}", tmp_value)
    object.remove_instance_variable("@#{variable}") if new
  end
end