Sha256: 6ab5ff32389e0e0a7d4270698dd4ed44ccf28e50334b2c73b4f070fe193d16b4

Contents?: true

Size: 889 Bytes

Versions: 1

Compression:

Stored size: 889 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) << args.first
      end
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

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