Class: ActsAsActsAs::Requirements::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/acts_as_acts_as/requirements/collector.rb

Overview

A specialized object we pass into the block of ‘acts_as` so the DSL is nice and pretty. It basically just builds a hash of requirements matchers.

Defined Under Namespace

Modules: Context

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Collector

Returns a new instance of Collector.



6
7
8
9
10
11
12
13
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 6

def initialize(options={})
  @requirements = {}
  @options_collectors = []
  @options = options

  # run into nil problems with temp_model if require_columns is nil
  @requirements[:require_columns] = []
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 4

def options
  @options
end

#options_collectorsObject

Returns the value of attribute options_collectors.



4
5
6
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 4

def options_collectors
  @options_collectors
end

#requirementsObject

Returns the value of attribute requirements.



4
5
6
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 4

def requirements
  @requirements
end

Instance Method Details

#allObject

Convenience method for getting the whole tree of collectors that may have been spawned off via ‘with`



32
33
34
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 32

def all
  return [self] + @options_collectors.inject([]) { |a, c| a + c.all }
end

#all_requirementsObject



36
37
38
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 36

def all_requirements
  all.inject({}) { |h, c| h.merge(c.requirements) }
end

#collect(&block) ⇒ Object



46
47
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 46

def collect(&block)
end

#should(matcher) ⇒ Object

Should just add the results of calling a matcher (i.e. the returned hash) to the list of requirements



17
18
19
20
21
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 17

def should(matcher)
  raise "Bad Matcher" unless matcher.is_a?(Array) and matcher.length == 2
  key, args = matcher
  @requirements[key] = (@requirements.key?(key)) ? @requirements[key] + args : args
end

#with(options) ⇒ Object

spin off a duplicate collector with the extra options specified



24
25
26
27
28
# File 'lib/acts_as_acts_as/requirements/collector.rb', line 24

def with(options)
  @options_collectors << self.class.new(options)
  @options_collectors.last.requirements = self.requirements.dup # dup important so my reqs don't change
  return @options_collectors.last
end