Class: Senren::Rails::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/senren/rails/registry.rb

Overview

Loads, validates, and queries the Senren component registry.

reg = Senren::Rails::Registry.load!
reg.find("button")           # => Component struct
reg.dependencies("dialog")    # => [<button>]
reg.group("forms")            # => [<form>, <input>, ...]

Defined Under Namespace

Classes: Component

Constant Summary collapse

REQUIRED_KEYS =
%w[category client can_have_client files depends_on pairs_with variants accessibility ai].freeze
VALID_CATEGORIES =
%w[actions forms overlays navigation layout data saas rich].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components_yaml, groups_yaml, recipes_yaml) ⇒ Registry

Returns a new instance of Registry.



45
46
47
48
49
# File 'lib/senren/rails/registry.rb', line 45

def initialize(components_yaml, groups_yaml, recipes_yaml)
  @components = parse_components(components_yaml)
  @groups     = (groups_yaml || {}).fetch('groups', [])
  @recipes    = (recipes_yaml || {}).fetch('recipes', {})
end

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components.



31
32
33
# File 'lib/senren/rails/registry.rb', line 31

def components
  @components
end

#groupsObject (readonly)

Returns the value of attribute groups.



31
32
33
# File 'lib/senren/rails/registry.rb', line 31

def groups
  @groups
end

#recipesObject (readonly)

Returns the value of attribute recipes.



31
32
33
# File 'lib/senren/rails/registry.rb', line 31

def recipes
  @recipes
end

Class Method Details

.load!(components_path: Senren::Rails.registry_path, groups_path: Senren::Rails.groups_path, recipes_path: Senren::Rails.recipes_path) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/senren/rails/registry.rb', line 33

def self.load!(
  components_path: Senren::Rails.registry_path,
  groups_path:     Senren::Rails.groups_path,
  recipes_path:    Senren::Rails.recipes_path
)
  new(
    YAML.safe_load_file(components_path, aliases: false),
    YAML.safe_load_file(groups_path, aliases: false),
    YAML.safe_load_file(recipes_path, aliases: false)
  ).tap(&:validate!)
end

Instance Method Details

#allObject



60
61
62
# File 'lib/senren/rails/registry.rb', line 60

def all
  @components.values
end

#dependencies(*requested) ⇒ Object

Returns the transitive dependency closure for one or more components, in install order (deps first), de-duplicated.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/senren/rails/registry.rb', line 86

def dependencies(*requested)
  requested = requested.flatten.map(&:to_s)
  result = []
  visiting = []

  visit = lambda do |name|
    return if result.include?(name)
    raise "Circular dependency: #{(visiting + [name]).join(' -> ')}" if visiting.include?(name)

    visiting << name
    comp = fetch(name)
    comp.depends_on.each { |dep| visit.call(dep) }
    visiting.pop
    result << name
  end

  requested.each { |name| visit.call(name) }
  result
end

#eachObject



64
65
66
# File 'lib/senren/rails/registry.rb', line 64

def each(&)
  all.each(&)
end

#fetch(name) ⇒ Object



55
56
57
58
# File 'lib/senren/rails/registry.rb', line 55

def fetch(name)
  find(name) or raise ArgumentError, "Unknown component: #{name.inspect}. " \
                                     "Known: #{@components.keys.sort.join(', ')}"
end

#find(name) ⇒ Object



51
52
53
# File 'lib/senren/rails/registry.rb', line 51

def find(name)
  @components[name.to_s]
end

#find_eachObject



68
69
70
# File 'lib/senren/rails/registry.rb', line 68

def find_each(&)
  each(&)
end

#group(category_id) ⇒ Object



76
77
78
# File 'lib/senren/rails/registry.rb', line 76

def group(category_id)
  @components.values.select { |c| c.category == category_id.to_s }
end

#namesObject



72
73
74
# File 'lib/senren/rails/registry.rb', line 72

def names
  @components.keys
end

#recipe(id) ⇒ Object



80
81
82
# File 'lib/senren/rails/registry.rb', line 80

def recipe(id)
  @recipes.fetch(id.to_s) { raise ArgumentError, "Unknown recipe: #{id}" }
end

#validate!Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/senren/rails/registry.rb', line 106

def validate!
  errors = []

  validate_components(errors)
  validate_recipes(errors)

  raise "Registry validation failed:\n - #{errors.join("\n - ")}" if errors.any?

  self
end