Class: Traits::List
Direct Known Subclasses
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #by_name(name) ⇒ Object
- #each(&block) ⇒ Object
- #filter(hash) ⇒ Object
- #first_where(hash) ⇒ Object
-
#initialize(list = []) ⇒ List
constructor
A new instance of List.
- #to_hash ⇒ Object
Constructor Details
#initialize(list = []) ⇒ List
Returns a new instance of List.
8 9 10 |
# File 'lib/traits/list.rb', line 8 def initialize(list = []) @list = list end |
Instance Method Details
#[](arg) ⇒ Object
24 25 26 |
# File 'lib/traits/list.rb', line 24 def [](arg) by_name(arg) end |
#by_name(name) ⇒ Object
32 33 34 35 |
# File 'lib/traits/list.rb', line 32 def by_name(name) name = name.to_sym if name.kind_of?(String) find { |attr| attr.name == name } end |
#each(&block) ⇒ Object
28 29 30 |
# File 'lib/traits/list.rb', line 28 def each(&block) @list.each(&block) end |
#filter(hash) ⇒ Object
12 13 14 15 16 |
# File 'lib/traits/list.rb', line 12 def filter(hash) select do |item| hash.all? { |method, expected| compare(item, method, expected) } end end |
#first_where(hash) ⇒ Object
18 19 20 21 22 |
# File 'lib/traits/list.rb', line 18 def first_where(hash) find do |item| hash.all? { |method, expected| compare(item, method, expected) } end end |
#to_hash ⇒ Object
37 38 39 |
# File 'lib/traits/list.rb', line 37 def to_hash each_with_object({}) { |item, memo| memo[item.name] = item.to_hash } end |