class Traits::List
Public Class Methods
new(list = [])
click to toggle source
# File lib/traits/list.rb, line 8 def initialize(list = []) @list = list end
Public Instance Methods
[](arg)
click to toggle source
# File lib/traits/list.rb, line 24 def [](arg) by_name(arg) end
by_name(name)
click to toggle source
# File lib/traits/list.rb, line 38 def by_name(name) name = name.to_sym if name.kind_of?(String) find { |attr| attr.name == name } end
each(&block)
click to toggle source
# File lib/traits/list.rb, line 34 def each(&block) @list.each(&block) end
fetch(name)
click to toggle source
# File lib/traits/list.rb, line 28 def fetch(name) el = by_name(name) raise StandardError, "#{name.inspect} not found" unless el el end
filter(hash)
click to toggle source
# 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)
click to toggle source
# 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()
click to toggle source
# File lib/traits/list.rb, line 43 def to_hash each_with_object({}) { |item, memo| memo[item.name] = item.to_hash } end
Protected Instance Methods
compare(item, method, expected)
click to toggle source
# File lib/traits/list.rb, line 48 def compare(item, method, expected) returned = item.send(method) case expected when Array then expected.include?(returned) when Regexp then returned =~ expected else returned == expected end end