Module: Ghaki::App::Mixable::ClassMethods

Defined in:
lib/ghaki/app/mixable.rb

Overview

Examples

require 'ghaki/app/plugin'
require 'ghaki/app/mixer'

class Duck
  attr_accessor :quack
end

class DuckApp < Ghaki::App::Plugin
  app_plugin_accessor Duck, :duck
end

class DuckMixin
   include Ghaki::App::Mixable
   app_mixin_accessor DuckApp, :duck
end

class UsingDuck
  include DuckMixin
  def quacker
    duck.quack = true
  end
end

Instance Method Summary collapse

Instance Method Details

#app_mixin_accessor(klass, source, target = source) ⇒ Object

Generate attribute reader and writer.



65
66
67
68
# File 'lib/ghaki/app/mixable.rb', line 65

def app_mixin_accessor klass, source, target=source
  app_mixin_reader klass, source, target
  app_mixin_writer target
end

#app_mixin_reader(klass, source, target = source) ⇒ Object

Generate mixin attribute reader. Uses the singleton copy unless overwritten by the writer.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ghaki/app/mixable.rb', line 35

def app_mixin_reader klass, source, target=source
  if klass.respond_to?(:instance)
    class_eval <<-"END"
      def #{target}
        @#{target} ||= #{klass}.instance.#{source}
      end
    END
    func = :instance
  elsif klass.respond_to?(:new)
    class_eval <<-"END"
      def #{target}
        @#{target} ||= #{klass}.new
      end
    END
  else
    raise ArgumentError, "Unknown Constructor: #{klass}"
  end
end

#app_mixin_writer(target) ⇒ Object

Generate mixin attribute writer. Allows the local copy of the singleton value to be overriden.



57
58
59
60
61
# File 'lib/ghaki/app/mixable.rb', line 57

def app_mixin_writer target
  class_exec do
    attr_writer target
  end
end