Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.4
Comment thread
kinnell marked this conversation as resolved.
NewCops: enable
DisplayCopNames: true
SuggestExtensions: false
Expand Down
108 changes: 108 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,114 @@ ticket.priority_metadata[:sla_hours] # 72
ticket.priority_metadata[:notify_manager] # false
```

### Registry & Standalone Registration

When `enum_field` is used in a model, definitions are automatically registered under a namespace derived from the model class name (e.g., `Campaign` becomes `campaign`).

You can also register definitions directly, outside of models, using the `namespace` DSL:

```ruby
# config/initializers/enum_fields.rb
EnumFields.namespace(:basic) do
enum_field :priority, {
low: {
value: "low",
label: "Low",
},
medium: {
value: "medium",
label: "Medium",
},
high: {
value: "high",
label: "High",
},
}

enum_field :status, {
active: {
value: "active",
label: "Active",
},
inactive: {
value: "inactive",
label: "Inactive",
},
}
end
```

Access the raw registry:

```ruby
EnumFields.registry
# => { "basic" => { "priority" => { ... }, "status" => { ... } }, "campaign" => { ... } }
```

### Catalog

`EnumFields.catalog` returns all registered definitions with namespaces sorted alphabetically, each field's entries as an array of metadata hashes (keys stripped):

```ruby
EnumFields.catalog
# => {
# "basic" => {
# "priority" => [
# {
# "value" => "low",
# "label" => "Low",
# },
# {
# "value" => "medium",
# "label" => "Medium",
# },
# {
# "value" => "high",
# "label" => "High",
# },
# ],
# "status" => [
# {
# "value" => "active",
# "label" => "Active",
# },
# {
# "value" => "inactive",
# "label" => "Inactive",
# },
# ],
# },
# "campaign" => {
# "stage" => [
# {
# "value" => "pending",
# "label" => "Pending",
# "icon" => "clock",
# "color" => "yellow",
# },
# {
# "value" => "processing",
# "label" => "Processing",
# "icon" => "cog",
# "color" => "blue",
# },
# {
# "value" => "shipped",
# "label" => "Shipped",
# "icon" => "truck",
# "color" => "green",
# },
# {
# "value" => "delivered",
# "label" => "Delivered",
# "icon" => "check",
# "color" => "green",
# },
# ],
# },
# }
```

## Development

After checking out the repo, run:
Expand Down
9 changes: 9 additions & 0 deletions lib/enum_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module EnumFields

autoload :Definition
autoload :EnumField
autoload :Namespace
autoload :Registry

def self.registry
Expand All @@ -28,6 +29,14 @@ def self.register(...)
registry.register(...)
end

def self.namespace(name, &)
Namespace.new(name).instance_eval(&)
end

def self.catalog
registry.catalog
end

def self.clear_registry!
@registry = nil
end
Expand Down
8 changes: 4 additions & 4 deletions lib/enum_fields/enum_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ def define!
private

def register!
EnumFields.register(
model_class: @model_class,
EnumFields.register({
namespace: @model_class.name&.underscore || @model_class.object_id.to_s,
accessor: @accessor,
definition: @definition.data
)
definition: @definition.data,
})
end

def store_definition!
Expand Down
17 changes: 17 additions & 0 deletions lib/enum_fields/namespace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module EnumFields
class Namespace
def initialize(namespace)
@namespace = namespace
end

def enum_field(accessor, definition = {})
EnumFields.register({
namespace: @namespace,
accessor: accessor,
definition: definition,
})
end
end
end
17 changes: 13 additions & 4 deletions lib/enum_fields/registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ def initialize
super(@store)
end

def register(model_class:, accessor:, definition:)
key = model_class.name&.underscore || model_class.object_id.to_s
@store[key] ||= {}.with_indifferent_access
@store[key][accessor] = definition
def register(args = {})
namespace = args.fetch(:namespace) { raise ArgumentError, "namespace is required" }
accessor = args.fetch(:accessor) { raise ArgumentError, "accessor is required" }
definition = args.fetch(:definition, {})

@store[namespace] ||= {}.with_indifferent_access
@store[namespace][accessor] = definition
end

def catalog
sort_by { |key, _| key.to_s }.to_h.transform_values do |fields|
fields.transform_values(&:values)
end
end
end
end
Loading