Class: Class

Inherits:
Object
  • Object
show all
Defined in:
lib/shadow_puppet/core_ext.rb

Constant Summary collapse

EMPTY_INHERITABLE_ATTRIBUTES =

Prevent this constant from being created multiple times

{}.freeze

Instance Method Summary collapse

Instance Method Details

#class_inheritable_accessor(*syms) ⇒ Object



57
58
59
60
# File 'lib/shadow_puppet/core_ext.rb', line 57

def class_inheritable_accessor(*syms)
  class_inheritable_reader(*syms)
  class_inheritable_writer(*syms)
end

#class_inheritable_reader(*syms) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/shadow_puppet/core_ext.rb', line 25

def class_inheritable_reader(*syms)
  syms.each do |sym|
    next if sym.is_a?(Hash)
    class_eval <<-EOS
      def self.#{sym}                        # def self.before_add_for_comments
        read_inheritable_attribute(:#{sym})  #   read_inheritable_attribute(:before_add_for_comments)
      end                                    # end
                                             #
      def #{sym}                             # def before_add_for_comments
        self.class.#{sym}                    #   self.class.before_add_for_comments
      end                                    # end
    EOS
  end
end

#class_inheritable_writer(*syms) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/shadow_puppet/core_ext.rb', line 40

def class_inheritable_writer(*syms)
  options = syms.extract_options!
  syms.each do |sym|
    class_eval <<-EOS
      def self.#{sym}=(obj)                          # def self.color=(obj)
        write_inheritable_attribute(:#{sym}, obj)    #   write_inheritable_attribute(:color, obj)
      end                                            # end
                                                     #
      #{"                                            #
      def #{sym}=(obj)                               # def color=(obj)
        self.class.#{sym} = obj                      #   self.class.color = obj
      end                                            # end
      " unless options[:instance_writer] == false }  # # the writer above is generated unless options[:instance_writer] == false
    EOS
  end
end

#inheritable_attributesObject



62
63
64
# File 'lib/shadow_puppet/core_ext.rb', line 62

def inheritable_attributes
  @inheritable_attributes ||= EMPTY_INHERITABLE_ATTRIBUTES
end

#read_inheritable_attribute(key) ⇒ Object



73
74
75
# File 'lib/shadow_puppet/core_ext.rb', line 73

def read_inheritable_attribute(key)
  inheritable_attributes[key]
end

#reset_inheritable_attributesObject



77
78
79
# File 'lib/shadow_puppet/core_ext.rb', line 77

def reset_inheritable_attributes
  @inheritable_attributes = EMPTY_INHERITABLE_ATTRIBUTES
end

#write_inheritable_attribute(key, value) ⇒ Object



66
67
68
69
70
71
# File 'lib/shadow_puppet/core_ext.rb', line 66

def write_inheritable_attribute(key, value)
  if inheritable_attributes.equal?(EMPTY_INHERITABLE_ATTRIBUTES)
    @inheritable_attributes = {}
  end
  inheritable_attributes[key] = value
end