This repository has been archived by the owner on May 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract collection to Collectible module (#34)
* Extract collection to Collectible module * Use nested module blocks instead of compact form * Use yield instead of block.call * Tell Rubocop to ignore nested method definition (But just here) * Add frozen string literal comment to new class
- Loading branch information
Showing
3 changed files
with
44 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
module Nacre | ||
module Collectible | ||
def self.included(base) | ||
base.class_exec do | ||
def initialize(resource_list = []) # rubocop:disable NestedMethodDefinition | ||
@members = [] | ||
|
||
resource_list.each do |resource_params| | ||
members << self.class.resource_class.new(resource_params) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def members | ||
@members | ||
end | ||
|
||
def members=(list) | ||
@members = list | ||
end | ||
|
||
def each | ||
members.each do |member| | ||
yield member | ||
end | ||
end | ||
|
||
def params | ||
members.map(&:params) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,12 @@ | ||
require 'nacre/concerns/collectible' | ||
|
||
module Nacre | ||
class Order::InvoiceCollection | ||
|
||
include Collectible | ||
include Enumerable | ||
|
||
attr_accessor :members | ||
|
||
def initialize(resource_list = []) | ||
self.members = [] | ||
resource_list.each do |resource_params| | ||
members << Nacre::Order::Invoice.new(resource_params) | ||
end | ||
end | ||
|
||
def each(&block) | ||
members.each do |member| | ||
block.call(member) | ||
end | ||
end | ||
|
||
def params | ||
members.map(&:params) | ||
def self.resource_class | ||
Nacre::Order::Invoice | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters