Module: Structure::ClassMethods

Defined in:
lib/structure.rb

Overview

The class interface

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



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

def attribute_names
  @attribute_names
end

Class Method Details

.extended(base) ⇒ Object



65
66
67
68
# File 'lib/structure.rb', line 65

def extended(base)
  base.instance_variable_set :@attribute_names, []
  base.send :override_initialize
end

Instance Method Details

#attribute(name, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/structure.rb', line 73

def attribute(name, &block)
  name = name.to_s

  if name.end_with?('?')
    name = name.chop
    module_eval <<-CODE, __FILE__, __LINE__ + 1
      def #{name}?
        #{name}
      end
    CODE
  end

  module_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}
      with_mutex do
        break if defined?(@#{name})

        @#{name} = unmemoized_#{name}
      end

      @#{name}
    end
  CODE
  private define_method "unmemoized_#{name}", block
  @attribute_names << name

  name.to_sym
end