Sha256: 4a50294cdd8105cf8917695266924404364be92dde4cbac1a65c6404ea18a00c

Contents?: true

Size: 1.57 KB

Versions: 25

Compression:

Stored size: 1.57 KB

Contents

# encoding: ascii-8bit

# Copyright 2014 Ball Aerospace & Technologies Corp.
# All Rights Reserved.
#
# This program is free software; you can modify and/or redistribute it
# under the terms of the GNU General Public License
# as published by the Free Software Foundation; version 3 with
# attribution addendums as found in the LICENSE.txt

# COSMOS specific additions to the Ruby Class class
class Class
  # Creates instance variables in the class which have corresponding class
  # method accessors. NOTE: You must define self.instance for this to work.
  #
  # For example:
  #   class MyClass
  #     instance_attr_reader :test
  #     @@instance = nil
  #     def self.instance
  #       @@instance ||= self.new
  #       return @@instance
  #     end
  #     def initialize
  #       @test = "Test"
  #       @@instance = self
  #     end
  #
  # Will allow the following:
  #   my = MyClass.new
  #   my.test # returns "Test"
  #   MyClass.test # returns "Test"
  #
  # @param args [Array<Symbol>] Array of symbols which should be turned into
  #   instance variables with class method accessors
  def instance_attr_reader(*args)
    args.each do |arg|
      self.class_eval("def #{arg};@#{arg};end")
      self.instance_eval("def #{arg};self.instance.#{arg};end")
    end
  end

  def instance_attr_accessor(*args)
    args.each do |arg|
      self.class_eval("def #{arg};@#{arg};end")
      self.instance_eval("def #{arg};self.instance.#{arg};end")
      self.class_eval("def #{arg}=(arg);@#{arg} = arg;end")
      self.instance_eval("def #{arg}=(arg);self.instance.#{arg} = arg;end")
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
cosmos-3.8.3 lib/cosmos/core_ext/class.rb
cosmos-3.8.2 lib/cosmos/core_ext/class.rb
cosmos-3.8.1 lib/cosmos/core_ext/class.rb
cosmos-3.8.0 lib/cosmos/core_ext/class.rb
cosmos-3.7.1 lib/cosmos/core_ext/class.rb
cosmos-3.7.0 lib/cosmos/core_ext/class.rb
cosmos-3.6.3 lib/cosmos/core_ext/class.rb
cosmos-3.6.2 lib/cosmos/core_ext/class.rb
cosmos-3.6.1 lib/cosmos/core_ext/class.rb
cosmos-3.6.0 lib/cosmos/core_ext/class.rb
cosmos-3.5.3 lib/cosmos/core_ext/class.rb
cosmos-3.5.2 lib/cosmos/core_ext/class.rb
cosmos-3.5.0 lib/cosmos/core_ext/class.rb
cosmos-3.4.2 lib/cosmos/core_ext/class.rb
cosmos-3.4.1 lib/cosmos/core_ext/class.rb
cosmos-3.4.0 lib/cosmos/core_ext/class.rb
cosmos-3.3.3 lib/cosmos/core_ext/class.rb
cosmos-3.3.2 lib/cosmos/core_ext/class.rb
cosmos-3.3.1 lib/cosmos/core_ext/class.rb
cosmos-3.3.0 lib/cosmos/core_ext/class.rb