Top Level Namespace

Defined Under Namespace

Modules: YeshouaCrm

Instance Method Summary collapse

Instance Method Details

#check_version_format(version) ⇒ Object



55
56
57
# File 'lib/yeshoua_crm.rb', line 55

def check_version_format(version)
  version =~ /^\d+.?\d*.?\d*$/m
end

#compare_versions(requirement, current) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/yeshoua_crm.rb', line 49

def compare_versions(requirement, current)
  raise ArgumentError.new('wrong version format') unless check_version_format(requirement)
  requirement = requirement.split('.').collect(&:to_i)
  requirement <=> current.slice(0, requirement.size)
end

#requires_yeshoua_crm(arg) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yeshoua_crm.rb', line 48

def requires_yeshoua_crm(arg)
  def compare_versions(requirement, current)
    raise ArgumentError.new('wrong version format') unless check_version_format(requirement)
    requirement = requirement.split('.').collect(&:to_i)
    requirement <=> current.slice(0, requirement.size)
  end

  def check_version_format(version)
    version =~ /^\d+.?\d*.?\d*$/m
  end

  arg = {:version_or_higher => arg} unless arg.is_a?(Hash)
  arg.assert_valid_keys(:version, :version_or_higher)

  current = YeshouaCrm::VERSION.split('.').map { |x| x.to_i }
  arg.each do |k, req|
    case k
      when :version_or_higher
        raise ArgumentError.new(':version_or_higher accepts a version string only') unless req.is_a?(String)
        unless compare_versions(req, current) <= 0
          Rails.logger.error "\033[31m[ERROR]\033[0m Yeshoua requires yeshoua_crm gem version #{req} or higher (you're using #{YeshouaCrm::VERSION}).\n\033[31m[ERROR]\033[0m Please update with 'bundle update yeshoua_crm'." if Rails.logger
          abort "\033[31mYeshoua requires yeshoua_crm gem version #{req} or higher (you're using #{YeshouaCrm::VERSION}).\nPlease update with 'bundle update yeshoua_crm'.\033[0m"
        end
      when :version
        req = [req] if req.is_a?(String)
        if req.is_a?(Array)
          unless req.detect { |ver| compare_versions(ver, current) == 0 }
            abort "\033[31mYeshoua requires yeshoua_crm gem version #{req} (you're using #{YeshouaCrm::VERSION}).\nPlease update with 'bundle update yeshoua_crm'.\033[0m"
          end
        elsif req.is_a?(Range)
          unless compare_versions(req.first, current) <= 0 && compare_versions(req.last, current) >= 0
            abort "\033[31mYeshoua requires yeshoua_crm gem version between #{req.first} and #{req.last} (you're using #{YeshouaCrm::VERSION}).\nPlease update with 'bundle update yeshoua_crm'.\033[0m"
          end
        else
          raise ArgumentError.new(':version option accepts a version string, an array or a range of versions')
        end
    end
  end
  true
end