Class: Anchormodel
- Inherits:
-
Object
- Object
- Anchormodel
- Defined in:
- lib/anchormodel.rb,
lib/anchormodel/version.rb
Overview
Inherit from this class and place your Anchormodel under app/anchormodels/your_anchor_model.rb
. Use it like Anchormodels::YourAnchorModel
.
Refer to the README for usage.
Defined Under Namespace
Modules: ModelMixin, Version Classes: ActiveModelTypeValue, 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.
Instance Method Summary collapse
- #==(other) ⇒ 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)
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/anchormodel.rb', line 27 def initialize(key, **attributes) @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 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.
13 14 15 |
# File 'lib/anchormodel.rb', line 13 def self.all entries_list end |
.find(key) ⇒ Object
Retrieves a particular value given the key. Fails if not found.
19 20 21 22 |
# File 'lib/anchormodel.rb', line 19 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 |
Instance Method Details
#==(other) ⇒ Object
44 45 46 |
# File 'lib/anchormodel.rb', line 44 def ==(other) self.class == other.class && key == other.key end |
#inspect ⇒ Object
53 54 55 |
# File 'lib/anchormodel.rb', line 53 def inspect "#<#{self.class.name}<#{key}>:#{hash}>" end |
#label ⇒ Object
Returns a Rails label that is compatible with the Rails FastGettext gem.
49 50 51 |
# File 'lib/anchormodel.rb', line 49 def label I18n.t("#{self.class.name.demodulize}|#{key.to_s.humanize}") end |
#to_s ⇒ Object
57 58 59 |
# File 'lib/anchormodel.rb', line 57 def to_s inspect end |