Skip to content

Commit 697f1a4

Browse files
committed
Add utility class to hold object_is_any_of?
1 parent 155d62e commit 697f1a4

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/octocatalog-diff/util/util.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
# Handy methods that are not tied to one particular class
4+
5+
module OctocatalogDiff
6+
module Util
7+
# Helper class to construct catalogs, performing all necessary steps such as
8+
# bootstrapping directories, installing facts, and running puppet.
9+
class Util
10+
# Utility Method!
11+
# `is_a?(class)` only allows one method, but this uses an array
12+
# @param object [?] Object to consider
13+
# @param classes [Array] Classes to determine if object is a member of
14+
# @return [Boolean] True if object is_a any of the classes, false otherwise
15+
def self.object_is_any_of?(object, classes)
16+
classes.each { |clazz| return true if object.is_a? clazz }
17+
false
18+
end
19+
end
20+
end
21+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../spec_helper'
4+
require 'ostruct'
5+
require OctocatalogDiff::Spec.require_path('/util/util')
6+
7+
describe OctocatalogDiff::Util::Util do
8+
describe '#object_is_any_of?' do
9+
it 'should return true when object is one of the classes' do
10+
object = 42
11+
classes = [Fixnum, Integer]
12+
expect(described_class.object_is_any_of?(object, classes)).to eq(true)
13+
end
14+
15+
it 'should return false when object is not one of the classes' do
16+
object = :chickens
17+
classes = [Fixnum, Integer]
18+
expect(described_class.object_is_any_of?(object, classes)).to eq(false)
19+
end
20+
end
21+
end

0 commit comments

Comments
 (0)