Module: Icss::ReceiverModel::ActsAsCatalog::ClassMethods

Includes:
Icss::ReceiverModel::ActsAsLoadable::ClassMethods
Defined in:
lib/icss/receiver_model/acts_as_catalog.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Icss::ReceiverModel::ActsAsLoadable::ClassMethods

#receive_from_file, #receive_json, #receive_yaml

Class Method Details

.extended(base) ⇒ Object

Include ActAsLoadable for file receivers Declare and initialize registry class/module variable to hash Set after_receiver(:register) for classes to register their objects



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 35

def self.extended(base)
  base.class_eval do
    if is_a?(Class)
      include Icss::ReceiverModel::ActsAsLoadable::ClassMethods
      class_attribute :registry
      class_attribute :_catalog_loaded
      after_receive(:register) do |hsh|
        register(self)
      end
    elsif is_a?(Module)
      mattr_accessor :registry
      mattr_accessor :_catalog_loaded
    end
    self.registry = Hash.new
  end
end

Instance Method Details

#all(name = '*') ⇒ Object

Basic ActiveRecord inspired methods Name can include wildcards(*) for simple searching

- example: geo.*.location.*
           matches anything with a namespace starting with 'geo'
           and containing 'location'


68
69
70
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 68

def all(name='*')
  find(:all, name)
end

#find(name_or_find_type, name = '*') ⇒ Object

ActiveRecord inspired find method Params:

1. :all, :first, :last, or registry identifier(ignores wildcards to force exact match)
2. registry name with wildcards(*)
      - example: geo.*.location.*


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 88

def find(name_or_find_type, name='*')  
  if !self._catalog_loaded
    self._catalog_loaded = true
    load_catalog(true)
  end
          
  method_name = case name_or_find_type
    when :all then :to_a
    when :first then :first
    when :last then :last
    else
      # If exact match not in registry, try looking in file catalog
      result = (find_in_registry(name_or_find_type, :exact_match => true) || load_files_from_catalog(name_or_find_type, :exact_match => true))
      raise Icss::NotFoundError, "Cannot find #{name_or_find_type}" if result.nil?
      return result
  end

  # If not in registry, try looking in file catalog
  (find_in_registry(name) || load_files_from_catalog(name)).send method_name
end

#first(name = '*') ⇒ Object



72
73
74
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 72

def first(name='*')
  find(:first, name)
end

#last(name = '*') ⇒ Object



76
77
78
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 76

def last(name='*')
  find(:last, name)
end

#load_from_catalog(fullname) ⇒ Object



109
110
111
112
113
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 109

def load_from_catalog(fullname)
  filepath = fullname.to_s.gsub(/(\.icss\.yaml)?$/,'').gsub(/\./, '/')
  filenames = catalog_filenames(filepath, [''])
  filenames.each{|filename| receive_from_file(filename) }.compact
end

#register(obj) ⇒ Object

Add object to registry using fullname method as the identifier



56
57
58
# File 'lib/icss/receiver_model/acts_as_catalog.rb', line 56

def register(obj)
  registry[obj.fullname] = obj
end