Sha256: 84e5bad49f62468a3d0c4179062c81c8af33fc3e6ec1ec18f398a1b206a4fa8d

Contents?: true

Size: 887 Bytes

Versions: 1

Compression:

Stored size: 887 Bytes

Contents

module ClassyAdornments

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

  def adorn(name,opts = {})
    adorn_with name       if opts.empty?
    adorn_with_array name if opts[:array]
  end

  private

  def adorn_with(name)
    define_method(name) do |*args|
      var = '@' + name.to_s
      if args.first
        return instance_variable_set(var,args.first)
      else
        return instance_variable_get(var)
      end
    end
    alias_method "#{name}=", name
  end

  def adorn_with_array(name)
    define_method(name) do |*args|
      var = '@' + name.to_s
      instance_variable_set(var,Array.new) if instance_variable_get(var).nil? 
      return case args.first
      when nil
        instance_variable_get(var)
      when Array
        instance_variable_get(var).concat args.first
      else
        instance_variable_get(var).concat args
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
classy-adornments-0.2.0 lib/classy_adornments.rb