Class: Anchormodel

Inherits:
Object
  • Object
show all
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 YourAnchorModel. Refer to the README for usage.

Defined Under Namespace

Modules: ModelMixin, Version Classes: ActiveModelTypeValue, Attribute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)

Parameters:

  • key (String, Symbol)

    The key under which the entry should be stored.

  • attributes

    All named arguments to Anchormodel are made available as instance attributes.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/anchormodel.rb', line 38

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
end

Instance Attribute Details

#indexObject (readonly)



6
7
8
# File 'lib/anchormodel.rb', line 6

def index
  @index
end

#keyObject (readonly)



5
6
7
# File 'lib/anchormodel.rb', line 5

def key
  @key
end

Class Method Details

.allObject

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.

Parameters:

  • key (String, Symbol)

    The key of the value that should be retrieved.



30
31
32
33
# File 'lib/anchormodel.rb', line 30

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

.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



57
58
59
# File 'lib/anchormodel.rb', line 57

def ==(other)
  self.class == other.class && key == other.key
end

#inspectObject



66
67
68
# File 'lib/anchormodel.rb', line 66

def inspect
  "#<#{self.class.name}<#{key}>:#{hash}>"
end

#labelObject

Returns a Rails label that is compatible with the Rails FastGettext gem.



62
63
64
# File 'lib/anchormodel.rb', line 62

def label
  I18n.t("#{self.class.name.demodulize}|#{key.to_s.humanize}")
end

#to_sObject



70
71
72
# File 'lib/anchormodel.rb', line 70

def to_s
  inspect
end