Sha256: 8c3735250be246be71650eab34f51f6571124148df62a08b5ed1d7802aeabf91

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module Cockpit
  
  def self.included(base)
    base.extend(ClassMethods)
    base.send(:include, InstanceMethods)
  end
  
  module ClassMethods
    def cockpit(options = {}, &block)
      if block_given?
        options = {:store => options.to_sym} unless options.is_a?(Hash)
        @cockpit = Cockpit::Settings.define!(options.merge(:for => self), &block)
        
        @cockpit.roots.map(&:key).flatten.each do |key|
          define_method key do
            send(:cockpit).send(key)
          end
          
          define_method "#{key}=" do |value|
            send(:cockpit).send("#{key}=", value)
          end
          
          define_method "#{key}?" do
            send(:cockpit).present?(key)
          end
          
          define_method "setting" do |key, scope|
            Cockpit::Settings.for(scope ? scope.to_s.camelize : NilClass)[key]
          end
        end
      end
      
      @cockpit
    end
    
    unless respond_to?(:key)
      def key(*args)
        
      end
    end
  end
  
  module InstanceMethods
    def cockpit(key = nil)
      @cockpit ||= Cockpit::Settings.new(:record => self)
      if key
        @cockpit.definition(key)
      else
        @cockpit
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cockpit-0.2.4.4 lib/cockpit/core/include.rb
cockpit-0.2.4.3 lib/cockpit/core/include.rb