Class: Senren::Rails::Registry
- Inherits:
-
Object
- Object
- Senren::Rails::Registry
- Includes:
- Enumerable
- Defined in:
- lib/senren/rails/registry.rb
Overview
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
-
#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(&block) ⇒ Object
- #fetch(name) ⇒ Object
- #find(name) ⇒ Object
- #find_each(&block) ⇒ 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.
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
#components ⇒ Object (readonly)
Returns the value of attribute components.
31 32 33 |
# File 'lib/senren/rails/registry.rb', line 31 def components @components end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
31 32 33 |
# File 'lib/senren/rails/registry.rb', line 31 def groups @groups end |
#recipes ⇒ Object (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
#all ⇒ Object
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 |
#each(&block) ⇒ Object
64 65 66 |
# File 'lib/senren/rails/registry.rb', line 64 def each(&block) all.each(&block) 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_each(&block) ⇒ Object
68 69 70 |
# File 'lib/senren/rails/registry.rb', line 68 def find_each(&block) each(&block) 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 |
#names ⇒ Object
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 |