Class: Dryml::Taglib

Inherits:
Object
  • Object
show all
Defined in:
lib/dryml/taglib.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src_file) ⇒ Taglib

Returns a new instance of Taglib.



60
61
62
63
# File 'lib/dryml/taglib.rb', line 60

def initialize(src_file)
  @src_file = src_file
  load
end

Class Method Details

.clear_cacheObject



22
23
24
# File 'lib/dryml/taglib.rb', line 22

def clear_cache
  @cache = {}
end

.get(options) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/dryml/taglib.rb', line 9

def get(options)
  taglib_filenames(options).map do |src_file|
    taglib = @cache[src_file]
    if taglib
      taglib.reload
    else
      taglib = Taglib.new(src_file)
      @cache[src_file] = taglib
    end
    taglib
  end
end

Instance Method Details

#import_into(class_or_module, as) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/dryml/taglib.rb', line 104

def import_into(class_or_module, as)
  if as
    # Define a method on class_or_module named whatever 'as'
    # is. The first time the method is called it creates and
    # returns an object that provides the taglib's tags as
    # methods. On subsequent calls the object is cached in an
    # instance variable "@_#{as}_taglib"

    taglib_module = @module
    ivar = "@_#{as}_taglib"
    class_or_module.send(:define_method, as) do
      instance_variable_get(ivar) or begin
                                       as_class = Class.new(TemplateEnvironment) { include taglib_module }
                                       as_object = as_class.new
                                       as_object.copy_instance_variables_from(self)
                                       instance_variable_set(ivar, as_object)
                                     end
    end
  else
    class_or_module.send(:include, @module)
    class_or_module.tag_attrs.update(@module.tag_attrs) if @module.respond_to?(:tag_attrs)
  end
end

#loadObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/dryml/taglib.rb', line 69

def load
  @module = Module.new do

    @tag_attrs = {}
    @tag_aliases = []

    class << self

      def included(base)
        @tag_aliases.each do |tag, feature|
          if base.respond_to? :alias_method_chain_on_include
            base.alias_method_chain_on_include tag, feature
          else
            base.send(:alias_method_chain, tag, feature)
          end
        end
      end

      def _register_tag_attrs(tag, attrs)
        @tag_attrs[tag] = attrs
      end
      attr_reader :tag_attrs

      def alias_method_chain_on_include(tag, feature)
        @tag_aliases << [tag, feature]
      end

    end

  end
  template = Template.new(File.read(@src_file), @module, @src_file)
  template.compile([], [])
  @last_load_time = File.mtime(@src_file)
end

#reloadObject



65
66
67
# File 'lib/dryml/taglib.rb', line 65

def reload
  load if File.mtime(@src_file) > @last_load_time
end