Class: Depression
- Inherits:
-
Object
- Object
- Depression
- Defined in:
- lib/depression.rb,
lib/depression/version.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(items) ⇒ Depression
constructor
A new instance of Depression.
- #process ⇒ Object
Constructor Details
#initialize(items) ⇒ Depression
Returns a new instance of Depression.
10 11 12 |
# File 'lib/depression.rb', line 10 def initialize(items) @items, @result, @changed = items, [], false end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
8 9 10 |
# File 'lib/depression.rb', line 8 def items @items end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
8 9 10 |
# File 'lib/depression.rb', line 8 def result @result end |
Class Method Details
.process(items) ⇒ Object
2 3 4 5 6 |
# File 'lib/depression.rb', line 2 def self.process(items) graph = new(items) graph.process graph.result end |
Instance Method Details
#process ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/depression.rb', line 14 def process items.each do |item| unless item.respond_to?(:name) && item.name raise "Items need to have a name." end end @data = Hash[*items.map do |item| [item.name, Data.new(0, [])] end.flatten] build_dependencies @data.each do |name, data| increment(name) unless data.relations.empty? data.relations.each do |rel| increment(rel.name) if @data[rel.name].weight <= data.weight end end @result = items.sort do |item, other| @data[item.name].weight.<=>(@data[other.name].weight) end end |