Module: TypedEAV

Extended by:
ActiveSupport::Autoload
Defined in:
lib/typed_eav.rb,
lib/typed_eav/config.rb,
lib/typed_eav/engine.rb,
lib/typed_eav/version.rb,
lib/typed_eav/registry.rb,
app/models/typed_eav/value.rb,
app/models/typed_eav/option.rb,
lib/typed_eav/has_typed_eav.rb,
lib/typed_eav/query_builder.rb,
app/models/typed_eav/section.rb,
lib/typed_eav/column_mapping.rb,
app/models/typed_eav/field/url.rb,
app/models/typed_eav/field/base.rb,
app/models/typed_eav/field/date.rb,
app/models/typed_eav/field/json.rb,
app/models/typed_eav/field/text.rb,
app/models/typed_eav/field/color.rb,
app/models/typed_eav/field/email.rb,
app/models/typed_eav/field/select.rb,
app/models/typed_eav/field/boolean.rb,
app/models/typed_eav/field/decimal.rb,
app/models/typed_eav/field/integer.rb,
app/models/typed_eav/field/date_time.rb,
app/models/typed_eav/field/long_text.rb,
app/models/typed_eav/field/date_array.rb,
app/models/typed_eav/field/text_array.rb,
app/models/typed_eav/application_record.rb,
app/models/typed_eav/field/multi_select.rb,
app/models/typed_eav/field/decimal_array.rb,
app/models/typed_eav/field/integer_array.rb,
lib/generators/typed_eav/install/install_generator.rb,
lib/generators/typed_eav/scaffold/scaffold_generator.rb

Defined Under Namespace

Modules: ColumnMapping, Field, Generators, HasTypedEAV Classes: ApplicationRecord, Config, Engine, Option, QueryBuilder, Registry, ScopeRequired, Section, Value

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.config {|Config| ... } ⇒ Object Also known as: configure

Yields:



28
29
30
31
# File 'lib/typed_eav.rb', line 28

def config
  yield Config if block_given?
  Config
end

.current_scopeObject

Current ambient scope value. Resolution order:

1. Inside `unscoped { }`      → nil (hard bypass)
2. Innermost `with_scope(v)`  → v
3. Configured `scope_resolver` callable
4. nil

Returns a string (via normalize), or nil when nothing resolves.



44
45
46
47
48
49
50
51
# File 'lib/typed_eav.rb', line 44

def current_scope
  return nil if Thread.current[THREAD_UNSCOPED]

  stack = Thread.current[THREAD_SCOPE_STACK]
  return normalize_scope(stack.last) if stack.present?

  normalize_scope(Config.scope_resolver&.call)
end

.normalize_scope(value) ⇒ Object

Coerce resolver/with_scope/explicit-kwarg inputs into the string shape stored in the ‘scope` column. Accepts raw scalars or AR records.



80
81
82
83
84
# File 'lib/typed_eav.rb', line 80

def normalize_scope(value)
  return nil if value.nil?

  value.respond_to?(:id) ? value.id.to_s : value.to_s
end

.registryObject



35
# File 'lib/typed_eav.rb', line 35

def registry = Registry

.unscopedObject

Run the block with scope enforcement disabled. Queries return results across all scopes. Use for admin tools, migrations, and tests.



65
66
67
68
69
70
71
# File 'lib/typed_eav.rb', line 65

def unscoped
  prev = Thread.current[THREAD_UNSCOPED]
  Thread.current[THREAD_UNSCOPED] = true
  yield
ensure
  Thread.current[THREAD_UNSCOPED] = prev
end

.unscoped?Boolean

True when inside an ‘unscoped { }` block.

Returns:

  • (Boolean)


74
75
76
# File 'lib/typed_eav.rb', line 74

def unscoped?
  !!Thread.current[THREAD_UNSCOPED]
end

.with_scope(value) ⇒ Object

Run the block with ‘value` as the ambient scope, restoring the prior stack on exit (exception-safe). Nests cleanly.



55
56
57
58
59
60
61
# File 'lib/typed_eav.rb', line 55

def with_scope(value)
  stack = (Thread.current[THREAD_SCOPE_STACK] ||= [])
  stack.push(value)
  yield
ensure
  stack&.pop
end