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, options = {}) ⇒ Program

Returns a new instance of Program.



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

def initialize(mappings, options = {})
  @mappings ||= mappings
  @prefixes = options[:prefixes] || {}
  @filters = options[:filters] || []
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



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

def filters
  @filters
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



36
37
38
39
40
41
42
43
# File 'lib/ldpath/program.rb', line 36

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