Class: BinderCore::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/binder_core/parser.rb

Overview

This is the base class for all parsers. When it is instanciated it is assigned one FileContext, which may be a file or a folder. Subclasses must define a parse method which is called when the time is right.

If the parse method doesn’t actively add data, create a new asset and/or descend into a folder then nothing gets added to the binder data and the scanner essentaily skips this file/folder.

Hence the NullParser implmentation:

class NullParser < BinderCore::Parser
  def parse
  end
end

Direct Known Subclasses

AssetParser, FolderParser, NullParser, TextParser

Instance Method Summary collapse

Constructor Details

#initialize(file_context) ⇒ Parser

Returns a new instance of Parser.



17
18
19
# File 'lib/binder_core/parser.rb', line 17

def initialize(file_context)
  @context = file_context
end

Instance Method Details

#add(data, key = "") ⇒ Object

adds a data value object to the parent hash you can specify the key if you supply a second parameter, otherwise ‘self.key’ is used



38
39
40
41
# File 'lib/binder_core/parser.rb', line 38

def add(data, key = "")
  if !key.empty? then @context.name = key end
  @context.data = data
end

#consoleObject

The console is used for debugging, it allows you to log messages and warnings. You can also trigger an internal error halting the parsing process. Check out BinderCore::Console



54
55
56
# File 'lib/binder_core/parser.rb', line 54

def console
  @context.console
end

#descend(&config) ⇒ Object

Instructs the compiler to decend into this folder and scan. You can override the path in the config block if you want to direct the compiler to a folder elsewhere. It will happily scan a different folder without disrupting the rest of the process.

The config block works the same as the one used when you defined the binder. This means that you can add new parser definitions which will only be applied to children of the folder… powerful stuff!

This method returns the data gleemed from that scanning process, you need to add it to make it part of the binder data, it will not do so automatically!

On the other hand, assets ARE automatically added to the binder asset bucket when created.

This returns an empty hash if no data is added from the contents of this folder



78
79
80
81
82
# File 'lib/binder_core/parser.rb', line 78

def descend(&config)
  config ||= lambda { |c|  }
  raw = @context.descend(&config)
  raw[:data]
end

#fileObject

Access FileRef object associated with this Parser



22
23
24
# File 'lib/binder_core/parser.rb', line 22

def file
  @context.file
end

#keyObject



32
33
34
# File 'lib/binder_core/parser.rb', line 32

def key
  @context.name
end

#key=(val) ⇒ Object

if you dont supply a second argument to the ‘add’ method this is the key that will be used when adding a data value to the parent hash



28
29
30
# File 'lib/binder_core/parser.rb', line 28

def key=(val)
  @context.name = val
end

#new_asset(path = nil) ⇒ Object

This creates a new asset object and adds it to the binder asset bucket. You have the option of supplying a path to a file, otherwise it creates an asset using the current file path



46
47
48
49
# File 'lib/binder_core/parser.rb', line 46

def new_asset(path = nil)
  path ||= @context.file.path
  @context.register_asset BinderCore::Asset.new( path )
end

#paramsObject

Access to arbitary user params inherited from the parent context. Any changes you make to this will be similarly inherited by direct children using a shallow duplicate.



60
61
62
# File 'lib/binder_core/parser.rb', line 60

def params
  @context.params
end