Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
Factor out redundant code in resource collections
Browse files Browse the repository at this point in the history
  • Loading branch information
allolex committed Apr 22, 2014
1 parent c93ee11 commit ced2719
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 38 deletions.
31 changes: 31 additions & 0 deletions lib/nacre/abstract_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Nacre
class AbstractCollection

include Enumerable

attr_accessor :members

def initialize(resource_hash = {})
self.members = []
resource_hash.each_pair do |key, value|
members << resource_class.new(key, value)
end
end

def each(&block)
members.each do |member|
block.call(member)
end
end

def params
members.map { |member| { member.key.to_sym => member.params } }
end

private

def resource_class
raise 'Calling class must define .collection_class'
end
end
end
25 changes: 6 additions & 19 deletions lib/nacre/order/assignment_collection.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
module Nacre
class Order::AssignmentCollection

include Enumerable

attr_accessor :members
require 'nacre/abstract_collection'

def initialize(resource_hash = {})
self.members = []
resource_hash.each_pair do |key, value|
members << Nacre::Order::Assignment.new(key, value)
end
end
module Nacre
class Order::AssignmentCollection < AbstractCollection

def each(&block)
members.each do |member|
block.call(member)
end
end
private

def params
members.map { |member| { member.key.to_sym => member.params } }
def resource_class
Nacre::Order::Assignment
end

end
Expand Down
25 changes: 6 additions & 19 deletions lib/nacre/order/row_collection.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
module Nacre
class Order::RowCollection

include Enumerable

attr_accessor :members
require 'nacre/abstract_collection'

def initialize(resource_hash = {})
self.members = []
resource_hash.each_pair do |key, value|
members << Nacre::Order::Row.new(key, value)
end
end
module Nacre
class Order::RowCollection < AbstractCollection

def each(&block)
members.each do |member|
block.call(member)
end
end
private

def params
members.map { |member| { member.key.to_sym => member.params } }
def resource_class
Nacre::Order::Row
end
end
end

0 comments on commit ced2719

Please sign in to comment.