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
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.
38 39 40 41 42 43 |
# File 'lib/senren/rails/registry.rb', line 38 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.
24 25 26 |
# File 'lib/senren/rails/registry.rb', line 24 def components @components end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
24 25 26 |
# File 'lib/senren/rails/registry.rb', line 24 def groups @groups end |
#recipes ⇒ Object (readonly)
Returns the value of attribute recipes.
24 25 26 |
# File 'lib/senren/rails/registry.rb', line 24 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
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/senren/rails/registry.rb', line 26 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
54 |
# File 'lib/senren/rails/registry.rb', line 54 def all = @components.values |
#dependencies(*requested) ⇒ Object
Returns the transitive dependency closure for one or more components, in install order (deps first), de-duplicated.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/senren/rails/registry.rb', line 70 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
55 |
# File 'lib/senren/rails/registry.rb', line 55 def each(&) = all.each(&) |
#fetch(name) ⇒ Object
49 50 51 52 |
# File 'lib/senren/rails/registry.rb', line 49 def fetch(name) find(name) or raise ArgumentError, "Unknown component: #{name.inspect}. " \ "Known: #{@components.keys.sort.join(', ')}" end |
#find(name) ⇒ Object
45 46 47 |
# File 'lib/senren/rails/registry.rb', line 45 def find(name) @components[name.to_s] end |
#group(category_id) ⇒ Object
60 61 62 |
# File 'lib/senren/rails/registry.rb', line 60 def group(category_id) @components.values.select { |c| c.category == category_id.to_s } end |
#names ⇒ Object
58 |
# File 'lib/senren/rails/registry.rb', line 58 def names = @components.keys |
#recipe(id) ⇒ Object
64 65 66 |
# File 'lib/senren/rails/registry.rb', line 64 def recipe(id) @recipes.fetch(id.to_s) { raise ArgumentError, "Unknown recipe: #{id}" } end |
#validate! ⇒ Object
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/senren/rails/registry.rb', line 90 def validate! errors = [] validate_components(errors) validate_recipes(errors) raise "Registry validation failed:\n - #{errors.join("\n - ")}" if errors.any? self end |