class Object

Public Class Methods

new(params) click to toggle source
# File lib/attr_init.rb, line 23
def initialize(params)
  fattr_init(params)
end

Public Instance Methods

accessor_struct(*attrs) click to toggle source
# File lib/attr_init.rb, line 61
def accessor_struct(*attrs)
  fattrs *attrs
  fattr_init *attrs
  fattr_hash *attrs
end
attr_hash(*attrs) click to toggle source
# File lib/attr_init.rb, line 47
def attr_hash(*attrs)
  define_method(:to_h) do
    AttrInit.get_attrs(self.class).each_with_object({}) do |attr, hash|
      hash[attr] = public_send(attr)
    end
  end
end
attr_init(*attrs) click to toggle source
# File lib/attr_init.rb, line 5
def attr_init(*attrs)
  AttrInit.add_new_attrs(self, attrs)

  define_method(:attr_init) do |params|
    AttrInit.get_attrs(self.class).each do |a|
      instance_variable_set "@#{a}", params[a]
    end
  end

  protected :attr_init

  define_method(:initialize) do |params|
    attr_init(params)
  end
end
fattr_hash(*attrs) click to toggle source
# File lib/attr_init.rb, line 37
def fattr_hash(*attrs)
  class_eval do
    def to_hash
      self.class.fattrs.each_with_object({}) do |key, hash|
        hash[key] = send key
      end
    end
  end
end
fattr_init(*attrs) click to toggle source
# File lib/attr_init.rb, line 21
def fattr_init(*attrs)
  class_eval do
    def initialize(params)
      fattr_init(params)
    end

    protected

    def fattr_init(params)
      params.each do |key, value|
        send key, value
      end
    end
  end
end
reader_struct(*attrs) click to toggle source
# File lib/attr_init.rb, line 55
def reader_struct(*attrs)
  attr_reader *attrs
  attr_init *attrs
  attr_hash *attrs
end
to_hash() click to toggle source
# File lib/attr_init.rb, line 39
def to_hash
  self.class.fattrs.each_with_object({}) do |key, hash|
    hash[key] = send key
  end
end