Module: Positify

Defined in:
lib/positify.rb,
lib/positify/version.rb

Defined Under Namespace

Modules: VERSION

Class Method Summary collapse

Class Method Details

.it(*args, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/positify.rb', line 5

def self.it(*args, &block)
  if block_given?
    raise(ArgumentError, "You can only pass in one options Hash in block mode, not #{args.inspect}") if args.size > 1
    object  = block.call
    options = args.shift || {}
  else
    case args.size
    when 1
      object  = args.shift
      options = {}
      raise(ArgumentError, "You forgot the brackets in your call. Try: Positify::it(...) do") if object.is_a?(Hash)
    when 2
      object  = args.shift
      options = args.shift
    else
      raise ArgumentError, "You must pass in an object and may pass in an options Hash, not #{args.inspect}"
    end
  end

  options[:max] = it(options[:max]) if options[:max]

  return 1 unless object.respond_to?(:to_i)
  result = object.to_i.abs > 0 ? object.to_i.abs : 1
  result = options[:max] if options[:max] && options[:max] < result
  result
end