Class: Imports::Export

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

Overview

Only def exports.something; end is evaluated in the context of Export. Hence these are not recommented for bigger things, but it works fine for a collection of few methods that don’t need a separate class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Export

Returns a new instance of Export.



109
110
111
# File 'lib/import.rb', line 109

def initialize(path)
  @__FILE__, @__DATA__ = path, ::Hash.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Register variables and constants.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/import.rb', line 123

def method_missing(method, *args, &block)
  if method.to_s.match(/=$/) && args.length == 1 && block.nil?
    object_name = method.to_s[0..-2].to_sym
    object = args.first

    @__DATA__[object_name] = object

    if object.is_a?(Class) && object.name.nil?
      object.define_singleton_method(:name) { object_name.to_s }
      object.define_singleton_method(:inspect) { object_name.to_s }
    end
  elsif @__DATA__.has_key?(method)
    @__DATA__[method]
  else
    super(method, *args, &block)
  end
end

Instance Attribute Details

#__DATA__Object (readonly)

Returns the value of attribute __DATA__.



108
109
110
# File 'lib/import.rb', line 108

def __DATA__
  @__DATA__
end

#__FILE__Object (readonly)

Returns the value of attribute __FILE__.



108
109
110
# File 'lib/import.rb', line 108

def __FILE__
  @__FILE__
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/import.rb', line 141

def respond_to_missing?(method, include_private = false)
  (method.to_s.match(/=$/) && args.length == 1 && block.nil?) || @__DATA__.has_key?(method)
end

#singleton_method_added(method) ⇒ Object

Register methods.



114
115
116
117
118
119
120
# File 'lib/import.rb', line 114

def singleton_method_added(method)
  @__DATA__[method] = self.method(method)

  @__DATA__[method].define_singleton_method(:inspect) do
    "#<#{self.class} ##{method}>"
  end
end