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
- #add_stale_field(field, reason:) ⇒ Object
- #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
- #stale_fields ⇒ Object
- #validate! ⇒ void
Constructor Details
#initialize(key, model:) ⇒ Resource
Returns a new instance of Resource.
8 9 10 11 12 13 14 15 |
# 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 = {} @stale_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
#add_stale_field(field, reason:) ⇒ Object
46 47 48 |
# File 'lib/airview/resource.rb', line 46 def add_stale_field(field, reason:) @stale_fields << { name: field.name.to_sym, label: field.label, reason: } end |
#default_fields ⇒ Object
38 39 40 |
# File 'lib/airview/resource.rb', line 38 def default_fields fields.values.select(&:default_visible?) end |
#editable_fields ⇒ Array[Field]
34 35 36 |
# File 'lib/airview/resource.rb', line 34 def editable_fields fields.values.select(&:editable?) end |
#field(name, type:, label: nil, **options) ⇒ Field
17 18 19 20 21 22 |
# File 'lib/airview/resource.rb', line 17 def field(name, type:, label: nil, **) definition = Field.new(name, type:, label:, **) definition.validate! fields[definition.name] = definition definition end |
#field!(name) ⇒ Field
28 29 30 31 32 |
# File 'lib/airview/resource.rb', line 28 def field!(name) fields.fetch(name.to_sym) do raise ConfigurationError, "Unknown Airview field #{name.inspect} for #{key}" end end |
#model_class ⇒ Object
24 25 26 |
# File 'lib/airview/resource.rb', line 24 def model_class model.to_s.constantize end |
#record_label(record) ⇒ String
56 57 58 |
# File 'lib/airview/resource.rb', line 56 def record_label(record) Airview.record_label(record, label_method) end |
#stale_fields ⇒ Object
42 43 44 |
# File 'lib/airview/resource.rb', line 42 def stale_fields @stale_fields.dup end |
#validate! ⇒ void
This method returns an undefined value.
50 51 52 53 54 |
# File 'lib/airview/resource.rb', line 50 def validate! raise ConfigurationError, "Airview resource #{key} must define at least one field" if fields.empty? fields.each_value(&:validate!) end |