Class: Fresco::ModelBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/fresco/model_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeModelBuilder

Returns a new instance of ModelBuilder.



30
31
32
33
# File 'lib/fresco/model_builder.rb', line 30

def initialize
  @finders    = []
  @validators = []
end

Instance Attribute Details

#findersObject (readonly)

Returns the value of attribute finders.



28
29
30
# File 'lib/fresco/model_builder.rb', line 28

def finders
  @finders
end

#validatorsObject (readonly)

Returns the value of attribute validators.



28
29
30
# File 'lib/fresco/model_builder.rb', line 28

def validators
  @validators
end

Instance Method Details

#finder(column_name) ⇒ Object

Each call adds one indexed column that gets a ‘where_<col>` finder method on the generated model class. Spinel-shape constraint: each finder is its own typed method, single arity —we don’t generate a dynamic ‘where(hash)` because hash dispatch widens to RbVal.



40
41
42
# File 'lib/fresco/model_builder.rb', line 40

def finder(column_name)
  @finders << column_name
end

#validates(column_name, **opts) ⇒ Object

M4 validations DSL. Currently supports ‘validates :col, presence: true` — emitted as a top-of-method early-return guard inside insert/update. All validation is pure-Ruby (no DB roundtrip); rules expand to monomorphic `==` / `.length` checks so Spinel doesn’t widen the param types.



49
50
51
52
53
# File 'lib/fresco/model_builder.rb', line 49

def validates(column_name, **opts)
  if opts[:presence]
    @validators << { column: column_name, rule: :presence }
  end
end