class Orchestrate::Rails::Schema

Extensions to parent class for mapping data between orchestrate and rails.

Public Class Methods

define_collection(args) click to toggle source

args: { :name, :properties, :event_types, :graphs, :classname }

# File rails/lib/orchestrate_rails/schema.rb, line 73
def self.define_collection(args)
  # puts "DEF_COLL: '#{args.inspect}'"
  coll = instance.load_collection SchemaCollection.new(args) { |c|
    c.classname = args[:classname]
    c.classpath = args[:classpath] #unless args[:classpath].blank?
    c.attr_map = build_attr_map args[:properties]
  }
  # puts "DEF_COLL: '#{coll.classpath}'"
end
define_event_type(args) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 83
def self.define_event_type(args)
  # puts "DEFINE: inst = '#{instance}', '#{args[:collection]}'"  #JMC
  instance.define_event_type(
    args[:collection], args[:event_type], args[:properties]
  )
end
define_graph(args) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 90
def self.define_graph(args)
  instance.define_graph(
    args[:collection], args[:relation_kind], args[:to_collection]
  )
end
load(schema) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 67
def self.load(schema)
  require Rails.root.join schema
end

Private Class Methods

build_attr_map(attrs) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 97
def self.build_attr_map(attrs)
  attr_map = {}
  attrs.map { |a| attr_map.merge!(a.to_orchio_rails_attr => a.to_s) }.last
end

Public Instance Methods

attr_map(collection_name) click to toggle source

Returns the table that maps attribute names to property names.

Property names are the original key names in json documents stored in orchestrate collections - before they are converted to the snake_case style used for model attribute names. Property names can be mapped to attribute names by using String#to_orchio_rails_attr or Symbol#to_orchio_rails_attr.

# File rails/lib/orchestrate_rails/schema.rb, line 29
def attr_map(collection_name)
  get_collection(collection_name).attr_map
end
attrs(collection_name) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 18
def attrs(collection_name)
  get_collection(collection_name).attr_map.keys
end
classname(collection_name) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 40
def classname(collection_name)
  get_collection(collection_name).classname
end
fullclassname(collection_name) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 44
def fullclassname(collection_name)
  c = get_collection(collection_name)
  c.classpath.nil? ? c.classname : "#{c.classpath}::#{c.classname}"
end
get_collection_from_class(classname) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 49
def get_collection_from_class(classname)
  classname = classname.split('::').last
  collections.values.each { |c| return c if c.classname == classname }
end
properties(collection_name) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 14
def properties(collection_name)
  get_collection(collection_name).properties
end
qmap(collection_name) click to toggle source

Returns a table that maps attribute names to property names. And the mapping conveniently works even when the attribute name is actually a property name.

# File rails/lib/orchestrate_rails/schema.rb, line 36
def qmap(collection_name)
  get_collection(collection_name).qmap ||= build_query_map(collection_name)
end

Private Instance Methods

build_query_map(collection) click to toggle source
# File rails/lib/orchestrate_rails/schema.rb, line 54
def build_query_map(collection)
  q_map = {}
  attr_map(collection).values.map { |a|
    q_map.merge!(a => a, a.to_orchio_rails_attr => a)
  }.last
end