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.



72
73
74
# File 'lib/structure.rb', line 72

def attribute_names
  @attribute_names
end

Class Method Details

.extended(base) ⇒ Object



75
76
77
78
# File 'lib/structure.rb', line 75

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

Instance Method Details

#attribute(name, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/structure.rb', line 83

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

  if name.end_with?('?')
    name = name.chop
    module_eval "      def \#{name}?\n        \#{name}\n      end\n    CODE\n  end\n\n  module_eval <<-CODE, __FILE__, __LINE__ + 1\n    def \#{name}\n      with_mutex do\n        break if defined?(@\#{name})\n\n        @\#{name} = unmemoized_\#{name}\n      end\n\n      @\#{name}\n    end\n  CODE\n  private define_method \"unmemoized_\#{name}\", block\n  @attribute_names << name\n\n  name.to_sym\nend\n", __FILE__, __LINE__ + 1