Class: Concern

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

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
File.read(File.join(File.dirname(__FILE__), '..', 'VERSION'))

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#concernedObject

Returns the value of attribute concerned.



4
5
6
# File 'lib/concern.rb', line 4

def concerned
  @concerned
end

Class Method Details

.add_accessor_for_concern(concerned, accessor, concern) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/concern.rb', line 26

def self.add_accessor_for_concern(concerned, accessor, concern)
  concerned.class_eval <<-EOF, __FILE__, __LINE__
    def #{accessor}
      return @#{accessor} if @#{accessor}
      @#{accessor} = #{concern}.new
      @#{accessor}.concerned = self
      @#{accessor}
    end
  EOF
end

.class_from_lib(lib) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/concern.rb', line 10

def self.class_from_lib(lib)
  lib = lib.to_s
  klass = Concern.classify(lib)
  begin
    klass = eval(klass)
  rescue
    require lib
    klass = eval(klass)
  end

  unless klass.instance_methods.map{|x|x.to_s}.include?('concerned=')
    raise "A concern must always extend Concern"
  end
  klass
end

.classify(lib) ⇒ Object



6
7
8
# File 'lib/concern.rb', line 6

def self.classify(lib)
  lib.split('/').map{|part| part.gsub(/(?:^|_)(.)/){ $1.upcase } }.join('::')
end