Class: Cohabit::Strategy

Inherits:
Object
  • Object
show all
Includes:
Configuration::Settings
Defined in:
lib/cohabit/strategy.rb

Constant Summary

Constants included from Configuration::Settings

Configuration::Settings::CUSTOM_HANDLERS, Configuration::Settings::DEFAULT_SETTINGS

Instance Attribute Summary collapse

Attributes included from Configuration::Settings

#settings

Instance Method Summary collapse

Methods included from Configuration::Settings

#generate_settings_hash!, included, #initialize_with_settings, #merge_settings!, #set, #set_globals

Constructor Details

#initialize(*args, &block) ⇒ Strategy

Returns a new instance of Strategy.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
# File 'lib/cohabit/strategy.rb', line 6

def initialize(*args, &block)
  raise ArgumentError, "you must supply a name" if args.empty?
  @strategies = []
  @name = args.shift.to_sym
  @settings = args.last.is_a?(Hash) ? args.last : {}
  instance_eval(&block) unless block.nil?
end

Instance Attribute Details

#model_codeObject (readonly)

Returns the value of attribute model_code.



16
17
18
# File 'lib/cohabit/strategy.rb', line 16

def model_code
  @model_code
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/cohabit/strategy.rb', line 16

def name
  @name
end

#strategiesObject (readonly)

Returns the value of attribute strategies.



16
17
18
# File 'lib/cohabit/strategy.rb', line 16

def strategies
  @strategies
end

Instance Method Details

#include_strategy(name, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
# File 'lib/cohabit/strategy.rb', line 23

def include_strategy(name, options = {})
  name = name.to_sym
  raise ArgumentError if name.nil?
  if @strategies.include?(name)
    raise Argumenterror, "can't nest the same strategy twice
    or use two model_eval blocks in the same strategy"
  end
  @strategies << name
end

#model_eval(&block) ⇒ Object



18
19
20
21
# File 'lib/cohabit/strategy.rb', line 18

def model_eval(&block)
  @model_code = block
  @strategies << @name
end