lib/cockpit/core/include.rb in cockpit-0.1.1 vs lib/cockpit/core/include.rb in cockpit-0.2.0

- old
+ new

@@ -1,90 +1,45 @@ module Cockpit + def self.included(base) - base.send(:include, ObjectInclude) - if defined?(::ActiveRecord::Base) && base.ancestors.include?(::ActiveRecord::Base) - base.send(:include, ActiveRecordInclude) - end + base.extend(ClassMethods) + base.send(:include, InstanceMethods) end - module ActiveRecordInclude - def self.included(base) - base.class_eval do - def self.cockpit(*args, &block) - if block_given? || @cockpit.nil? - @cockpit = Cockpit::Settings.new( - :name => self.name.underscore.gsub(/[^a-z0-9]/, "_").squeeze("_"), - :store => :active_record, - &block - ) - - @cockpit.keys.each do |key| - next if key =~ /\./ - - define_method key do - send(:cockpit)[key] - end - - define_method "#{key}?" do - !send(key).blank? - end - end - - else - @cockpit + 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 - end - - def cockpit - unless @cockpit - @cockpit = Cockpit::Settings.new( - :name => self.class.cockpit.name, - :store => :active_record, - :record => self - ) - end - @cockpit + define_method "#{key}?" do + send(:cockpit).has_key?(key) + end end + end + + @cockpit + end + + unless respond_to?(:key) + def key(*args) - def get(key) - cockpit[key] - end unless respond_to?(:get) - - def set(*args) - if args.last.is_a?(Hash) - cockpit.set(args.last) - else - cockpit[args.first] = args.last - end - end unless respond_to?(:set) end end end - module ObjectInclude - def self.included(base) - base.class_eval do - def self.cockpit(*args, &block) - if block_given? - @cockpit = Cockpit::Settings.new( - :name => self.name.underscore.gsub(/[^a-z0-9]/, "_").squeeze("_"), - :scope => "default", - :store => args.first || "memory", - &block - ) - else - @cockpit - end - end - - def cockpit - unless @cockpit - @cockpit = self.class.cockpit.dup - end - - @cockpit - end + module InstanceMethods + def cockpit(key = nil) + @cockpit ||= Cockpit::Settings.new(:record => self) + if key + @cockpit.definition(key) + else + @cockpit end end end end