Sha256: 613a3386aefda587873a2b2a00678e3ec31d82e4051573a07df9fd1f2e1d4f94

Contents?: true

Size: 1.75 KB

Versions: 2

Compression:

Stored size: 1.75 KB

Contents

# encoding: utf-8
require 'active_support'

# attr_accessor + initialize
module AttributesInitializable
  extend ActiveSupport::Concern

  module ClassMethods
    # generate attr_accessor + initialize
    # params
    # - symbols : attribute names
    def attr_accessor_init(*symbols)
      generate_attr_accessor(symbols)
      generate_initializer(symbols)
    end

    # generate attr_reader + initialize
    # params
    # - symbols : attribute names
    def attr_reader_init(*symbols)
      generate_attr_reader(symbols)
      generate_initializer(symbols)
    end

    # generate attr_writer + initialize
    # params
    # - symbols : attribute names
    def attr_writer_init(*symbols)
      generate_attr_writer(symbols)
      generate_initializer(symbols)
    end

    private

    def generate_attr_accessor(symbols)
      generate_attr symbols, :accessor
    end

    def generate_attr_reader(symbols)
      generate_attr symbols, :reader
    end

    def generate_attr_writer(symbols)
      generate_attr symbols, :writer
    end

    def generate_attr(symbols, type)
      results = symbols.reduce([]) do |results, sym|
        fail TypeError, "invalid type #{sym.class}. you have to use Symbol" unless sym.class == Symbol
        results << ":#{sym.to_s}"
      end
      class_eval "attr_#{type.to_s} #{results.join(',')}"
    end

    def generate_initializer(symbols)
      instance_eval do
        define_method :initialize do |values = nil, &block|
          return block.call self if block
          # symbols.each { |symbol|send("#{symbol.to_s}=", values[symbol]) }
          symbols.each { |symbol|self.instance_variable_set("@#{symbol.to_s}", values[symbol]) }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.13 lib/attributes_initializable.rb
tbpgr_utils-0.0.12 lib/attributes_initializable.rb