class Sequent::Generator::Event
Attributes
attrs[R]
event[R]
Public Class Methods
new(name, event, attrs)
click to toggle source
# File lib/sequent/generator/event.rb, line 14 def initialize(name, event, attrs) @name = name @event = event @attrs = attrs.map { |a| a.split(':') } end
Public Instance Methods
execute()
click to toggle source
# File lib/sequent/generator/event.rb, line 24 def execute ensure_existing_aggregate! add_event_to_aggregate end
name()
click to toggle source
# File lib/sequent/generator/event.rb, line 20 def name @name ||= File.basename(path) end
Private Instance Methods
add_event_to_aggregate()
click to toggle source
# File lib/sequent/generator/event.rb, line 60 def add_event_to_aggregate append_event append_event_to_domain end
append_event()
click to toggle source
# File lib/sequent/generator/event.rb, line 31 def append_event File.open("#{path_to_dir}/events.rb", 'a') { |f| f << event_template.result(binding) } end
append_event_to_domain()
click to toggle source
# File lib/sequent/generator/event.rb, line 35 def append_event_to_domain ast = Parser::CurrentRuby.parse(File.read("#{path_to_dir}/#{name_underscored}.rb")) target_cursor_position = find_target_cursor_position(ast) File.open("#{path_to_dir}/#{name_underscored}.rb", 'r+') do |f| f.seek(target_cursor_position, IO::SEEK_SET) lines_to_be_overwritten = f.read f.seek(target_cursor_position, IO::SEEK_SET) f << event_handler_template.result(binding).gsub(/^.+(\s)$/) { |x| x.gsub!(Regexp.last_match(1), '') } f << lines_to_be_overwritten end end
ensure_existing_aggregate!()
click to toggle source
# File lib/sequent/generator/event.rb, line 77 def ensure_existing_aggregate! fail NoAggregateFound if !File.directory?(path_to_dir) || !File.exist?("#{path_to_dir}/#{name_underscored}.rb") end
event_handler_template()
click to toggle source
# File lib/sequent/generator/event.rb, line 90 def event_handler_template ERB.new <<~EOF \n on <%= event %> do |event| end EOF end
event_template()
click to toggle source
# File lib/sequent/generator/event.rb, line 81 def event_template ERB.new <<~EOF class <%= event %> < Sequent::Event <% attrs.each do |name, type| %>attrs <%= name.downcase %>: <%= type.downcase.capitalize %><% end %> end EOF end
find_target_cursor_position(ast)
click to toggle source
# File lib/sequent/generator/event.rb, line 48 def find_target_cursor_position(ast) return unless ast.children.any? return if ast.children.any? { |c| c.class.to_s != 'Parser::AST::Node' } if (child = ast.children.find { |c| c.type.to_s == 'block' }) return child.loc.expression.end_pos end ast.children.reverse.map do |c| find_target_cursor_position(c) end.flatten.compact.max end
name_underscored()
click to toggle source
# File lib/sequent/generator/event.rb, line 69 def name_underscored @name_underscored ||= name.underscore end
path()
click to toggle source
# File lib/sequent/generator/event.rb, line 65 def path @path ||= File.expand_path('lib') end
path_to_dir()
click to toggle source
# File lib/sequent/generator/event.rb, line 73 def path_to_dir @path_to_dir ||= "#{path}/#{name_underscored}" end