Class: HasHelpers::Cards::CardBuilder::Base
- Inherits:
-
Object
- Object
- HasHelpers::Cards::CardBuilder::Base
- Defined in:
- app/models/has_helpers/cards/card_builder/base.rb
Class Method Summary collapse
-
.application(name = nil) ⇒ Object
DSL macro:.
- .build_definitions_from(block) ⇒ Object
-
.define(&block) ⇒ Object
DSL entry point:.
- .definitions ⇒ Object
Class Method Details
.application(name = nil) ⇒ Object
DSL macro:
class FooCard < Base application "AgenciesHQ" end
- setter when name is provided
- getter when called without args
16 17 18 19 20 21 |
# File 'app/models/has_helpers/cards/card_builder/base.rb', line 16 def application(name = nil) if name @application_name = name.to_s end @application_name || (superclass.respond_to?(:application) ? superclass.application : nil) end |
.build_definitions_from(block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/models/has_helpers/cards/card_builder/base.rb', line 52 def self.build_definitions_from(block) context = DefinitionContext.new(self) context.instance_eval(&block) card_definitions = context.build_each @definitions = card_definitions.map do |card_hash| # propagate application from the card class (if set) card_hash[:application] ||= application # ensure identifier constant exists when derived (identifier is a name string here) if card_hash[:identifier].present? && card_hash[:identifier].is_a?(String) && ::HasHelpers::Cards::CardIdentifier.respond_to?(:define_constant) && ::HasHelpers::Cards::CardIdentifier.respond_to?(:lookup) && ::HasHelpers::Cards::CardIdentifier.lookup(card_hash[:identifier]).nil? ::HasHelpers::Cards::CardIdentifier.define_constant(card_hash[:identifier]) end HasHelpers::Cards::Registry.register_card!(card_hash) card_hash end end |
.define(&block) ⇒ Object
DSL entry point:
class DemographicsCard < HasHelpers::Cards::CardBuilder::Base define do resources { add Advisor } card "Demo", relationship: "demo", identifier: "CardIdentifier::DEMOGRAPHICS", layout: LayoutType::KEY_VALUE end end
This:
- evaluates the DSL in a DefinitionContext
- builds normalized hashes
- stores them in .definitions
- registers them in HasHelpers::Cards::Registry
39 40 41 42 43 44 45 |
# File 'app/models/has_helpers/cards/card_builder/base.rb', line 39 def self.define(&block) raise ArgumentError, "Block required for .define" unless block @definition_block = block @definitions = nil HasHelpers::Cards::Registry.register_definition(owner: self, block: block) end |
.definitions ⇒ Object
47 48 49 50 |
# File 'app/models/has_helpers/cards/card_builder/base.rb', line 47 def self.definitions HasHelpers::Cards::Registry.ensure_definitions_for(self) @definitions end |