Module: Structure::ClassMethods
- Defined in:
- lib/structure/double.rb,
lib/structure/class_methods.rb
Instance Attribute Summary collapse
-
#attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names.
5 6 7 |
# File 'lib/structure/class_methods.rb', line 5 def attribute_names @attribute_names end |
Class Method Details
.extended(base) ⇒ Object
7 8 9 |
# File 'lib/structure/class_methods.rb', line 7 def self.extended(base) base.instance_variable_set :@attribute_names, [] end |
Instance Method Details
#__overwrite_initialize__ ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/structure/class_methods.rb', line 43 def __overwrite_initialize__ class_eval do unless method_defined?(:__custom_initialize__) define_method :__custom_initialize__ do |*args| @__mutex__ = ::Thread::Mutex.new __original_initialize__(*args) end end return if instance_method(:initialize) == instance_method(:__custom_initialize__) alias_method :__original_initialize__, :initialize alias_method :initialize, :__custom_initialize__ end end |
#attribute(name, &block) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/structure/class_methods.rb', line 11 def attribute(name, &block) name = name.to_s if name.chomp!('?') module_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}? #{name} end CODE end module_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} @__mutex__.synchronize { return @#{name} if defined?(@#{name}) @#{name} = __#{name}__ @#{name}.freeze unless @#{name}.is_a?(Structure) @#{name} } end CODE define_method "__#{name}__", block private "__#{name}__" @attribute_names << name name.to_sym end |
#double ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/structure/double.rb', line 5 def double klass = Class.new(self) ( private_instance_methods(false) + protected_instance_methods(false) - [:initialize] ).each do |name| klass.send :undef_method, name end klass.module_eval do def initialize(data = {}) data.each do |key, value| unless value.is_a?(Structure) || (defined?(::RSpec::Mocks::Double) && value.is_a?(::RSpec::Mocks::Double)) value.freeze end instance_variable_set :"@#{key}", value end end attribute_names.each do |name| module_eval <<-CODE, __FILE__, __LINE__ + 1 def __#{name}__ @#{name} end CODE private "__#{name}__" end module_eval(&Proc.new) if block_given? end class << klass undef_method :double end klass end |