class Flounder::Entity

An entity corresponds to a table in the database. On top of its table name, an entity will know its code name (plural) and its singular name, what to call a single row returned from this entity.

An entity is not a model. In fact, it is what you need to completely hand-roll your models, without being the model itself.

Entities are mainly used to start a query via one of the query initiators. Almost all of the chain methods on the Query object can be used here as well - they will return a query object. Entities, like queries, can be used as enumerables.

Example:

foo = domain.entity(:plural, :singular, 'table_name')

foo.all # SELECT * FROM table_name
foo.where(id: 1).first  # SELECT * FROM table_name WHERE "table_name"."id" = 1

Attributes

domain[R]

@return [Domain] Domain this entity is defined in.

name[R]

@return [Symbol] Name of the entity in plural form.

plural[R]

@return [Symbol] Name of the entity in plural form.

relations[RW]

@return [Array] relations that this entity has to other entities

singular[R]

@return [Symbol] Name of the entity in singular form.

table[R]

@return [Arel::Table] Arel table that underlies this entity.

table_name[R]

@return [String] Name of the table that underlies this entity.

Public Class Methods

new(domain, plural, singular, table_name) click to toggle source
# File lib/flounder/entity.rb, line 27
def initialize domain, plural, singular, table_name
  @domain = domain
  @name = plural
  @singular = singular
  @table_name = table_name
  @columns_hash = nil
  @relations = {} # name -> relation

  @table = Arel::Table.new(table_name)
end

Public Instance Methods

[](name) click to toggle source

@return [Field] Field with name of the entity.

# File lib/flounder/entity.rb, line 63
def [] name
  Field.new(self, name, table[name])
end
add_relationship(type, name, entity_ref=nil, **join_conditions) click to toggle source
# File lib/flounder/entity.rb, line 104
def add_relationship type, name, entity_ref=nil, **join_conditions
  raise ArgumentError, "Must have join conditions." if join_conditions.empty?
  
  relations[name] = Relation.new(
    domain, type, name, entity_ref||name, join_conditions)
end
as(plural, singular) click to toggle source

Temporarily creates a new entity that is available as if it was declared with the given plural and singular, but referencing to the same underlying relation.

# File lib/flounder/entity.rb, line 150
def as plural, singular
  EntityAlias.new(self, plural, singular)
end
belongs_to(*a) click to toggle source

Adds a relationship where one record on the other entity corresponds to possibly many on our side.

Example:

domain.entity :foos, :foo, 'foo' do |foo|
  foo.belongs_to :bar, :bar_id => :id
end

You can also explicitly name the relationship instead of going through the singular name of the other entity:

Example:

domain.entity :foos, :foo, 'foo' do |foo|
  foo.belongs_to :bar_stuff, :bar, :bar_id => :id
end
# File lib/flounder/entity.rb, line 100
def belongs_to *a
  add_relationship :belongs_to, *a
end
column_names() click to toggle source
# File lib/flounder/entity.rb, line 161
def column_names
  columns_hash.keys
end
columns_hash() click to toggle source
# File lib/flounder/entity.rb, line 154
def columns_hash
  @columns_hash ||= begin
    domain.connection_pool.with_connection do |conn|
      conn.columns_hash(table_name)
    end
  end      
end
cond(*conditions) click to toggle source

Builds a condition part or a general SQL expression.

entity.cond(a: 1)
# File lib/flounder/entity.rb, line 115
def cond *conditions
  builder = Expression::Builder.new(domain)
  builder.interpret_conditions(self, conditions)
end
delete() click to toggle source
# File lib/flounder/entity.rb, line 142
def delete 
  Query::Delete.new(domain, self)
end
each(&block) click to toggle source
# File lib/flounder/entity.rb, line 182
def each &block
  all.each &block
end
fields(*names) click to toggle source
# File lib/flounder/entity.rb, line 66
def fields *names
  (names.empty? ? column_names : names).map { |name| self[name] }
end
has_many(*a) click to toggle source

Adds a relationship where many records on the other entity correspond to this one.

# File lib/flounder/entity.rb, line 80
def has_many *a
  add_relationship :has_many, *a
end
insert(hash) click to toggle source
# File lib/flounder/entity.rb, line 132
def insert hash
  Query::Insert.new(domain, self).tap { |q| 
    q.row(hash) }
end
inspect() click to toggle source
# File lib/flounder/entity.rb, line 70
def inspect
  "<Flounder/Entity #{name}(#{table_name})>"
end
Also aliased as: to_s
select() { |q| ... } click to toggle source

Starts a new select query and yields it to the block. Note that you don’t need to call this method to obtain a select query - any of the methods on Query::Select should work on the entity and return a new query object.

@return [Query::Select]

# File lib/flounder/entity.rb, line 126
def select
  Query::Select.new(domain, self).tap { |q| 
    yield q if block_given? 
  }
end
to_s()
Alias for: inspect
update(hash) click to toggle source
# File lib/flounder/entity.rb, line 137
def update hash
  Query::Update.new(domain, self).tap { |u|
    u.set(hash) }
end