Sha256: 307645d0a932bf894847f50ab2954641382aa3cfc0b960370254bcdb70b1783a

Contents?: true

Size: 1.34 KB

Versions: 10

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Opera
  module Operation
    module Builder
      INSTRUCTIONS = %I[validate transaction benchmark step success operation operations].freeze

      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def instructions
          @instructions ||= []
        end

        INSTRUCTIONS.each do |instruction|
          define_method instruction do |method = nil, &blk|
            instructions.concat(InnerBuilder.new.send(instruction, method, &blk))
          end
        end
      end

      class InnerBuilder
        attr_reader :instructions

        def initialize(&block)
          @instructions = []
          instance_eval(&block) if block_given?
        end

        INSTRUCTIONS.each do |instruction|
          define_method instruction do |method = nil, &blk|
            instructions << if !blk.nil?
                              {
                                kind: instruction,
                                instructions: InnerBuilder.new(&blk).instructions
                              }
                            else
                              {
                                kind: instruction,
                                method: method
                              }
                            end
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
opera-0.2.6 lib/opera/operation/builder.rb
opera-0.2.5 lib/opera/operation/builder.rb
opera-0.2.4 lib/opera/operation/builder.rb
opera-0.2.3 lib/opera/operation/builder.rb
opera-0.2.2 lib/opera/operation/builder.rb
opera-0.2.1 lib/opera/operation/builder.rb
opera-0.2.0 lib/opera/operation/builder.rb
opera-0.1.2 lib/opera/operation/builder.rb
opera-0.1.1 lib/opera/operation/builder.rb
opera-0.1.0 lib/opera/operation/builder.rb