Class: Railspress::EntityConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/railspress/entity.rb

Overview

Stores field configuration for a single entity

Stores class name as string for Rails reloader compatibility. Class is resolved lazily via constantize on each access, ensuring fresh class reference after code reload in development.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class_name) ⇒ EntityConfig

Returns a new instance of EntityConfig.



12
13
14
15
16
17
# File 'lib/railspress/entity.rb', line 12

def initialize(model_class_name)
  @model_class_name = model_class_name.to_s
  @field_definitions = {}
  @label = nil # Resolved lazily from model_class if not set
  @types_resolved = false
end

Instance Attribute Details

#labelObject



57
58
59
# File 'lib/railspress/entity.rb', line 57

def label
  @label || model_class.model_name.human.pluralize
end

Instance Method Details

#add_field(name, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/railspress/entity.rb', line 24

def add_field(name, options = {})
  # Store the explicit type or mark for lazy detection
  explicit_type = options[:as]
  @field_definitions[name.to_sym] = {
    type: explicit_type,
    options: options.except(:as),
    needs_detection: explicit_type.nil?
  }

  # Auto-wire virtual attributes for array types
  if explicit_type.in?([ :list, :lines ])
    define_array_accessors(name, explicit_type)
  end
end

#fieldsObject

Access fields with resolved types (lazy resolution)



40
41
42
43
# File 'lib/railspress/entity.rb', line 40

def fields
  resolve_field_types! unless @types_resolved
  @field_definitions
end

#model_classObject

Lazily resolve class - gets fresh class after Rails reload



20
21
22
# File 'lib/railspress/entity.rb', line 20

def model_class
  @model_class_name.constantize
end

#param_keyObject



49
50
51
# File 'lib/railspress/entity.rb', line 49

def param_key
  model_class.model_name.param_key
end

#route_keyObject



45
46
47
# File 'lib/railspress/entity.rb', line 45

def route_key
  model_class.model_name.plural
end

#singular_labelObject



53
54
55
# File 'lib/railspress/entity.rb', line 53

def singular_label
  (label || model_class.model_name.human.pluralize).singularize
end