class Microdata::Schema::Thing

Constants

ATTRIBUTES

Public Class Methods

new(*args) click to toggle source
Calls superclass method Microdata::Schema::new
# File lib/microdata/schema/thing.rb, line 8
def initialize(*args)
  hash = args.first
  return super if hash.nil?
  raise "args needs Hash" if hash.class != Hash
  super
  hash.each do |key, value|
    raise "key not found (#{key})" unless attributes.include?(key)
    value.top_level = false if value.is_a? Thing
    instance_variable_set("@#{key}", value)
    @assined_attributes << key
  end
end

Public Instance Methods

as_json(options = nil) click to toggle source
# File lib/microdata/schema/thing.rb, line 21
def as_json(options = nil)
  hash = {}
  assined_attributes.each do |attribute|
    camelCase = to_camel_case(attribute.to_s)
    camelCase = "@id" if camelCase == "id"
    hash[camelCase] = instance_variable_get("@#{attribute}")
  end
  hash = @required_attributes.merge(hash)
  if @top_level
    @required_attributes_for_top_level_things.merge!(hash)
    @required_attributes_for_top_level_things
  else
    hash
  end
end
to_json(options = nil) click to toggle source
# File lib/microdata/schema/thing.rb, line 37
def to_json(options = nil)
  as_json(options).to_json
end
to_json_ld(options = nil) click to toggle source
# File lib/microdata/schema/thing.rb, line 41
def to_json_ld(options = nil)
  "<script type=\"application/ld+json\">" + to_json(options) + "</script>"
end

Private Instance Methods

to_camel_case(string) click to toggle source
# File lib/microdata/schema/thing.rb, line 47
def to_camel_case(string)
  _words = []
  words = string.split("_")
  _words << words.shift
  words.each.with_index do |word|
    word[0] = word[0].upcase
    _words << word
  end
  _words.join
end