Class: Cucumber::Core::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/core/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.event_idSymbol

Returns the underscored name of the class to be used as the key in an event registry.

Returns:

  • (Symbol)

    the underscored name of the class to be used as the key in an event registry



43
44
45
# File 'lib/cucumber/core/event.rb', line 43

def event_id
  underscore(name.split('::').last).to_sym
end

.new(*events) ⇒ Object

Macro to generate new subclasses of Cucumber::Core::Event with attribute readers.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cucumber/core/event.rb', line 7

def self.new(*events)
  # Use normal constructor for subclasses of Event
  return super if ancestors.index(Event).positive?

  Class.new(Event) do
    # NB: We need to use metaprogramming here instead of direct variable obtainment
    # because JRuby does not guarantee the order in which variables are defined is equivalent
    # to the order in which they are obtainable
    #
    # See https://github.com/jruby/jruby/issues/7988 for more info
    attr_reader(*events)

    define_method(:initialize) do |*attributes|
      events.zip(attributes) do |name, value|
        instance_variable_set(:"@#{name}", value)
      end
    end
  end
end

Instance Method Details

#attributesObject



33
34
35
# File 'lib/cucumber/core/event.rb', line 33

def attributes
  instance_variables.map { |var| instance_variable_get(var) }
end

#event_idObject



37
38
39
# File 'lib/cucumber/core/event.rb', line 37

def event_id
  self.class.event_id
end

#to_hObject



27
28
29
30
31
# File 'lib/cucumber/core/event.rb', line 27

def to_h
  instance_variables.to_h do |variable_name|
    [variable_name[1..].to_sym, instance_variable_get(variable_name)]
  end
end