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 <grammar> to activate its methods when that XML Element is encountered in a Meta file
Constant Summary
Constants included from Duxml
Constants included from Meta
Instance Attribute Summary
Attributes included from Duxml
Attributes included from Saxer
Class Method Summary collapse
-
.import(path) ⇒ GrammarClass, Element
XML Element named <grammar> and GrammarClass object are functionally equivalent.
-
.xml ⇒ Element
Returns self as XML element e.g.
Instance Method Summary collapse
- #add_observer(obj, sym = nil) ⇒ Object
-
#defined? ⇒ Boolean
Whether or not any rules have been defined yet in this grammar.
-
#description ⇒ String
Lists XML schema and content rules in order of precedence.
-
#get_rule(change_or_pattern) ⇒ 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’).
-
#history ⇒ History
History that this grammar is currently reporting to.
-
#inspect ⇒ String
Formatted to appear in tight spaces e.g.
-
#name ⇒ String
‘grammar’.
-
#qualify(change_or_pattern) ⇒ Boolean
False if any rule disqualifies; true if they all pass.
- #validate(node) ⇒ Object
Methods included from RelaxNG
Methods included from Duxml
#create, #load, #log, #relaxng, #save
Methods included from Meta
Methods included from Saxer
Methods included from LazyOx
Methods included from PatternMaker
#get_child_patterns, #get_existing_attr_patterns, #get_null_attr_patterns, #get_null_child_patterns, #get_relationships
Methods included from Spreadsheet
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 <grammar> 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
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.
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.
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’).
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.
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.
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’.
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.
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
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 |