Class: CSL::Schema

Inherits:
Object show all
Defined in:
lib/csl/schema.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.attributesObject

Returns the value of attribute attributes.



141
142
143
# File 'lib/csl/schema.rb', line 141

def attributes
  @attributes
end

.categoriesObject

Returns the value of attribute categories.



141
142
143
# File 'lib/csl/schema.rb', line 141

def categories
  @categories
end

.default_licenseObject

Returns the value of attribute default_license.



141
142
143
# File 'lib/csl/schema.rb', line 141

def default_license
  @default_license
end

.default_rights_stringObject

Returns the value of attribute default_rights_string.



141
142
143
# File 'lib/csl/schema.rb', line 141

def default_rights_string
  @default_rights_string
end

.major_versionObject

Returns the value of attribute major_version.



141
142
143
# File 'lib/csl/schema.rb', line 141

def major_version
  @major_version
end

.namespaceObject

Returns the value of attribute namespace.



141
142
143
# File 'lib/csl/schema.rb', line 141

def namespace
  @namespace
end

.preambleObject

Returns the value of attribute preamble.



141
142
143
# File 'lib/csl/schema.rb', line 141

def preamble
  @preamble
end

.typesObject

Returns the value of attribute types.



141
142
143
# File 'lib/csl/schema.rb', line 141

def types
  @types
end

.valuesObject

Returns the value of attribute values.



141
142
143
# File 'lib/csl/schema.rb', line 141

def values
  @values
end

.variablesObject

Returns the value of attribute variables.



141
142
143
# File 'lib/csl/schema.rb', line 141

def variables
  @variables
end

.versionObject

Returns the value of attribute version.



141
142
143
# File 'lib/csl/schema.rb', line 141

def version
  @version
end

Class Method Details

.attr(*arguments) ⇒ Object



147
148
149
# File 'lib/csl/schema.rb', line 147

def attr(*arguments)
  attributes.values_at(*arguments).flatten(1)
end

.valid?(style) ⇒ Boolean

Whether or not the passed-in style (or list of styles) is valid.

Parameters:

  • style (Style, String, IO, Array)

    the style (or a list of styles) to validate.

Returns:

  • (Boolean)

    whether or not the passed-in style (or styles) is valid.

Raises:

  • (ArgumentError)

    if the passed-in argument is not a Style or a valid style location.

  • (ValidationError)

    if the validation process fails

See Also:



215
216
217
# File 'lib/csl/schema.rb', line 215

def valid?(style)
  validate(style).empty?
end

.validate(node) ⇒ <<Fixnum,String>>

Validates the passed-in style or list of styles. The style argument(s) can either be a CSL::Style object, a style’s file handle, XML content or a valid location (wildcards are supported). The method returns a list of validation errors; the passed-in style is valid if the method returns an empty list.

Examples:

CSL::Schema.validate(CSL::Style.load(:apa))

CSL::Schema.validate('my-styles/style.csl')
CSL::Schema.validate('my-styles/*.csl')
CSL::Schema.validate('http://www.zotero.org/styles/vancouver')

Parameters:

  • style (Node, String, IO, Array)

    the style (or a list of styles) to validate.

Returns:

  • (<<Fixnum,String>>)

    a list of validation errors

Raises:

  • (ArgumentError)

    if the passed-in argument is not a Style or a valid style location.

  • (ValidationError)

    if the validation process fails



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/csl/schema.rb', line 172

def validate(node)
  case
  when node.is_a?(Node)
    validator[schema, node.to_xml]
  when node.respond_to?(:read)
    validator[schema, node.read]
  when node.is_a?(Enumerable) && !node.is_a?(String)
    node.map { |n| validate(n) }.flatten(1)
  when node.respond_to?(:to_s)
    node = node.to_s

    case
    when node =~ /^\s*</
      validator[schema, node]
    when File.exists?(node)
      validator[schema, File.open(node, 'r:UTF-8')]
    else
      glob = Dir.glob(node)
      
      if glob.empty?
        validator[schema, Kernel.open(node)]            
      else
        glob.map { |n| validator[schema, File.open(n, 'r:UTF-8')] }.flatten(1)
      end
    end
  else
    raise ArgumentError, "failed to validate #{node.inspect}: not a CSL node"
  end
end