Module: Speccify::Functions

Defined in:
lib/speccify.rb

Class Method Summary collapse

Class Method Details

.build_matcher(matcher_name, args, &block) ⇒ Object



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
88
89
90
# File 'lib/speccify.rb', line 62

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
      @matcher_name
    end
    
    attr_accessor :positive_msg, :negative_msg, :msgs, :loc
    def initialize match_block
      @match_block = match_block
    end
    
    def method_missing id, *args, &block
      require 'ostruct'
      (self.msgs ||= []) << OpenStruct.new( "name" => id, "args" => args, "block" => block ) 
      self
    end

    def matches? given
      @positive_msg ||= Speccify::Functions::message("#{given} should #{self.class.matcher_name}", "no match", self.class.matcher_name , self.loc || "") 
      @negative_msg ||= Speccify::Functions::message("#{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



58
59
60
# File 'lib/speccify.rb', line 58

def self.determine_class_name(name)
  name.to_s.split(/\W+/).map { |s| s[0..0].upcase + s[1..-1] }.join 
end

.message(expected, actual, op, location) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/speccify.rb', line 92

def self.message(expected, actual, op, location) 
        """
          Expected: #{expected.to_s}
               got: #{actual.to_s} 
   comparison with: #{op.to_s}
          location: (#{location})
        """
end