Class: AdminIt::ResourceDefinition
- Inherits:
-
Object
- Object
- AdminIt::ResourceDefinition
- Defined in:
- lib/admin_it/definitions.rb
Overview
< SimpleDelegator
Constant Summary collapse
- COLLECTIONS =
%i(table tiles list)
- SINGLE =
%i(show new edit)
- CONTEXTS =
COLLECTIONS + SINGLE
Instance Method Summary collapse
- #all(&block) ⇒ Object
- #collection(&block) ⇒ Object
- #context(name, context_class: nil, &block) ⇒ Object
- #contexts(*names, &block) ⇒ Object
- #default_context(value) ⇒ Object
- #exclude_collection ⇒ Object
- #exclude_context(*args) ⇒ Object
- #exclude_single ⇒ Object
- #icon(value = nil) ⇒ Object
-
#initialize(resource) ⇒ ResourceDefinition
constructor
dsl_array :context, ArrayOfContext, create_entity: :create_context.
- #single(&block) ⇒ Object
Constructor Details
#initialize(resource) ⇒ ResourceDefinition
dsl_array :context, ArrayOfContext, create_entity: :create_context
18 19 20 21 22 23 24 |
# File 'lib/admin_it/definitions.rb', line 18 def initialize(resource) @resource = resource @resource.contexts.replace(CONTEXTS.map do |name| Object.const_get("AdminIt::#{name.capitalize}Context") .create_class(name, @resource) end) end |
Instance Method Details
#all(&block) ⇒ Object
67 68 69 |
# File 'lib/admin_it/definitions.rb', line 67 def all(&block) @resource.contexts.each { |c| c.instance_eval(&block) } if block_given? end |
#collection(&block) ⇒ Object
75 76 77 78 |
# File 'lib/admin_it/definitions.rb', line 75 def collection(&block) return unless block_given? @resource.collections.each { |c| c.instance_eval(&block) } end |
#context(name, context_class: nil, &block) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/admin_it/definitions.rb', line 52 def context(name, context_class: nil, &block) Utils.assert_symbol_arg!(name, 'name') context = @resource[name] if context.nil? unless context_class <= Context context_class = Object.const_get("AdminIt::#{name.capitalize}Context") end context = context_class.create_class(name, @resource) @resource.contexts << context end context.instance_eval(&block) if block_given? context end |
#contexts(*names, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/admin_it/definitions.rb', line 41 def contexts(*names, &block) unless names.empty? @resource.contexts.replace( names.flatten.uniq.map { |name| context(name) } ) end if block_given? @resource.contexts.each { |context| context.instance_eval(&block) } end end |
#default_context(value) ⇒ Object
80 81 82 83 |
# File 'lib/admin_it/definitions.rb', line 80 def default_context(value) return if @resource[value].nil? @resource.instance_variable_set(:@default_context, value) end |
#exclude_collection ⇒ Object
33 34 35 |
# File 'lib/admin_it/definitions.rb', line 33 def exclude_collection @resource.contexts.reject! { |c| c.collection? } end |
#exclude_context(*args) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/admin_it/definitions.rb', line 26 def exclude_context(*args) args.flatten.each do |arg| arg = Utils.assert_symbol_arg(arg) { next } @resource.contexts.reject! { |c| c.context_name == arg } end end |
#exclude_single ⇒ Object
37 38 39 |
# File 'lib/admin_it/definitions.rb', line 37 def exclude_single @resource.contexts.reject! { |c| c.single? } end |
#icon(value = nil) ⇒ Object
85 86 87 |
# File 'lib/admin_it/definitions.rb', line 85 def icon(value = nil) @resource.instance_variable_set(:@icon, value.to_s) end |
#single(&block) ⇒ Object
71 72 73 |
# File 'lib/admin_it/definitions.rb', line 71 def single(&block) @resource.singles.each { |c| c.instance_eval(&block) } if block_given? end |