lib/troles/common/config.rb in troles-0.5.2 vs lib/troles/common/config.rb in troles-0.6.0

- old
+ new

@@ -1,131 +1,98 @@ module Troles::Common class Config - autoload :ValidRoles, 'troles/common/config/valid_roles' - autoload :StaticRoles, 'troles/common/config/static_roles' - autoload :Schema, 'troles/common/config/schema' - autoload :SchemaHelpers, 'troles/common/config/schema_helpers' + autoload_modules :ValidRoles, :StaticRoles, :Schema, :SchemaHelpers, :ClassMethods def self.sub_modules [:valid_roles, :static_roles, :schema] end - + + # include sub-modules as needed sub_modules.each do |name| send :include, "Troles::Common::Config::#{name.to_s.camelize}".constantize end - attr_accessor :subject_class, :strategy, :log_on, :generic, :auto_relations + attr_accessor :subject_class, :strategy, :log_on, :generic attr_writer :orm - + + # configure Config object with subject class and various options def initialize subject_class, options = {} + raise ArgumentError, "The first argument must be the Class which is the subject of the behavior" unless subject_class.is_a?(Class) @subject_class = subject_class - # set instance var for each pair in options + apply_options! options end - class << self - attr_reader :default_orm, :auto_load - attr_accessor :log_on + extend ClassMethods - def log_on? - log_on || false - end - - def default_orm= orm - @default_orm ||= orm - end - - def auto_load= mode - raise "Autoload must be set to true or false" if ![true, false].include? mode - @auto_load = mode - end - - def auto_load? - @auto_load + # Call setter for each key/value pair + def apply_options! options = {} + options.each_pair do |key, value| + send("#{key}=", value) if self.respond_to?(:"#{key}=") end + end - def auto_config - auto_config_setings - end - - def auto_config? name - auto_config_setings[name] - end - - protected - - - def auto_config_setings - @auto_config_setings ||= auto_config_defaults - end - - # default auto_config settings - def auto_config_defaults - {:models => true, :relations => true, :fields => true} - end + # Configure subject with behavior + # First apply any remaining options needed + # Then configure models if configured to do do + def configure! options = {} + apply_options! options + configure_models if auto_config?(:models) end + # is logging on? def log_on? log_on || Troles::Config.log_on end + # get the auto configuration settings hash def auto_config - auto_config_setings + @auto_config ||= {} end - + + # is a certain type of auto configuration enabled? def auto_config? name - return auto_config_setings[name] if !auto_config_setings[name].nil? + return auto_config[name] if !auto_config[name].nil? Troles::Config.auto_config?(name) end - def apply_options! options = {} - options.each_pair do |key, value| - send("#{key}=", value) if self.respond_to?(:"#{key}") - end - end - - def configure! options = {} - apply_options! options - configure_models if auto_config?(:models) - end - - # protected - - def auto_config_setings - @auto_config_setings ||= {} - end - - def role_field - @role_field ||= begin - default_role_field + # Get the main field name that is used for the behavior added, fx :troles for roles behavior + def main_field + @main_field ||= begin + default_main_field end end + alias_method :role_field, :main_field - def role_field= field_name + # Set the main field of the behavior + def main_field= field_name name = field_name.to_s.alpha_numeric.to_sym - raise ArgumentException, "Not a valid role field name: #{field_name}" if !valid_field_name?(name) - @role_field ||= name + raise ArgumentException, "Not a valid field name: #{field_name}" if !valid_field_name?(name) + @main_field ||= name end + alias_method :role_field=, :main_field= - def default_role_field - singularity == :many ? :troles : :trole + # get the default name of the main field + # for roles, it depends on the singularity (one or many) of the strategy + # see (#singularity) + def default_main_field + @default_main_field ||= (singularity == :many) ? :troles : :trole end + alias_method :default_role_field, :default_main_field + # get the orm name def orm @orm || self.class.default_orm end + # get the singularity (one or many) of the strategy def singularity @singularity ||= (strategy =~ /_many$/) ? :many : :one end - # def singularity= value - # raise ArgumentError, "Must be :many or :one" if ![:one, :many].include?(value) - # @singularity ||= value - # end - + # is it a generic strategy/orm ? def generic? - return true if orm.nil? + return true if orm.nil? || orm == :generic @generic.nil? ? false : @generic end end end \ No newline at end of file