Class: Anchormodel
- Inherits:
-
Object
- Object
- Anchormodel
- Defined in:
- lib/anchormodel.rb,
lib/anchormodel/version.rb,
lib/anchormodel/simple_form_inputs/helpers/anchormodel_inputs_common.rb
Overview
Inherit from this class and place your Anchormodel under app/anchormodels/your_anchor_model.rb
. Use it like YourAnchorModel
.
Refer to the README for usage.
Defined Under Namespace
Modules: ModelMixin, SimpleFormInputs, Util, Version Classes: ActiveModelTypeValueMulti, ActiveModelTypeValueSingle, Attribute
Instance Attribute Summary collapse
- #index ⇒ Object readonly
- #key ⇒ Object readonly
Class Method Summary collapse
-
.all ⇒ Object
Returns all possible values this Anchormodel can take.
-
.find(key) ⇒ Object
Retrieves a particular value given the key.
-
.first ⇒ Object
Shorthand to satisfy rubocop.
-
.form_collection ⇒ Object
Returns an array of tuples [label, key] suitable for passing as a collection to some form input helpers.
-
.setup! ⇒ Object
When a descendant of Anchormodel is first used, it must overwrite the class_attributes to prevent cross-class pollution.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json ⇒ Object
-
#initialize(key, **attributes) ⇒ Anchormodel
constructor
Call this initializer directly in your Anchormodel class.
- #inspect ⇒ Object
-
#label ⇒ Object
Returns a Rails label that is compatible with the Rails FastGettext gem.
- #to_s ⇒ Object
Constructor Details
#initialize(key, **attributes) ⇒ Anchormodel
Call this initializer directly in your Anchormodel class. To set @foo=:bar
for anchor :ter
, use new(:ter, foo: :bar)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/anchormodel.rb', line 48 def initialize(key, **attributes) self.class.setup! unless self.class.setup_completed @key = key.to_sym @index = entries_list.count # Save attributes as instance variables attributes.each do |k, v| instance_variable_set(:"@#{k}", v) end # Register self entries_list << self entries_hash[key] = self # Register valid keys valid_keys << key # Define boolean reader self.class.define_method(:"#{key}?") do @key == key end end |
Instance Attribute Details
#index ⇒ Object (readonly)
6 7 8 |
# File 'lib/anchormodel.rb', line 6 def index @index end |
#key ⇒ Object (readonly)
5 6 7 |
# File 'lib/anchormodel.rb', line 5 def key @key end |
Class Method Details
.all ⇒ Object
Returns all possible values this Anchormodel can take.
24 25 26 |
# File 'lib/anchormodel.rb', line 24 def self.all entries_list end |
.find(key) ⇒ Object
Retrieves a particular value given the key. Fails if not found.
40 41 42 43 |
# File 'lib/anchormodel.rb', line 40 def self.find(key) return nil if key.nil? return entries_hash[key.to_sym] || fail("Retreived undefined anchor model key #{key.inspect} for #{inspect}.") end |
.first ⇒ Object
Shorthand to satisfy rubocop
29 30 31 |
# File 'lib/anchormodel.rb', line 29 def self.first all.first end |
.form_collection ⇒ Object
Returns an array of tuples [label, key] suitable for passing as a collection to some form input helpers
34 35 36 |
# File 'lib/anchormodel.rb', line 34 def self.form_collection entries_list.map { |el| [el.label, el.key.to_s] } end |
.setup! ⇒ Object
When a descendant of Anchormodel is first used, it must overwrite the class_attributes to prevent cross-class pollution.
15 16 17 18 19 20 21 |
# File 'lib/anchormodel.rb', line 15 def self.setup! fail("`setup!` was called twice for Anchormodel subclass #{self}.") if setup_completed self.entries_list = entries_list.dup self.entries_hash = entries_hash.dup self.valid_keys = valid_keys.dup self.setup_completed = true end |
Instance Method Details
#==(other) ⇒ Object
72 73 74 |
# File 'lib/anchormodel.rb', line 72 def ==(other) self.class == other.class && key == other.key end |
#as_json ⇒ Object
89 90 91 |
# File 'lib/anchormodel.rb', line 89 def as_json key.to_s end |
#inspect ⇒ Object
81 82 83 |
# File 'lib/anchormodel.rb', line 81 def inspect "#<#{self.class.name}<#{key}>:#{hash}>" end |
#label ⇒ Object
Returns a Rails label that is compatible with the Rails FastGettext gem.
77 78 79 |
# File 'lib/anchormodel.rb', line 77 def label I18n.t("#{self.class.name.demodulize}|#{key.to_s.humanize}") end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/anchormodel.rb', line 85 def to_s inspect end |