Class: Senren::Rails::Registry
- Inherits:
-
Object
- Object
- Senren::Rails::Registry
- Includes:
- Enumerable
- Defined in:
- lib/senren/rails/registry.rb
Overview
Loads, validates, and queries the Senren component registry.
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
- OPTIONAL_KEYS =
%w[controller stub].freeze
- ALLOWED_KEYS =
(REQUIRED_KEYS + OPTIONAL_KEYS).freeze
- VALID_CATEGORIES =
%w[actions forms overlays navigation layout data saas rich].freeze
- NAME_PATTERN =
/\A[a-z][a-z0-9_]*\z/
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Returns the value of attribute components.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#recipes ⇒ Object
readonly
Returns the value of attribute recipes.
Class Method Summary collapse
Instance Method Summary collapse
- #all ⇒ Object
-
#dependencies(*requested) ⇒ Object
Returns the transitive dependency closure for one or more components, in install order (deps first), de-duplicated.
- #each ⇒ Object (also: #find_each)
- #fetch(name) ⇒ Object
- #find(name) ⇒ Object
- #group(category_id) ⇒ Object
-
#initialize(components_yaml, groups_yaml, recipes_yaml) ⇒ Registry
constructor
A new instance of Registry.
- #names ⇒ Object
- #recipe(id) ⇒ Object
- #validate! ⇒ Object
Constructor Details
#initialize(components_yaml, groups_yaml, recipes_yaml) ⇒ Registry
Returns a new instance of Registry.
35 36 37 38 39 40 |
# File 'lib/senren/rails/registry.rb', line 35 def initialize(components_yaml, groups_yaml, recipes_yaml) @raw_components = (components_yaml || {}).fetch('components', {}) @components = parse_components(components_yaml) @groups = (groups_yaml || {}).fetch('groups', []) @recipes = (recipes_yaml || {}).fetch('recipes', {}) end |
Instance Attribute Details
#components ⇒ Object (readonly)
Returns the value of attribute components.
21 22 23 |
# File 'lib/senren/rails/registry.rb', line 21 def components @components end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
21 22 23 |
# File 'lib/senren/rails/registry.rb', line 21 def groups @groups end |
#recipes ⇒ Object (readonly)
Returns the value of attribute recipes.
21 22 23 |
# File 'lib/senren/rails/registry.rb', line 21 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
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/senren/rails/registry.rb', line 23 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
#all ⇒ Object
51 |
# File 'lib/senren/rails/registry.rb', line 51 def all = @components.values |
#dependencies(*requested) ⇒ Object
Returns the transitive dependency closure for one or more components, in install order (deps first), de-duplicated.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/senren/rails/registry.rb', line 67 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 |
#each ⇒ Object Also known as: find_each
52 |
# File 'lib/senren/rails/registry.rb', line 52 def each(&) = all.each(&) |
#fetch(name) ⇒ Object
46 47 48 49 |
# File 'lib/senren/rails/registry.rb', line 46 def fetch(name) find(name) or raise ArgumentError, "Unknown component: #{name.inspect}. " \ "Known: #{@components.keys.sort.join(', ')}" end |
#find(name) ⇒ Object
42 43 44 |
# File 'lib/senren/rails/registry.rb', line 42 def find(name) @components[name.to_s] end |
#group(category_id) ⇒ Object
57 58 59 |
# File 'lib/senren/rails/registry.rb', line 57 def group(category_id) @components.values.select { |c| c.category == category_id.to_s } end |
#names ⇒ Object
55 |
# File 'lib/senren/rails/registry.rb', line 55 def names = @components.keys |
#recipe(id) ⇒ Object
61 62 63 |
# File 'lib/senren/rails/registry.rb', line 61 def recipe(id) @recipes.fetch(id.to_s) { raise ArgumentError, "Unknown recipe: #{id}" } end |
#validate! ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/senren/rails/registry.rb', line 87 def validate! errors = [] validate_components(errors) validate_recipes(errors) raise "Registry validation failed:\n - #{errors.join("\n - ")}" if errors.any? self end |