Sha256: cbb99d45ae3e279e5c701361c70b7fb2ae494966a8e27d697f7b384211e1ad3e
Contents?: true
Size: 1.97 KB
Versions: 1
Compression:
Stored size: 1.97 KB
Contents
# @api description # 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. class Anchormodel attr_reader :key attr_reader :index class_attribute :entries_list, default: [] # For ordering class_attribute :entries_hash, default: {} # For quick access class_attribute :valid_keys, default: Set.new # Returns all possible values this Anchormodel can take. def self.all entries_list end # Retrieves a particular value given the key. Fails if not found. # @param key [String,Symbol] The key of the value that should be retrieved. 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 # Call this initializer directly in your Anchormodel class. To set `@foo=:bar` for anchor `:ter`, use `new(:ter, foo: :bar)` # @param key [String,Symbol] The key under which the entry should be stored. # @param attributes All named arguments to Anchormodel are made available as instance attributes. 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 def ==(other) self.class == other.class && key == other.key end # Returns a Rails label that is compatible with the [Rails FastGettext](https://github.com/grosser/gettext_i18n_rails/) gem. def label I18n.t("#{self.class.name.demodulize}|#{key.to_s.humanize}") end def inspect "#<#{self.class.name}<#{key}>:#{hash}>" end def to_s inspect end end require 'anchormodel/active_model_type_value' require 'anchormodel/attribute' require 'anchormodel/model_mixin' require 'anchormodel/version'
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
anchormodel-0.0.1 | lib/anchormodel.rb |