Class: Riddl::Utils::Properties::Backend
- Inherits:
-
Object
- Object
- Riddl::Utils::Properties::Backend
- Defined in:
- lib/ruby/riddl/utils/properties.rb
Overview
}}}
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
{{{.
-
#rng ⇒ Object
readonly
{{{.
-
#schema ⇒ Object
readonly
{{{.
Instance Method Summary collapse
- #activate_schema(name) ⇒ Object
- #init_state?(property, new) ⇒ Boolean
-
#initialize(schema, target, init = nil) ⇒ Backend
constructor
A new instance of Backend.
- #is_state?(property) ⇒ Boolean
- #modifiable?(property) ⇒ Boolean
- #modify(&block) ⇒ Object
- #property_type(property) ⇒ Object
- #valid_state?(property, current, new) ⇒ Boolean
Constructor Details
#initialize(schema, target, init = nil) ⇒ Backend
Returns a new instance of Backend.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ruby/riddl/utils/properties.rb', line 64 def initialize(schema,target,init=nil) @target = target.gsub(/^\/+/,'/') @schemas = {} @rngs = {} if schema.is_a? Hash schema.each { |k,v| add_schema k, v } elsif schema.is_a? String add_schema 'default', schema end raise "no schemas provided" if @schemas.length == 0 @schema = @schemas.first[1] @rng = @rngs.first[1] FileUtils::mkdir_p(File::dirname(@target)) unless File.exists?(@target) FileUtils::cp init, @target if init and not File.exists?(@target) raise "properties file not found" unless File.exists?(@target) @data = XML::Smart.open_unprotected(@target) @data.register_namespace 'p', 'http://riddl.org/ns/common-patterns/properties/1.0' @mutex = Mutex.new end |
Instance Attribute Details
#data ⇒ Object (readonly)
{{{
62 63 64 |
# File 'lib/ruby/riddl/utils/properties.rb', line 62 def data @data end |
#rng ⇒ Object (readonly)
{{{
62 63 64 |
# File 'lib/ruby/riddl/utils/properties.rb', line 62 def rng @rng end |
#schema ⇒ Object (readonly)
{{{
62 63 64 |
# File 'lib/ruby/riddl/utils/properties.rb', line 62 def schema @schema end |
Instance Method Details
#activate_schema(name) ⇒ Object
87 88 89 90 91 92 93 94 95 |
# File 'lib/ruby/riddl/utils/properties.rb', line 87 def activate_schema(name) if @schemas[name] @schema = @schemas[name] @rng = @rngs[name] true else false end end |
#init_state?(property, new) ⇒ Boolean
117 118 119 |
# File 'lib/ruby/riddl/utils/properties.rb', line 117 def init_state?(property,new) @schema.find("boolean(/p:properties/p:#{property}/p:#{new}[position()=1])") || schema.find("boolean(/p:properties/p:optional/p:#{property}/p:#{new}[position()=1])") end |
#is_state?(property) ⇒ Boolean
114 115 116 |
# File 'lib/ruby/riddl/utils/properties.rb', line 114 def is_state?(property) @schema.find("boolean(/p:properties/p:#{property}[@type='state'])") || schema.find("boolean(/p:properties/p:optional/p:#{property}[@type='state'])") end |
#modifiable?(property) ⇒ Boolean
108 109 110 |
# File 'lib/ruby/riddl/utils/properties.rb', line 108 def modifiable?(property) @schema.find("boolean(/p:properties/p:#{property}[@modifiable='true'])") || schema.find("boolean(/p:properties/p:optional/p:#{property}[@modifiable='true'])") end |
#modify(&block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ruby/riddl/utils/properties.rb', line 125 def modify(&block) tdoc = @data.root.to_doc tdoc.register_namespace 'p', 'http://riddl.org/ns/common-patterns/properties/1.0' @mutex.synchronize do block.call tdoc if tdoc.validate_against(@rng){|err| puts err. } block.call @data @data.save_as(@target) true else false end end end |
#property_type(property) ⇒ Object
120 121 122 123 |
# File 'lib/ruby/riddl/utils/properties.rb', line 120 def property_type(property) exis = @schema.find("/p:properties/*[name()='#{property}']|/p:properties/p:optional/*[name()='#{property}']") exis.any? ? exis.first.attributes['type'].to_sym : nil end |
#valid_state?(property, current, new) ⇒ Boolean
111 112 113 |
# File 'lib/ruby/riddl/utils/properties.rb', line 111 def valid_state?(property,current,new) @schema.find("boolean(/p:properties/p:#{property}/p:#{current}/p:#{new}[@putable='true'])") || schema.find("boolean(/p:properties/p:optional/p:#{property}/p:#{current}/p:#{new}[@putable='true'])") end |