Module: Speccify::Functions
- Defined in:
- lib/speccify.rb
Overview
Temporary
A temporary module that holds functionality that awaits to be refactored to the right place.
Class Method Summary collapse
-
.build_matcher(matcher_name, args, &block) ⇒ Object
A matcher in speccify is a builder method that returns the right matcher object.
-
.determine_class_name(name) ⇒ Object
:nodoc:.
- .make_constantizeable(string) ⇒ Object
-
.message(expected, actual, op, location) ⇒ Object
:nodoc:.
Class Method Details
.build_matcher(matcher_name, args, &block) ⇒ Object
A matcher in speccify is a builder method that returns the right matcher object. This build_matcher method is the internal interface to the matcher system. (You should use the def_matcher() method to define matchers.)
Example:
# The obj.should be_true matcher.
def be_true
Speccify::Functions.build_matcher(:be_true, []) do |given, matcher, args|
given
end
end
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/speccify.rb', line 91 def self.build_matcher(matcher_name, args, &block) match_block = lambda do |actual, matcher| block.call(actual, matcher, args) end body = lambda do |klass| @matcher_name = matcher_name.to_s def self.matcher_name #:nodoc: @matcher_name end attr_accessor :positive_msg, :negative_msg, :msgs, :loc def initialize match_block #:nodoc: @match_block = match_block end def method_missing id, *args, &block # :nodoc: require 'ostruct' (self.msgs ||= []) << OpenStruct.new( "name" => id, "args" => args, "block" => block ) self end def matches? given #:nodoc: @positive_msg ||= Speccify::Functions::( "#{given} should #{self.class.matcher_name}", "no match", self.class.matcher_name , self.loc || "") @negative_msg ||= Speccify::Functions::( "#{given} should not #{self.class.matcher_name}", "match", self.class.matcher_name , self.loc || "") @match_block.call(given, self) end end Class.new(&body).new(match_block) end |
.determine_class_name(name) ⇒ Object
:nodoc:
74 75 76 |
# File 'lib/speccify.rb', line 74 def self.determine_class_name(name) #:nodoc: name.to_s.split(/\W+/).map { |s| s[0..0].upcase + s[1..-1] }.join end |
.make_constantizeable(string) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/speccify.rb', line 123 def self.make_constantizeable(string) return string unless string.class.to_s == "String" string = string.gsub(/[^\w\s]/,"").gsub(/^[\d\s]*/,"") raise ArgumentError.new( "Invalid argument. Must not be empty after removing '\W'-class chars." ) if string.gsub(/\s/,"").empty? string end |
.message(expected, actual, op, location) ⇒ Object
:nodoc:
132 133 134 135 136 137 138 139 |
# File 'lib/speccify.rb', line 132 def self.(expected, actual, op, location) # :nodoc: """ Expected: #{expected.to_s} got: #{actual.to_s} comparison with: #{op.to_s} location: (#{location}) """ end |