Class: ActiveExt::DataStructures::Columns

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_ext/data_structures/columns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(active_record_class, *args) ⇒ Columns

Returns a new instance of Columns.



16
17
18
19
20
21
22
23
# File 'lib/active_ext/data_structures/columns.rb', line 16

def initialize(active_record_class, *args)
  @active_record_class = active_record_class
  @_inheritable = []
  @excluded_columns = []
  @set = []

  self.add *args
end

Instance Attribute Details

#active_record_classObject (readonly)

This accessor is used by ActionColumns to create new Column objects without adding them to this set



14
15
16
# File 'lib/active_ext/data_structures/columns.rb', line 14

def active_record_class
  @active_record_class
end

Instance Method Details

#_inheritable=(value) ⇒ Object

This collection is referenced by other parts of ActiveExt and by methods within this DataStructure. IT IS NOT MEANT FOR PUBLIC USE (but if you know what you’re doing, go ahead)



8
9
10
11
# File 'lib/active_ext/data_structures/columns.rb', line 8

def _inheritable=(value)
  @sorted = true
  @_inheritable = value
end

#add(*args) ⇒ Object Also known as: <<

the way to add columns to the set. this is primarily useful for virtual columns. note that this also makes columns inheritable



27
28
29
30
31
32
33
34
35
# File 'lib/active_ext/data_structures/columns.rb', line 27

def add(*args)
  args.flatten! # allow [] as a param
  args = args.collect { |a| a.to_sym }

  # make the columns inheritable
  @_inheritable.concat(args)
  # then add columns to @set (unless they already exist)
  args.each { |a| @set << ActiveExt::DataStructures::Column.new(a.to_sym, @active_record_class) unless find_by_name(a) }
end

#eachObject



59
60
61
# File 'lib/active_ext/data_structures/columns.rb', line 59

def each
  @set.each { |i| yield i }
end

#exclude(*args) ⇒ Object



39
40
41
42
43
# File 'lib/active_ext/data_structures/columns.rb', line 39

def exclude(*args)
  # only remove columns from _inheritable. we never want to completely forget about a column.
  args.each { |a| @_inheritable.delete a }
  @excluded_columns.concat(args)
end

#exclude_column?(name) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/active_ext/data_structures/columns.rb', line 63

def exclude_column?(name)
  @excluded_columns.include?(name)
end

#find_by_name(name) ⇒ Object Also known as: []

returns the column of the given name.



51
52
53
54
55
# File 'lib/active_ext/data_structures/columns.rb', line 51

def find_by_name(name)
  # this works because of `def column.=='
  column = @set.find { |c| c.name == name }
  column
end

#find_by_names(*names) ⇒ Object

returns an array of columns with the provided names



46
47
48
# File 'lib/active_ext/data_structures/columns.rb', line 46

def find_by_names(*names)
  @set.find_all { |column| names.include? column.name }
end