Class: Ldpath::Program

Inherits:
Object
  • Object
show all
Defined in:
lib/ldpath/program.rb

Constant Summary collapse

ParseError =
Class.new StandardError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mappings, default_loader: Ldpath::Loaders::Direct.new, prefixes: {}, filters: [], loaders: {}) ⇒ Program

Returns a new instance of Program.



30
31
32
33
34
35
36
37
# File 'lib/ldpath/program.rb', line 30

def initialize(mappings, default_loader: Ldpath::Loaders::Direct.new, prefixes: {}, filters: [], loaders: {})
  @mappings ||= mappings
  @default_loader = default_loader
  @loaders = loaders
  @prefixes = prefixes
  @filters = filters

end

Instance Attribute Details

#default_loaderObject (readonly)

Returns the value of attribute default_loader.



29
30
31
# File 'lib/ldpath/program.rb', line 29

def default_loader
  @default_loader
end

#filtersObject (readonly)

Returns the value of attribute filters.



29
30
31
# File 'lib/ldpath/program.rb', line 29

def filters
  @filters
end

#loadersObject (readonly)

Returns the value of attribute loaders.



29
30
31
# File 'lib/ldpath/program.rb', line 29

def loaders
  @loaders
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



29
30
31
# File 'lib/ldpath/program.rb', line 29

def mappings
  @mappings
end

#prefixesObject (readonly)

Returns the value of attribute prefixes.



29
30
31
# File 'lib/ldpath/program.rb', line 29

def prefixes
  @prefixes
end

Class Method Details

.load(program) ⇒ Object



12
13
14
15
16
# File 'lib/ldpath/program.rb', line 12

def load(program)
  parser.parse(program, reporter: Parslet::ErrorReporter::Deepest.new)
rescue Parslet::ParseFailed => e
  raise ParseError, e.parse_failure_cause.ascii_tree
end

.parse(program, transform_context = {}) ⇒ Object



6
7
8
9
10
# File 'lib/ldpath/program.rb', line 6

def parse(program, transform_context = {})
  ast = transform.apply load(program), transform_context

  Ldpath::Program.new ast.compact, transform_context
end

Instance Method Details

#evaluate(uri, context: nil, limit_to_context: false) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/ldpath/program.rb', line 39

def evaluate(uri, context: nil, limit_to_context: false)
  result = Ldpath::Result.new(self, uri, context: context, limit_to_context: limit_to_context)
  unless filters.empty?
    return {} unless filters.all? { |f| f.evaluate(result, uri, result.context) }
  end

  result.to_hash
end

#load(uri) ⇒ Object



48
49
50
51
52
53
# File 'lib/ldpath/program.rb', line 48

def load(uri)
  loader = loaders.find { |k, v| uri =~ k }&.last
  loader ||= default_loader

  loader.load(uri)
end