Sha256: aa15f6844bd061ab0ef2b3dee8a1a9003fa2cef150c0eb647a8fd6c9e8a74b53

Contents?: true

Size: 908 Bytes

Versions: 2

Compression:

Stored size: 908 Bytes

Contents

require 'active_support'
require 'active_support/all'

class ConcernBuilder
  require 'concern_builder/version'
  require 'concern_builder/options_parser'

  include OptionsParser

  attr_reader :attr_names, :methods_def

  def initialize(attr_names, instance, options = {})
    @attr_names = attr_names
    @instance = instance
    @options = options
    @methods_def = []
    init
  end

  def build
    methods_def.each do |method_def|
      @instance.module_eval(method_def, __FILE__, __LINE__ + 1)
    end
  end

  private

  def add_method(name, code = nil, &block)
    if code.is_a?(String)
      add_code_method(name, code)
    else
      add_block_method(name, block)
    end
  end

  def add_block_method(name, block)
    @instance.send(:define_method, name, block)
  end

  def add_code_method(name, code)
    @methods_def << <<-CODE
      def #{name}
        #{code}
      end
    CODE
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
concern_builder-0.0.3 lib/concern_builder.rb
concern_builder-0.0.2 lib/concern_builder.rb