Sha256: ce4322e84de50d74299f9d91f8366b3426be98ae6ecb18a1245adb8fa5be177f

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module Opera
  module Operation
    module Builder
      INSTRUCTIONS = %I[validate transaction benchmark step success finish_if 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

14 entries across 14 versions & 1 rubygems

Version Path
opera-0.3.1 lib/opera/operation/builder.rb
opera-0.3.0 lib/opera/operation/builder.rb
opera-0.2.18 lib/opera/operation/builder.rb
opera-0.2.17 lib/opera/operation/builder.rb
opera-0.2.16 lib/opera/operation/builder.rb
opera-0.2.15 lib/opera/operation/builder.rb
opera-0.2.14 lib/opera/operation/builder.rb
opera-0.2.13 lib/opera/operation/builder.rb
opera-0.2.12 lib/opera/operation/builder.rb
opera-0.2.11 lib/opera/operation/builder.rb
opera-0.2.10 lib/opera/operation/builder.rb
opera-0.2.9 lib/opera/operation/builder.rb
opera-0.2.8 lib/opera/operation/builder.rb
opera-0.2.7 lib/opera/operation/builder.rb