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


83
84
85
# File 'lib/structure.rb', line 83

def attribute_names
  @attribute_names
end

Class Method Details

.extended(base) ⇒ Object


86
87
88
89
# File 'lib/structure.rb', line 86

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

Instance Method Details

#attribute(name, &block) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/structure.rb', line 94

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