Class: PgSerializable::Trait

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_serializable/trait.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Trait

Returns a new instance of Trait.



5
6
7
8
# File 'lib/pg_serializable/trait.rb', line 5

def initialize(klass)
  @klass = klass
  @attribute_nodes = []
end

Instance Attribute Details

#attribute_nodesObject (readonly)

Returns the value of attribute attribute_nodes.



3
4
5
# File 'lib/pg_serializable/trait.rb', line 3

def attribute_nodes
  @attribute_nodes
end

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/pg_serializable/trait.rb', line 3

def klass
  @klass
end

Instance Method Details

#attribute(column_name, label: nil, &blk) ⇒ Object



16
17
18
# File 'lib/pg_serializable/trait.rb', line 16

def attribute(column_name, label: nil, &blk)
  @attribute_nodes << Nodes::Attribute.new(klass, column_name, label: label, &blk)
end

#attributes(*attrs) ⇒ Object



10
11
12
13
14
# File 'lib/pg_serializable/trait.rb', line 10

def attributes(*attrs)
  attrs.each do |attribute|
    @attribute_nodes << Nodes::Attribute.new(klass, attribute)
  end
end

#belongs_to(association, label: nil, trait: :default) ⇒ Object



24
25
26
# File 'lib/pg_serializable/trait.rb', line 24

def belongs_to(association, label: nil, trait: :default)
  @attribute_nodes << Nodes::Association.new(klass, association, :belongs_to, label: label, trait: trait)
end

#has_and_belongs_to_many(association, label: nil, trait: :default) ⇒ Object



32
33
34
# File 'lib/pg_serializable/trait.rb', line 32

def has_and_belongs_to_many(association, label: nil, trait: :default)
  @attribute_nodes << Nodes::Association.new(klass, association, :has_and_belongs_to_many, label: label, trait: trait)
end

#has_many(association, label: nil, trait: :default) ⇒ Object



20
21
22
# File 'lib/pg_serializable/trait.rb', line 20

def has_many(association, label: nil, trait: :default)
  @attribute_nodes << Nodes::Association.new(klass, association, :has_many, label: label, trait: trait)
end

#has_one(association, label: nil, trait: :default) ⇒ Object



28
29
30
# File 'lib/pg_serializable/trait.rb', line 28

def has_one(association, label: nil, trait: :default)
  @attribute_nodes << Nodes::Association.new(klass, association, :has_one, label: label, trait: trait)
end