Class: AimsProject::GeometryFile
- Inherits:
-
Object
- Object
- AimsProject::GeometryFile
- Includes:
- Aims, Observable
- Defined in:
- lib/aims_project/geometry_file.rb
Overview
The geometry model utilized by this application
Instance Attribute Summary collapse
-
#aims_geometry ⇒ Object
readonly
The Aims::Geometry object.
-
#file ⇒ Object
readonly
The filename of the geometry file.
-
#input_geometry ⇒ Object
readonly
The string representation of the parsed input geometry.
-
#raw_input ⇒ Object
The raw unevaluated input.
Class Method Summary collapse
-
.eval_geometry(str, _binding = nil) ⇒ Object
Read and parse a geometry file.
Instance Method Summary collapse
-
#evaluate(_binding = nil) ⇒ Object
Evaluate the raw input and return a geometry String formatted in the Aims geometry.in format.
-
#initialize(input = nil, _binding = nil) ⇒ GeometryFile
constructor
Parse a geometry input Evaluate any embedded ruby using the given binding.
-
#is_valid? ⇒ Boolean
Check the consistency between the raw, the evaluated and the object-space geometries.
-
#method_missing(symbol, *args, &block) ⇒ Object
Delegate calls to the Aims::Geometry object if it exists.
-
#save ⇒ Object
Save this geometry raises an InvalidFilenameException unless @file is not nil.
-
#save_as(file) ⇒ Object
Save the raw input to file if it passes validation raises an InvalidFilenameException unless @file is not nil raises a GeometryValidationException if it fails to pass Validation.
-
#validate ⇒ Object
Check the consistency of all internal data models.
Constructor Details
#initialize(input = nil, _binding = nil) ⇒ GeometryFile
Parse a geometry input Evaluate any embedded ruby using the given binding
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/aims_project/geometry_file.rb', line 46 def initialize(input = nil, _binding=nil) if input.nil? @raw_input = "" @input_geometry = "" return end if input.respond_to? :read @file = input @raw_input = @file.read elsif input.is_a? String @raw_input = input elsif input.is_a? Aims::Geometry @aims_geometry = input @input_geometry = @aims_geometry.format_geometry_in @raw_input = @input_geometry end begin # Attempt to evaluate the raw input, but don't require it. @binding = _binding evaluate(@binding) rescue end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
Delegate calls to the Aims::Geometry object if it exists.
98 99 100 101 102 103 104 |
# File 'lib/aims_project/geometry_file.rb', line 98 def method_missing(symbol, *args, &block) if @aims_geometry.nil? raise GeometryEvaluationException.new else @aims_geometry.send(symbol, *args, &block) end end |
Instance Attribute Details
#aims_geometry ⇒ Object (readonly)
The Aims::Geometry object
33 34 35 |
# File 'lib/aims_project/geometry_file.rb', line 33 def aims_geometry @aims_geometry end |
#file ⇒ Object (readonly)
The filename of the geometry file
30 31 32 |
# File 'lib/aims_project/geometry_file.rb', line 30 def file @file end |
#input_geometry ⇒ Object (readonly)
The string representation of the parsed input geometry
36 37 38 |
# File 'lib/aims_project/geometry_file.rb', line 36 def input_geometry @input_geometry end |
#raw_input ⇒ Object
The raw unevaluated input.
39 40 41 |
# File 'lib/aims_project/geometry_file.rb', line 39 def raw_input @raw_input end |
Class Method Details
.eval_geometry(str, _binding = nil) ⇒ Object
Read and parse a geometry file. Use the given binding to evaluate any embedded ruby
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/aims_project/geometry_file.rb', line 16 def GeometryFile.eval_geometry(str, _binding=nil) b = if _binding _binding else binding() end erb = ERB.new(str) erb.result(b) end |
Instance Method Details
#evaluate(_binding = nil) ⇒ Object
Evaluate the raw input and return a geometry String formatted in the Aims geometry.in format
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/aims_project/geometry_file.rb', line 82 def evaluate(_binding=nil) if _binding @binding = _binding end @input_geometry = GeometryFile.eval_geometry(@raw_input, @binding) @aims_geometry = GeometryParser.parse_string(@input_geometry) changed notify_observers @aims_geometry end |
#is_valid? ⇒ Boolean
Check the consistency between the raw, the evaluated and the object-space geometries
107 108 109 110 111 112 113 114 |
# File 'lib/aims_project/geometry_file.rb', line 107 def is_valid? begin validate return true rescue Exception => e return false end end |
#save ⇒ Object
Save this geometry raises an InvalidFilenameException unless @file is not nil
154 155 156 |
# File 'lib/aims_project/geometry_file.rb', line 154 def save save_as(@file) end |
#save_as(file) ⇒ Object
Save the raw input to file if it passes validation raises an InvalidFilenameException unless @file is not nil raises a GeometryValidationException if it fails to pass Validation
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/aims_project/geometry_file.rb', line 163 def save_as(file) if file.nil? raise InvalidFilenameException(nil) unless @file file = @file end # validate File.open(file, 'w') {|f| f.write @raw_input } return self end |
#validate ⇒ Object
Check the consistency of all internal data models
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/aims_project/geometry_file.rb', line 118 def validate # The raw input evaluated must match the input_geometry erb = ERB.new(@raw_input) res = erb.result(@binding) unless res == @input_geometry raise GeometryValidationException.new("raw input doesn't match evaluated input") end # Also need to somehow validate against the Aims::Geometry g = GeometryParser.parse_string(res) unless g.atoms.size == @aims_geometry.atoms.size raise GeometryValidationException("input geometry atom count doesn't match aims_geometry") end unless g.lattice_vectors.size == @aims_geometry.lattice_vectors.size raise GeometryValidationException("input geometry lattice vector count doesn't match aims_geometry") end g.atoms.each{|a| unless @aims_geometry.atoms.member?(a) raise GeometryValidationException("atom mismatch") end } g.lattice_vectors.each{|v| unless @aims_geometry.lattice_vectors.member?(v) raise GeometryValidationException("lattice vector mismatch") end } return true end |