Module: ReeDao::DSL::ClassMethods

Includes:
Ree::Contracts::ArgContracts, Ree::Contracts::Core
Defined in:
lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb

Defined Under Namespace

Classes: DaoFilter

Instance Method Summary collapse

Instance Method Details

#dao(name, &proc) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb', line 60

def dao(name, &proc)
  if !block_given?
    raise Ree::Error.new("dao requires block to link specific db connection as :db => link :some_db, as: :db", :invalid_dsl_usage)
  end

  dsl = Ree::ObjectDsl.new(
    Ree.container.packages_facade, name, self, :object
  )

  dsl.instance_exec(&proc)
  dsl.tags(["object", "dao"])

  db_link = dsl.object.links.detect { _1.as == :db }

  if !db_link
    raise Ree::Error.new("dao should link specific db connection as :db => link :some_db, as: :db", :invalid_dsl_usage)
  end

  if dsl.object.factory? && dsl.object.factory != :build
    raise Ree::Error.new("dao DSL automatically assigns `factory :build` to object. Custom factory methods are not supported", :invalid_dsl_usage)
  end

  dsl.link(:build_dao, from: :ree_dao)
  dsl.factory(:build)
  dsl.singleton

  # automatically assign table name from object class
  table(
    ReeString::Underscore.new
      .call(self.name.split("::").last.gsub(/dao$/i, ''))
      .to_sym
  )

  Ree.container.compile(dsl.package, name)
end

#default_select_columns(columns) ⇒ Object



108
109
110
111
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb', line 108

def default_select_columns(columns)
  @default_select_columns = columns
  nil
end

#filter(name, proc) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb', line 131

def filter(name, proc)
  filters = self.instance_variable_get(:@filters)

  if filters.nil?
    filters = []
    self.instance_variable_set(:@filters, filters)
  end

  filters.push(DaoFilter.new(name, proc))

  nil
end

#primary_key(primary_key) ⇒ Object



102
103
104
105
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb', line 102

def primary_key(primary_key)
  @primary_key = primary_key
  nil
end

#schema(dto_class, &proc) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb', line 114

def schema(dto_class, &proc)
  mapper_factory = ReeMapper.get_mapper_factory(
    Object.const_get(self.name.split('::').first)
  )

  mapper = mapper_factory
    .call
    .use(:db_dump)
    .use(:db_load, dto: dto_class, &proc)

  self.instance_variable_set(:@schema_mapper, mapper)
  nil
end

#table(table_name) ⇒ Object



97
98
99
# File 'lib/ree_lib/packages/ree_dao/package/ree_dao/dsl.rb', line 97

def table(table_name)
  @table = table_name
end