Class: CommonCoreParser::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/common_core_parser/element.rb

Direct Known Subclasses

Cluster, Component, Domain, Standard, StandardType, SubjectGrade

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xmldoc) ⇒ Element

Returns a new instance of Element.

Raises:

  • (ArgumentError)


7
8
9
10
11
# File 'lib/common_core_parser/element.rb', line 7

def initialize(xmldoc)
  raise(ArgumentError, "Not a LearningStandardItem\n #{xmldoc}") unless xmldoc.name == 'LearningStandardItem'
  @data = xmldoc
  @children_hash = {}
end

Instance Attribute Details

#children_hashObject (readonly)

Returns the value of attribute children_hash.



5
6
7
# File 'lib/common_core_parser/element.rb', line 5

def children_hash
  @children_hash
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/common_core_parser/element.rb', line 4

def data
  @data
end

Instance Method Details

#add_child(element) ⇒ Object



45
46
47
# File 'lib/common_core_parser/element.rb', line 45

def add_child(element)
  @children_hash[element.ref_id] = element
end

#childrenObject



25
26
27
# File 'lib/common_core_parser/element.rb', line 25

def children
  children_hash.values
end

#codeObject



49
50
51
# File 'lib/common_core_parser/element.rb', line 49

def code
  @code ||= data.xpath('./StatementCodes/StatementCode').first.text.strip
end

#error_messageObject



73
74
75
# File 'lib/common_core_parser/element.rb', line 73

def error_message
  errors.join(',')
end

#errorsObject

Raises:

  • (RuntimeError)


69
70
71
# File 'lib/common_core_parser/element.rb', line 69

def errors
  raise RuntimeError
end

#gradesObject



57
58
59
# File 'lib/common_core_parser/element.rb', line 57

def grades
  @grades ||= data.xpath('./GradeLevels/GradeLevel').map(&:text).map {|string| convert_grade_string_to_grades(string) }.flatten
end

#illigit?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/common_core_parser/element.rb', line 77

def illigit?
  return false unless ref_id.blank?
  case
    when (ref_id.blank? and statement.match(/not applicable/)) then true
    when (ref_id.blank? and statement.match(/begins in grade [0-9]+/)) then true
    else false
  end
end

#parentObject



21
22
23
# File 'lib/common_core_parser/element.rb', line 21

def parent
  @parent ||= Master.instance.elements_hash[parent_ref_id]
end

#parent_ref_idObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/common_core_parser/element.rb', line 29

def parent_ref_id
  return @parent_ref_id if @parent_ref_id

  #new file format
  data.xpath('./RelatedLearningStandardItems/LearningStandardItemRefId').each do |lsiri|
    return @parent_ref_id = BrokenDataCorrector.patch_duplicated_parent_ref_ids(self,lsiri.text.strip) if lsiri.attributes['RelationshipType'].value == 'childOf'
  end

  #old file format
  data.xpath('./PredecessorItems/LearningStandardItemRefId').each do |lsiri|
    return @parent_ref_id = BrokenDataCorrector.patch_duplicated_parent_ref_ids(self,lsiri.text.strip)
  end

  return nil
end

#ref_idObject



17
18
19
# File 'lib/common_core_parser/element.rb', line 17

def ref_id
  @ref_id ||= BrokenDataCorrector.patch_duplicated_ref_ids(self,(data.attributes['RefID'] || data.attributes['RefId']).value.strip)
end

#statementObject



53
54
55
# File 'lib/common_core_parser/element.rb', line 53

def statement
  @statement ||= data.xpath('./Statements/Statement').first.text.strip
end

#to_sObject



13
14
15
# File 'lib/common_core_parser/element.rb', line 13

def to_s
  "<#{self.class} ref_id: #{ref_id}, parent_ref_id: #{parent_ref_id}, code: #{code}, statement: #{statement}, grades: #{grades.join(',')}>"
end

#valid?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/common_core_parser/element.rb', line 65

def valid?
  errors.blank?
end

#valid_grades?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/common_core_parser/element.rb', line 61

def valid_grades?
  (grades & %w(K HS K-12 01 02 03 04 05 06 07 08 09 10 11 12)) == grades
end