Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/quickbooks/extlib/object.rb,
lib/quickbooks/extlib/inflection.rb,
lib/quickbooks/extlib/days_and_times/object.rb
Instance Method Summary collapse
- #bind_class_object_method(other, self_method_name, other_method_name, args = [[],[]]) ⇒ Object
- #bind_object_method(other, self_method_name, other_method_name, args = [[],[]]) ⇒ Object
-
#full_const_get(name) ⇒ Object
The constant corresponding to the name.
-
#full_const_set(name, value) ⇒ Object
The constant corresponding to the name.
-
#in?(arrayish, *more) ⇒ TrueClass, FalseClass
True if the object is included in arrayish (+ more).
- #instance_variable_defined?(variable) ⇒ Boolean
-
#make_module(str) ⇒ NilClass
Defines module from a string name (e.g. Foo::Bar::Baz) If module already exists, no exception raised.
-
#meta_class ⇒ Class
Extracts the singleton class, so that metaprogramming can be done on it.
- #plural ⇒ Object (also: #pluralize)
-
#quacks_like?(duck) ⇒ TrueClass, FalseClass
True if the object quacks like duck.
- #singular ⇒ Object (also: #singularize)
-
#try_dup ⇒ Object
Override this in a child if it cannot be dup’ed.
Instance Method Details
#bind_class_object_method(other, self_method_name, other_method_name, args = [[],[]]) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 |
# File 'lib/quickbooks/extlib/days_and_times/object.rb', line 2 def bind_class_object_method(other, self_method_name, other_method_name, args=[[],[]]) # Since I can't pass the 'other' object into eval as a string, I have to # set a class instance variable and copy the contents to a class variable # so that the generated method will play nicely with subclasses. self.instance_variable_set("@#{self_method_name.to_s}_OBJ", other) self.send :eval, "@@#{self_method_name.to_s}_OBJ = @#{self_method_name.to_s}_OBJ def #{self_method_name.to_s}(#{args[0].join(', ')}) @@#{self_method_name.to_s}_OBJ.#{other_method_name.to_s}(#{args[1].join(', ')}) end" self end |
#bind_object_method(other, self_method_name, other_method_name, args = [[],[]]) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/quickbooks/extlib/days_and_times/object.rb', line 13 def bind_object_method(other, self_method_name, other_method_name, args=[[],[]]) self.instance_variable_set("@#{self_method_name}_OBJ", other) eval "def self.#{self_method_name}(#{args[0].join(', ')}) @#{self_method_name}_OBJ.#{other_method_name}(#{args[1].join(', ')}) end" end |
#full_const_get(name) ⇒ Object
Returns The constant corresponding to the name.
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/quickbooks/extlib/object.rb', line 64 def full_const_get(name) list = name.split("::") list.shift if list.first.blank? obj = self list.each do |x| # This is required because const_get tries to look for constants in the # ancestor chain, but we only want constants that are HERE obj = obj.const_defined?(x) ? obj.const_get(x) : obj.const_missing(x) end obj end |
#full_const_set(name, value) ⇒ Object
Returns The constant corresponding to the name.
80 81 82 83 84 85 86 87 |
# File 'lib/quickbooks/extlib/object.rb', line 80 def full_const_set(name, value) list = name.split("::") toplevel = list.first.blank? list.shift if toplevel last = list.pop obj = list.empty? ? Object : Object.full_const_get(list.join("::")) obj.const_set(last, value) if obj && !obj.const_defined?(last) end |
#in?(arrayish, *more) ⇒ TrueClass, FalseClass
Returns True if the object is included in arrayish (+ more).
147 148 149 150 |
# File 'lib/quickbooks/extlib/object.rb', line 147 def in?(arrayish,*more) arrayish = more.unshift(arrayish) unless more.empty? arrayish.include?(self) end |
#instance_variable_defined?(variable) ⇒ Boolean
158 159 160 |
# File 'lib/quickbooks/extlib/object.rb', line 158 def instance_variable_defined?(variable) instance_variables.include?(variable.to_s) end |
#make_module(str) ⇒ NilClass
Defines module from a string name (e.g. Foo::Bar::Baz) If module already exists, no exception raised.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/quickbooks/extlib/object.rb', line 95 def make_module(str) mod = str.split("::") current_module = self mod.each do |x| unless current_module.const_defined?(x) current_module.class_eval "module #{x}; end" end current_module = current_module.const_get(x) end current_module end |
#meta_class ⇒ Class
Extracts the singleton class, so that metaprogramming can be done on it.
59 |
# File 'lib/quickbooks/extlib/object.rb', line 59 def () class << self; self end end |
#plural ⇒ Object Also known as: pluralize
431 432 433 434 |
# File 'lib/quickbooks/extlib/inflection.rb', line 431 def plural raise MethodNotFound, caller(1) unless self.respond_to?(:to_s) Extlib::Inflection.plural(to_s) end |
#quacks_like?(duck) ⇒ TrueClass, FalseClass
The behavior of the method depends on the type of duck as follows:
- Symbol
-
Check whether the object respond_to?(duck).
- Class
-
Check whether the object is_a?(duck).
- Array
-
Check whether the object quacks_like? at least one of the options in the array.
Returns True if the object quacks like duck.
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/quickbooks/extlib/object.rb', line 119 def quacks_like?(duck) case duck when Symbol self.respond_to?(duck) when Class self.is_a?(duck) when Array duck.any? {|d| self.quacks_like?(d) } else false end end |
#singular ⇒ Object Also known as: singularize
426 427 428 429 |
# File 'lib/quickbooks/extlib/inflection.rb', line 426 def singular raise MethodNotFound, caller(1) unless self.respond_to?(:to_s) Extlib::Inflection.singular(to_s) end |
#try_dup ⇒ Object
Override this in a child if it cannot be dup’ed
135 136 137 |
# File 'lib/quickbooks/extlib/object.rb', line 135 def try_dup self.dup end |