Class: Airview::Resource
- Inherits:
-
Object
- Object
- Airview::Resource
- Defined in:
- lib/airview/resource.rb,
sig/airview.rbs
Instance Attribute Summary collapse
-
#fields ⇒ Hash[Symbol, Field]
readonly
Returns the value of attribute fields.
-
#key ⇒ Symbol
readonly
Returns the value of attribute key.
-
#label ⇒ String
Returns the value of attribute label.
-
#label_method ⇒ Symbol
Returns the value of attribute label_method.
-
#model ⇒ String
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #default_fields ⇒ Object
- #editable_fields ⇒ Array[Field]
- #field(name, type:, label: nil, **options) ⇒ Field
- #field!(name) ⇒ Field
-
#initialize(key, model:) ⇒ Resource
constructor
A new instance of Resource.
- #model_class ⇒ Object
- #record_label(record) ⇒ String
- #validate! ⇒ void
Constructor Details
#initialize(key, model:) ⇒ Resource
Returns a new instance of Resource.
8 9 10 11 12 13 14 |
# File 'lib/airview/resource.rb', line 8 def initialize(key, model:) @key = key.to_sym @model = model @label = key.to_s.humanize @label_method = :to_s @fields = {} end |
Instance Attribute Details
#fields ⇒ Hash[Symbol, Field] (readonly)
Returns the value of attribute fields.
6 7 8 |
# File 'lib/airview/resource.rb', line 6 def fields @fields end |
#key ⇒ Symbol (readonly)
Returns the value of attribute key.
6 7 8 |
# File 'lib/airview/resource.rb', line 6 def key @key end |
#label ⇒ String
Returns the value of attribute label.
5 6 7 |
# File 'lib/airview/resource.rb', line 5 def label @label end |
#label_method ⇒ Symbol
Returns the value of attribute label_method.
5 6 7 |
# File 'lib/airview/resource.rb', line 5 def label_method @label_method end |
#model ⇒ String (readonly)
Returns the value of attribute model.
6 7 8 |
# File 'lib/airview/resource.rb', line 6 def model @model end |
Instance Method Details
#default_fields ⇒ Object
37 38 39 |
# File 'lib/airview/resource.rb', line 37 def default_fields fields.values.select(&:default_visible?) end |
#editable_fields ⇒ Array[Field]
33 34 35 |
# File 'lib/airview/resource.rb', line 33 def editable_fields fields.values.select(&:editable?) end |
#field(name, type:, label: nil, **options) ⇒ Field
16 17 18 19 20 21 |
# File 'lib/airview/resource.rb', line 16 def field(name, type:, label: nil, **) definition = Field.new(name, type:, label:, **) definition.validate! fields[definition.name] = definition definition end |
#field!(name) ⇒ Field
27 28 29 30 31 |
# File 'lib/airview/resource.rb', line 27 def field!(name) fields.fetch(name.to_sym) do raise ConfigurationError, "Unknown Airview field #{name.inspect} for #{key}" end end |
#model_class ⇒ Object
23 24 25 |
# File 'lib/airview/resource.rb', line 23 def model_class model.to_s.constantize end |
#record_label(record) ⇒ String
47 48 49 |
# File 'lib/airview/resource.rb', line 47 def record_label(record) Airview.record_label(record, label_method) end |
#validate! ⇒ void
This method returns an undefined value.
41 42 43 44 45 |
# File 'lib/airview/resource.rb', line 41 def validate! raise ConfigurationError, "Airview resource #{key} must define at least one field" if fields.empty? fields.each_value(&:validate!) end |