Module: RorHack::ObjectHack
- Included in:
- Object
- Defined in:
- lib/ror_hack/object_hack.rb
Instance Method Summary collapse
- #current_class_namespace(str) ⇒ Object
- #current_namespace(str) ⇒ Object
- #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
#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
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
26 27 28 |
# File 'lib/ror_hack/object_hack.rb', line 26 def is?(other) self == other end |
#is_not?(other) ⇒ 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
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
55 56 57 |
# File 'lib/ror_hack/object_hack.rb', line 55 def not_in?(arg) !self.in?(arg) end |
#p2a ⇒ Object
77 78 79 80 81 |
# File 'lib/ror_hack/object_hack.rb', line 77 def p2a result = presence result = [] if result.nil? result end |
#p2h ⇒ Object
71 72 73 74 75 |
# File 'lib/ror_hack/object_hack.rb', line 71 def p2h result = presence result = {} if result.nil? result end |
#p2n ⇒ Object
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 |
#p2ros ⇒ Object
83 84 85 86 |
# File 'lib/ror_hack/object_hack.rb', line 83 def p2ros result = presence result = OpenStruct.new if result.nil? end |
#p2s ⇒ Object
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 |