Class: DocuBot::MetaSection

Inherits:
Object
  • Object
show all
Defined in:
lib/docubot/metasection.rb,
lib/docubot/metasection.rb

Defined Under Namespace

Modules: Castable

Constant Summary collapse

META_SEPARATOR =

Sort of like +++ATH0

/^\+\+\+\s*$/
NIL_CASTABLE =
nil.extend( Castable )

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}, file_path = nil) ⇒ MetaSection

Returns a new instance of MetaSection.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/docubot/metasection.rb', line 24

def initialize( attrs={}, file_path=nil )
	@attrs = {}
	attrs.each{ |key,value| self[key]=value }
	if file_path && File.exists?( file_path )
		parts = IO.read( file_path, encoding:'utf-8' ).split( META_SEPARATOR, 2 )
		if parts.length > 1
			parts.first.scan(/.+/) do |line|
				next if line =~ /^\s*#/
				next unless line.include?(':')
				attribute, value = line.split(':',2).map{ |str| str.strip }
				self[attribute] = value
			end
		end
		@__contents__ = parts.last && parts.last.strip
	end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/docubot/metasection.rb', line 53

def method_missing( method, *args )
	key=method.to_s
	case key[-1..-1] # the last character of the method name
		when '=' then self[key[0..-2]] = args.first
		else self[key]
	end
end

Instance Attribute Details

#__contents__Object (readonly)

Returns the value of attribute __contents__.



22
23
24
# File 'lib/docubot/metasection.rb', line 22

def __contents__
  @__contents__
end

Instance Method Details

#[](attribute) ⇒ Object



45
46
47
# File 'lib/docubot/metasection.rb', line 45

def []( attribute )
	@attrs.has_key?( attribute ) ? @attrs[attribute] : NIL_CASTABLE
end

#[]=(attribute, value) ⇒ Object



49
50
51
# File 'lib/docubot/metasection.rb', line 49

def []=( attribute, value )
	@attrs[attribute.to_s] = value.extend(Castable)
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_key?( key )
	@attrs.has_key?( key )
end