Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/quickbooks/ruby_magic.rb
Overview
This file contains several little tidbits of magic that I’ve made to make certain things easier. See Object, Class, and Hash.
Instance Method Summary collapse
-
#delegate_methods(hsh) ⇒ Object
This allows one to effectively ‘proxy’ specific methods to be called on the return value of one of its methods.
- #error? ⇒ Boolean
-
#errors ⇒ Object
This way anything, even nil, can hold on to an error message to be picked up later.
-
#is_not_one_of?(*ary) ⇒ Boolean
The opposite of is_one_of?.
-
#is_one_of?(*ary) ⇒ Boolean
Normally one would say, “if [:a, :b, :c].include?(:a)” – which is backward thinking, instead you should use this magic: “:a.is_one_of?(:a, :b, :c)”, or :a.is_one_of?([:a, :b, :c]).
-
#to_a ⇒ Object
Wraps any non-Array into an array.
Instance Method Details
#delegate_methods(hsh) ⇒ Object
This allows one to effectively ‘proxy’ specific methods to be called on the return value of one of its methods. For example, obj.length could be delegated to do the same thing as obj.full_text.length
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/quickbooks/ruby_magic.rb', line 7 def delegate_methods(hsh) raise ArgumentError, "delegate_methods should be called like: delegate_methods [:method1, :method2] => :delegated_to_method" unless hsh.keys.first.is_a?(Array) methods = hsh.keys.first delegate_to = hsh[methods] methods.each do |method| self.send(:eval, <<-ddddddd def #{method}(*args, &block) block_given? ? self.#{delegate_to}.#{method}(*args, &block) : self.#{delegate_to}.#{method}(*args) end ddddddd ) end end |
#error? ⇒ Boolean
41 42 43 |
# File 'lib/quickbooks/ruby_magic.rb', line 41 def error? !@errors.blank? end |
#errors ⇒ Object
This way anything, even nil, can hold on to an error message to be picked up later.
38 39 40 |
# File 'lib/quickbooks/ruby_magic.rb', line 38 def errors @errors ||= [] end |
#is_not_one_of?(*ary) ⇒ Boolean
The opposite of is_one_of?
33 34 35 |
# File 'lib/quickbooks/ruby_magic.rb', line 33 def is_not_one_of?(*ary) !is_one_of?(ary) end |
#is_one_of?(*ary) ⇒ Boolean
Normally one would say, “if [:a, :b, :c].include?(:a)” – which is backward thinking, instead you should use this magic: “:a.is_one_of?(:a, :b, :c)”, or :a.is_one_of?([:a, :b, :c]).
28 29 30 31 |
# File 'lib/quickbooks/ruby_magic.rb', line 28 def is_one_of?(*ary) raise ArgumentError, "exists_in? requires an array to be passed" unless ary.is_a?(Array) ary.flatten.include?(self) end |