Module: MarcBot

Defined in:
lib/marc_bot/find_definitions.rb,
lib/marc_bot.rb,
lib/marc_bot/factory.rb,
lib/marc_bot/version.rb,
lib/marc_bot/registry.rb,
lib/marc_bot/field_builder.rb

Overview

Defined Under Namespace

Classes: Error, Factory, FieldBuilder, Registry

Constant Summary collapse

VERSION =
"0.2.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.definition_file_pathsObject

An Array of strings specifying locations that should be searched for MARC record definitions. By default, marc_bot will attempt to require “records”, “test/records” and “spec/records”. Only the first existing file will be loaded.



7
8
9
# File 'lib/marc_bot/find_definitions.rb', line 7

def definition_file_paths
  @definition_file_paths
end

Class Method Details

.build(record_symbol, **options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/marc_bot.rb', line 24

def build(record_symbol, **options)
  find_definitions if factories.nil?

  record_factory = factories.find(record_symbol)
  factory = Factory.new
  factory.instance_exec(&record_factory)
  options.map do |option|
    factory.send(option[0]) { option[1] }
  end
  factory.record
end

.define(&block) ⇒ Object



15
16
17
# File 'lib/marc_bot.rb', line 15

def define(&block)
  instance_exec(&block)
end

.factoriesObject



41
42
43
# File 'lib/marc_bot.rb', line 41

def factories
  @factories ||= Registry.new("Factory")
end

.factory(record_name, &block) ⇒ Object



19
20
21
22
# File 'lib/marc_bot.rb', line 19

def factory(record_name, &block)
  block ||= ->(result) { result }
  factories.register(record_name, block)
end

.find_definitionsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/marc_bot/find_definitions.rb', line 12

def self.find_definitions
  absolute_definition_file_paths = definition_file_paths.map { |path|
    File.expand_path(path)
  }

  absolute_definition_file_paths.uniq.each do |path|
    load("{path}.rb") if File.exist?("{path}.rb")

    if File.directory? path
      Dir[File.join(path, "**", "*.rb")].sort.each do |file|
        load file
      end
    end
  end
end

.reloadObject



36
37
38
39
# File 'lib/marc_bot.rb', line 36

def reload
  @factories = nil
  find_definitions
end