module RorHack::ObjectHack
Public Instance Methods
current_class_namespace(str)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/ror_hack/object_hack.rb, line 12 def exist_and_eql?(other) self && (self == other) end
instance_variable_fetch(name, value = nil) { || ... }
click to toggle source
实例变量获取,如果不存在,则设为某个值
# 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)
click to toggle source
# File lib/ror_hack/object_hack.rb, line 26 def is?(other) self == other end
is_not?(other)
click to toggle source
# File lib/ror_hack/object_hack.rb, line 30 def is_not?(other) !is?(other) end
is_not_a?(arg)
click to toggle source
# File lib/ror_hack/object_hack.rb, line 51 def is_not_a?(arg) !self.is_a?(arg) end
not_in?(arg)
click to toggle source
# File lib/ror_hack/object_hack.rb, line 55 def not_in?(arg) !self.in?(arg) end
p2a()
click to toggle source
# File lib/ror_hack/object_hack.rb, line 77 def p2a result = presence result = [] if result.nil? result end
p2h()
click to toggle source
# File lib/ror_hack/object_hack.rb, line 71 def p2h result = presence result = {} if result.nil? result end
p2n()
click to toggle source
# File lib/ror_hack/object_hack.rb, line 59 def p2n result = presence result = 0 if result.nil? result end
p2ros()
click to toggle source
# File lib/ror_hack/object_hack.rb, line 83 def p2ros result = presence result = OpenStruct.new if result.nil? end
p2s()
click to toggle source
# File lib/ror_hack/object_hack.rb, line 65 def p2s result = presence result = '' if result.nil? result end
passport_block(object, variable, value) { || ... }
click to toggle source
可能想给has_one传递参数,用此可以设定对象的属性,来获取一些特殊关联。
# 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