Class: Troles::Common::Config

Inherits:
Object
  • Object
show all
Extended by:
ClassMethods
Defined in:
lib/troles/common/config.rb,
lib/troles/common/config/schema.rb,
lib/troles/common/config/valid_roles.rb,
lib/troles/common/config/static_roles.rb,
lib/troles/common/config/class_methods.rb,
lib/troles/common/config/schema/helpers.rb

Defined Under Namespace

Modules: ClassMethods, Schema, StaticRoles, ValidRoles

Instance Attribute Summary collapse

Attributes included from ClassMethods

#auto_load, #default_orm

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

auto_load?

Constructor Details

#initialize(subject_class, options = {}) ⇒ Config

configure Config object with subject class and various options

Raises:

  • (ArgumentError)


18
19
20
21
22
23
# File 'lib/troles/common/config.rb', line 18

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

  apply_options! options
end

Instance Attribute Details

#genericObject

Returns the value of attribute generic.



14
15
16
# File 'lib/troles/common/config.rb', line 14

def generic
  @generic
end

#log_onObject

Returns the value of attribute log_on.



14
15
16
# File 'lib/troles/common/config.rb', line 14

def log_on
  @log_on
end

#ormObject

get the orm name



83
84
85
# File 'lib/troles/common/config.rb', line 83

def orm
  @orm || self.class.default_orm
end

#strategyObject

Returns the value of attribute strategy.



14
15
16
# File 'lib/troles/common/config.rb', line 14

def strategy
  @strategy
end

#subject_classObject

Returns the value of attribute subject_class.



14
15
16
# File 'lib/troles/common/config.rb', line 14

def subject_class
  @subject_class
end

Class Method Details

.sub_modulesObject



5
6
7
# File 'lib/troles/common/config.rb', line 5

def self.sub_modules
  [:valid_roles, :static_roles, :schema]
end

Instance Method Details

#apply_options!(options = {}) ⇒ Object

Call setter for each key/value pair



28
29
30
31
32
# File 'lib/troles/common/config.rb', line 28

def apply_options! options = {}
  options.each_pair do |key, value| 
    send("#{key}=", value) if self.respond_to?(:"#{key}=")
  end      
end

#auto_configObject

get the auto configuration settings hash



48
49
50
# File 'lib/troles/common/config.rb', line 48

def auto_config
  @auto_config ||= {}
end

#auto_config?(name) ⇒ Boolean

is a certain type of auto configuration enabled?

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/troles/common/config.rb', line 53

def auto_config? name
  return auto_config[name] if !auto_config[name].nil?
  Troles::Config.auto_config?(name)
end

#configure!(options = {}) ⇒ Object

Configure subject with behavior First apply any remaining options needed Then configure models if configured to do do



37
38
39
40
# File 'lib/troles/common/config.rb', line 37

def configure! options = {}
  apply_options! options
  configure_models if auto_config?(:models)
end

#default_main_fieldObject Also known as: default_role_field

get the default name of the main field for roles, it depends on the singularity (one or many) of the strategy see (#singularity)



77
78
79
# File 'lib/troles/common/config.rb', line 77

def default_main_field
  @default_main_field ||= (singularity == :many) ? :troles : :trole
end

#generic?Boolean

is it a generic strategy/orm ?

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/troles/common/config.rb', line 93

def generic?
  return true if orm.nil? || orm == :generic
  @generic.nil? ? false : @generic
end

#log_on?Boolean

is logging on?

Returns:

  • (Boolean)


43
44
45
# File 'lib/troles/common/config.rb', line 43

def log_on?
  log_on || Troles::Config.log_on
end

#main_fieldObject Also known as: role_field

Get the main field name that is used for the behavior added, fx :troles for roles behavior



59
60
61
62
63
# File 'lib/troles/common/config.rb', line 59

def main_field
  @main_field ||= begin
    default_main_field
  end
end

#main_field=(field_name) ⇒ Object Also known as: role_field=

Set the main field of the behavior

Raises:

  • (ArgumentException)


67
68
69
70
71
# File 'lib/troles/common/config.rb', line 67

def main_field= field_name
  name = field_name.to_s.alpha_numeric.to_sym
  raise ArgumentException, "Not a valid field name: #{field_name}"  if !valid_field_name?(name)
  @main_field ||= name
end

#singularityObject

get the singularity (one or many) of the strategy



88
89
90
# File 'lib/troles/common/config.rb', line 88

def singularity
  @singularity ||= (strategy =~ /_many$/) ? :many : :one
end