Module: Duxml::Grammar

Includes:
Duxml, LazyOx, PatternMaker, RelaxNG, Spreadsheet, Reportable
Included in:
GrammarClass
Defined in:
lib/duxml/meta/grammar.rb,
lib/duxml/meta/grammar.rb

Overview

module shares name with to activate its methods when that XML Element is encountered in a Meta file

Instance Attribute Summary

Attributes included from Duxml

#doc

Attributes included from Saxer

#io

Attributes included from Reportable

#observer_peers

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RelaxNG

#relaxng

Methods included from Duxml

#load, #log, #save

Methods included from Saxer

#sax

Methods included from LazyOx

#method_missing

Methods included from PatternMaker

#get_child_patterns, #get_existing_attr_patterns, #get_null_attr_patterns, #get_null_child_patterns, #get_relationships

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Duxml::LazyOx

Class Method Details

.import(path) ⇒ GrammarClass, Element

Returns XML Element named and GrammarClass object are functionally equivalent.

Parameters:

  • path (String) —

    path of grammar file; can be in .xlsx, .csv or Duxml GrammarClass file

Returns:

  • (GrammarClass, Element) —

    XML Element named and GrammarClass object are functionally equivalent



43
44
45
46
47
48
49
50
51
# File 'lib/duxml/meta/grammar.rb', line 43

def self.import(path)
  if %w(.xlsx .csv).include?(File.extname path)
    doc = Spreadsheet.sheet_to_xml path
    File.write(File.basename(path)+'.xml', Ox.dump(doc)) #TODO make optional!
    doc
  else
    Ox.parse_obj(File.read path)
  end
end

Instance Method Details

#add_observer(obj, sym = nil) ⇒ Object

Parameters:

  • obj (Object) —

    object that will observe this grammar's rules, usually the History



69
70
71
72
# File 'lib/duxml/meta/grammar.rb', line 69

def add_observer(obj, sym=nil)
  super(obj, sym || :update)
  @rules.each do |r| r.add_observer(obj, :update)end
end

#defined? ⇒ Boolean

Returns whether or not any rules have been defined yet in this grammar.

Returns:

  • (Boolean) —

    whether or not any rules have been defined yet in this grammar



64
65
66
# File 'lib/duxml/meta/grammar.rb', line 64

def defined?
  !rules.empty?
end

#description ⇒ String

Returns lists XML schema and content rules in order of precedence.

Returns:

  • (String) —

    lists XML schema and content rules in order of precedence



75
76
77
78
79
80
# File 'lib/duxml/meta/grammar.rb', line 75

def description
"grammar follows: \n" +
    rules.collect do |change_or_error|
      change_or_error.description
    end.join("\n")
end

#get_rule(change_or_pattern) ⇒ Array[Duxml::Rule]

Returns rules that match the pattern type (e.g. :change_content => :content_rule) and subject (e.g. change_or_pattern.subject.type => 'blobs' && rule.subject => 'blobs').

Parameters:

Returns:

  • (Array[Duxml::Rule]) —

    rules that match the pattern type (e.g. :change_content => :content_rule) and subject (e.g. change_or_pattern.subject.type => 'blobs' && rule.subject => 'blobs')



130
131
132
# File 'lib/duxml/meta/grammar.rb', line 130

def get_rule(change_or_pattern)
  rules.select do |rule| rule.applies_to?(change_or_pattern) end
end

#history ⇒ History

Returns history that this grammar is currently reporting to.

Returns:

  • (History) —

    history that this grammar is currently reporting to



59
60
61
# File 'lib/duxml/meta/grammar.rb', line 59

def history
  @observer_peers.first.first if @observer_peers and @observer_peers.any? and @observer_peers.first.any?
end

#inspect ⇒ String

Returns formatted to appear in tight spaces e.g. debugger.

Returns:

  • (String) —

    formatted to appear in tight spaces e.g. debugger



83
84
85
# File 'lib/duxml/meta/grammar.rb', line 83

def inspect
  "#<#{self.class.to_s} #{object_id}: @rules=#{rules.size}>"
end

#name ⇒ String

Returns 'grammar'.

Returns:



88
89
90
# File 'lib/duxml/meta/grammar.rb', line 88

def name
  'grammar'
end

#qualify(change_or_pattern) ⇒ Boolean

Returns false if any rule disqualifies; true if they all pass.

Parameters:

  • change_or_pattern (Duxml::Change, Duxml::Pattern) —

    applies applicable rule type and subject to given change_or_pattern and generates errors when disqualified

Returns:

  • (Boolean) —

    false if any rule disqualifies; true if they all pass



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/duxml/meta/grammar.rb', line 106

def qualify(change_or_pattern)
  return true unless self.defined?
  rules = get_rule(change_or_pattern)

  # define behaviors for when there are no rules applying to a given pattern
  if rules.empty?

    if change_or_pattern.respond_to?(:text) or
        change_or_pattern.respond_to?(:value) or
        change_or_pattern.subject.is_a?(Doc)
      return true
    end
    report(:ValidateError, change_or_pattern)
    return false
  end
  results = rules.collect do |rule|
    rule.qualify change_or_pattern
  end
  !results.any? do |qualified| !qualified end
end

#validate(node) ⇒ Object

Parameters:

  • node (Duxml::Object) —

    applies grammar rules to all relationships of the given object



94
95
96
97
98
99
100
101
# File 'lib/duxml/meta/grammar.rb', line 94

def validate(node)
  rels = get_relationships(node)
  results = rels.collect do |rel|
    qualify rel
  end
  any_disqualified = results.any? do |qualified| !qualified end
  !any_disqualified
end

#xml ⇒ Doc

Returns self as XML document

Returns:

  • (Doc) —

    returns self as XML document



54
55
56
# File 'lib/duxml/meta/grammar.rb', line 54

def xml
  Element.new('grammar') << rules.collect do |rule| rule.xml end
end