Module: MPatch::Include::Object
- Defined in:
- lib/mpatch/object.rb
Instance Method Summary collapse
- #boolean? ⇒ Boolean
- #class? ⇒ Boolean
-
#class_def(name, &blk) ⇒ Object
Defines an instance method within a class.
-
#class_exists? ⇒ Boolean
this will check that the class is defined or not in the runtime memory.
-
#constantize ⇒ Object
constantize object.
-
#convert_to_class(*attributes) ⇒ Object
(also: #create_attributes)
This will convert a symbol or string and format to be a valid constant name and create from it a class with instance attribute accessors Best use is when you get raw data in string from external source and you want make them class objects.
-
#convert_to_hash ⇒ Object
convert class instance instance variables into a hash object.
- #false? ⇒ Boolean
-
#meta_def(name, &blk) ⇒ Object
Adds methods to a metaclass.
-
#meta_eval(&blk) ⇒ Object
extend the metaclass with an instance eval.
-
#metaclass ⇒ Object
The hidden singleton lurks behind everyone.
-
#must_be(class_name) ⇒ Object
basic validations for those who fear the DUCK!.
-
#privatize(opts = {}) ⇒ Object
this is used for make private methods in an object you can also use this to convert already defined methods in an object or class use:.
- #true? ⇒ Boolean
Instance Method Details
#boolean? ⇒ Boolean
7 8 9 |
# File 'lib/mpatch/object.rb', line 7 def boolean? !!self == self end |
#class? ⇒ Boolean
19 20 21 |
# File 'lib/mpatch/object.rb', line 19 def class? self.class == ::Class end |
#class_def(name, &blk) ⇒ Object
Defines an instance method within a class
53 54 55 |
# File 'lib/mpatch/object.rb', line 53 def class_def name, &blk class_eval { define_method name, &blk } end |
#class_exists? ⇒ Boolean
this will check that the class is defined or not in the runtime memory
91 92 93 94 95 96 |
# File 'lib/mpatch/object.rb', line 91 def class_exists? klass = ::Module.const_get(self) return klass.is_a?(::Class) rescue ::NameError return false end |
#constantize ⇒ Object
constantize object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/mpatch/object.rb', line 58 def constantize camel_cased_word= self.to_s names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = ::Object names.each do |name| constant = constant.const_defined?(name, false) ? constant.const_get(name) : constant.const_missing(name) end constant end |
#convert_to_class(*attributes) ⇒ Object Also known as: create_attributes
This will convert a symbol or string and format to be a valid constant name and create from it a class with instance attribute accessors Best use is when you get raw data in string from external source and you want make them class objects
:hello_world.to_class(:test)
HelloWorld.to_class(:sup)
hw_var = HelloWorld.new
hw_var.sup = "Fine thanks!"
hw_var.test = 5
puts hw_var.test
#> produce 5 :Integer
you can also use this formats
:HelloWorld , "hello.world",
"hello/world", "Hello::World",
"hello:world"...
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/mpatch/object.rb', line 117 def convert_to_class(*attributes) unless self.class <= ::Symbol || self.class <= ::String || self.class <= ::Class raise ::ArgumentError, "object must be symbol or string to make able build class to it" end class_name= self.to_s unless self.class <= ::Class class_name= class_name[0].upcase+class_name[1..class_name.length] %w[ _ . : / ].each do |one_sym| loop do index_nmb= class_name.index(one_sym) break if index_nmb.nil? class_name[index_nmb..index_nmb+1]= class_name[index_nmb+1].upcase end end end create_attribute = ::Proc.new do |*args| end unless class_name.class_exists? self.class.const_set( class_name, ::Class.new ) end class_name.constantize.class_eval do attributes.each do |one_attribute| attr_accessor one_attribute.to_s.to_sym end end return true end |
#convert_to_hash ⇒ Object
convert class instance instance variables into a hash object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mpatch/object.rb', line 72 def convert_to_hash unless self.class.class <= ::Class super #raise ::NoMethodError, "undefined method `to_hash' for #{self.inspect}" end tmp_hash= ::Hash.new() self.instance_variables.each do|var| tmp_hash[var.to_s.delete("@")] = self.instance_variable_get(var) end return tmp_hash end |
#false? ⇒ Boolean
15 16 17 |
# File 'lib/mpatch/object.rb', line 15 def false? self == false end |
#meta_def(name, &blk) ⇒ Object
Adds methods to a metaclass
48 49 50 |
# File 'lib/mpatch/object.rb', line 48 def name, &blk { define_method name, &blk } end |
#meta_eval(&blk) ⇒ Object
extend the metaclass with an instance eval
45 |
# File 'lib/mpatch/object.rb', line 45 def &blk; .instance_eval &blk; end |
#metaclass ⇒ Object
The hidden singleton lurks behind everyone
42 |
# File 'lib/mpatch/object.rb', line 42 def ; class << self; self; end; end |
#must_be(class_name) ⇒ Object
basic validations for those who fear the DUCK!
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/mpatch/object.rb', line 24 def must_be class_name if class_name.class == ::Class begin if self.class != class_name raise ::ArgumentError, "invalid parameter given: #{self}" end end else begin if self != class_name raise ::ArgumentError, "invalid parameter given: #{self}" end end end return self end |
#privatize(opts = {}) ⇒ Object
this is used for make private methods in an object you can also use this to convert already defined methods in an object or class use:
privatize in: 'hello_world' #> make hello world method private in the self obj
privatize target: 'instance' #> you can use this in a class to make instance methods private
privatize target: 'singleton' #> you can use this in a class to make self methods private
privatize ex: Symbol/String/Array #> you can use this for make exceptions what should be not touched in the prcs
privatize in: Symbol/String/Array #> you can use this for make targeted collection of methods for privatize
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/mpatch/object.rb', line 180 def privatize( opts= {} ) unless opts.class <= Hash raise ArgumentError,"invalid input for options" end %W[ e ex exc ].each do |str_sym| opts[:exclude] ||= opts[str_sym.to_sym] end %W[ i in inc only methods ].each do |str_sym| opts[:include] ||= opts[str_sym.to_sym] end %W[ t target ].each do |str_sym| opts[:target] ||= opts[str_sym.to_sym] end opts[:target] ||= 's' opts[:target]= opts[:target].to_s.downcase unless opts[:target][0] == "s" || opts[:target][0] == "i" %W[ singleton instance ].include?(opts[:target].to_s) raise ArgumentError, [ "invalid options for target, you should use the following: ", "\n\tsingleton for targeting the singleton class what is de", "fault\n\tinstance for targeting the object instance methods." ].join end opts[:exclude] ||= [] if opts[:target][0] == 'i' && self.class <= Module opts[:include] ||= self.instance_methods.map{|e| e.to_s } else opts[:target]= 's' opts[:include] ||= self.methods.map{|e| e.to_s } end [:include,:exclude].each do |array_name| unless opts[array_name].class <= Array opts[array_name]= [ opts[array_name] ] end opts[array_name].map!{ |element| ( element.class == String ? element : element.to_s ) } end opts[:exclude] + %W[ __send__ object_id ] if opts[:target][0] == 's' self.instance_eval do opts[:include].each do |sym| unless opts[:exclude].include?(sym) .__send__ :private, sym end end end elsif opts[:target][0] == 'i' opts[:include].each do |sym| unless opts[:exclude].include?(sym) self.__send__ :private, sym end end else STDERR.puts "invalid target definition" end end |
#true? ⇒ Boolean
11 12 13 |
# File 'lib/mpatch/object.rb', line 11 def true? self == true end |