Class: Object

Inherits:
BasicObject
Defined in:
lib/qbxml/support.rb,
lib/quickbooks/ruby_magic.rb

Instance Method Summary collapse

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

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/qbxml/support.rb', line 8

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

Returns:

  • (Boolean)


43
44
45
# File 'lib/quickbooks/ruby_magic.rb', line 43

def error?
  !@errors.empty?
end

#errorsObject

This way anything, even nil, can hold on to an error message to be picked up later.



40
41
42
# File 'lib/quickbooks/ruby_magic.rb', line 40

def errors
  @errors ||= []
end

#is_not_one_of?(*ary) ⇒ Boolean

The opposite of is_one_of?

Returns:

  • (Boolean)


35
36
37
# File 'lib/quickbooks/ruby_magic.rb', line 35

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]).

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


24
25
26
27
# File 'lib/qbxml/support.rb', line 24

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

#to_aObject

Wraps any non-Array into an array



24
25
26
# File 'lib/quickbooks/ruby_magic.rb', line 24

def to_a
  self.is_a?(Array) ? self : [self]
end