Module: RorHack::ObjectHack
- Included in:
- Object
- Defined in:
- lib/ror_hack/object_hack.rb
Instance Method Summary collapse
- #exist_and_eql?(other) ⇒ Boolean
-
#instance_variable_fetch(name, value = nil) ⇒ Object
实例变量获取,如果不存在,则设为某个值.
- #is?(other) ⇒ Boolean
- #is_not?(other) ⇒ Boolean
- #is_not_a?(arg) ⇒ Boolean
- #not_in?(arg) ⇒ Boolean
- #p2a ⇒ Object
- #p2h ⇒ Object
- #p2n ⇒ Object
- #p2ros ⇒ Object
- #p2s ⇒ Object
-
#passport_block(object, variable, value) ⇒ Object
可能想给has_one传递参数,用此可以设定对象的属性,来获取一些特殊关联。.
Instance Method Details
#exist_and_eql?(other) ⇒ 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
17 18 19 |
# File 'lib/ror_hack/object_hack.rb', line 17 def is?(other) self == other end |
#is_not?(other) ⇒ 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
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
46 47 48 |
# File 'lib/ror_hack/object_hack.rb', line 46 def not_in?(arg) !self.in?(arg) end |
#p2a ⇒ Object
68 69 70 71 72 |
# File 'lib/ror_hack/object_hack.rb', line 68 def p2a result = presence result = [] if result.nil? result end |
#p2h ⇒ Object
62 63 64 65 66 |
# File 'lib/ror_hack/object_hack.rb', line 62 def p2h result = presence result = {} if result.nil? result end |
#p2n ⇒ Object
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 |
#p2ros ⇒ Object
74 75 76 77 |
# File 'lib/ror_hack/object_hack.rb', line 74 def p2ros result = presence result = OpenStruct.new if result.nil? end |
#p2s ⇒ Object
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 |