Sha256: eabce2a41c3e05faaceeb00906b7fa537928bd54c07f3cd6172f6cb03badb803

Contents?: true

Size: 1.23 KB

Versions: 6

Compression:

Stored size: 1.23 KB

Contents

require 'set'

# A Mock can fake an object.
#
# This is useful when frontend developers want to fill a page with
# mock data. Mocks can pre-define interface for real data.
# 
# == Usage
#
# app/mocks/user_mock.rb:
#
#   class UserMock < Neo::Rails::Mock
#     def age
#       mock.tagged?(:old) ? 78 : 23
#     end
#
#     def name
#       "Uncle bob"
#     end
#
#     def logged_in?
#       true
#     end
#
#     def sexy?
#       mock.tagged?(:sexy)
#     end
#   end
#
# Further...
#
#   old_man = UserMock.new(:old)
#   old_man.age # => 78
#   old_man.name # => "Uncle Bob"
#
#   old_sexbomb = UserMock.new(:old, :sexy)
#   old_sexbomb.age # => 78
#   old_sexbomb.sexy? # => true
#
module Neo
  module Rails
    class Mock
      attr_reader :mock

      # Initializes a Mock with optional tag list.
      def initialize(*args)
        @mock = MockConfig.new(args)
      end

      class MockConfig
        def initialize(args)
          @tags = args
        end

        # Returns a human readable tag list.
        def description
          @tags.to_a.map { |tag| tag.to_s.capitalize }.join(", ")
        end

        # Checks if this mock is tagged with +tag+.
        def tagged?(tag)
          @tags.include?(tag)
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
neo-rails-0.0.6 lib/neo/rails/mock.rb
neo-rails-0.0.5 lib/neo/rails/mock.rb
neo-rails-0.0.4 lib/neo/rails/mock.rb
neo-rails-0.0.3 lib/neo/rails/mock.rb
neo-rails-0.0.2 lib/neo/rails/mock.rb
neo-rails-0.0.1 lib/neo/rails/mock.rb