Module: ExtendIt::ArrayOf::ArrayClassMethods

Defined in:
lib/extend_it/array_of.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#finderObject (readonly)

Returns the value of attribute finder.



23
24
25
# File 'lib/extend_it/array_of.rb', line 23

def finder
  @finder
end

#scopesObject (readonly)

Returns the value of attribute scopes.



23
24
25
# File 'lib/extend_it/array_of.rb', line 23

def scopes
  @scopes
end

Instance Method Details

#entity?(obj) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/extend_it/array_of.rb', line 33

def entity?(obj)
  obj.is_a?(@entity_class)
end

#find_by(name, &block) ⇒ Object



49
50
51
52
# File 'lib/extend_it/array_of.rb', line 49

def find_by(name, &block)
  name = name.ensure_symbol!
  @finder = block.nil? ? proc { |e| e.send(name) } : block
end

#finder?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/extend_it/array_of.rb', line 54

def finder?
  !@finder.nil?
end

#scope(*names, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/extend_it/array_of.rb', line 37

def scope(*names, &block)
  names = names.ensure_array(:flatten, :ensure_symbol, :compact, :uniq)
  names.each do |name|
    @scopes[name] = block.nil? ? proc { |e| e.send(name) } : block
    str = name.to_s
    if str[-1] == '?'
      @scopes[str[0..-2].to_sym] =
        block.nil? ? proc { |e| e.send(name) } : block
    end
  end
end

#select(arr) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/extend_it/array_of.rb', line 25

def select(arr)
  if arr.is_a?(self)
    arr.to_a
  else
    arr.select { |a| a.is_a?(@entity_class) }
  end
end