Module: RorHack::ObjectHack

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

Instance Method Summary collapse

Instance Method Details

#current_class_namespace(str) ⇒ Object



8
9
10
# File 'lib/ror_hack/object_hack.rb', line 8

def current_class_namespace(str)
  "#{self.class.parent_name}::#{str.to_s.camelize}"
end

#current_namespace(str) ⇒ Object



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

def current_namespace(str)
  "#{self.parent_name}::#{str.to_s.camelize}"
end

#exist_and_eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/ror_hack/object_hack.rb', line 12

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

#instance_variable_fetch(name, value = nil) ⇒ Object

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



17
18
19
20
21
22
23
24
# File 'lib/ror_hack/object_hack.rb', line 17

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)


26
27
28
# File 'lib/ror_hack/object_hack.rb', line 26

def is?(other)
  self == other
end

#is_not?(other) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/ror_hack/object_hack.rb', line 30

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

#is_not_a?(arg) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#not_in?(arg) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/ror_hack/object_hack.rb', line 55

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

#p2aObject



77
78
79
80
81
# File 'lib/ror_hack/object_hack.rb', line 77

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

#p2hObject



71
72
73
74
75
# File 'lib/ror_hack/object_hack.rb', line 71

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

#p2nObject



59
60
61
62
63
# File 'lib/ror_hack/object_hack.rb', line 59

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

#p2rosObject



83
84
85
86
# File 'lib/ror_hack/object_hack.rb', line 83

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

#p2sObject



65
66
67
68
69
# File 'lib/ror_hack/object_hack.rb', line 65

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

#passport_block(object, variable, value) ⇒ Object

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ror_hack/object_hack.rb', line 35

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