Module: Highland::Environment

Extended by:
Environment
Included in:
Highland, Environment
Defined in:
lib/highland/environment.rb

Overview

Environment is the initial set of methods, which gets loaded after you include highland in your file. It depends on the set of static files inside highland_db folder. If you write “require ‘highland’” in your file and noothing works then it means that you didn’t initialize your database in the folder of your application ($ highland init).

Constant Summary collapse

@@klasses =
[]
@@stringklasses =
[]
@@db_path =
""

Instance Method Summary collapse

Instance Method Details

#build(file) ⇒ Object

Build is an alternative for load method. Allows you to redefine class variable. It’s not recommended to use this method.



32
33
34
35
# File 'lib/highland/environment.rb', line 32

def build(file)
  @@db_path = find_root(expander(file)) if defined?(DB_PATH) == nil
  load_collections
end

#create_missing(collections, static) ⇒ Object

Creates all the missing static hashes, which are mentioned in manifesto.



67
68
69
70
71
# File 'lib/highland/environment.rb', line 67

def create_missing(collections, static)
  collections.each do |c|
    File.open(@@db_path + "db/#{c.downcase}.hl", "w") if static.include?(c.downcase) == false
  end
end

#define_classes(collections) ⇒ Object

Creates classes for relevant collections and provides them with API.



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/highland/environment.rb', line 83

def define_classes(collections)      
  collections.each do |collection|
    if @@klasses.include?(collection) == false
      new_class = Object.const_set(collection, Class.new)
      new_class.extend Highland::CollectionMethods
      new_class.extend Highland::DatabaseMethods
      @@stringklasses << new_class
      @@klasses << collection
    end
  end
  return @@stringklasses           
end

#delete_unused(collections, static) ⇒ Object

Deletes all static collections, which are not mentioned in the manifesto.



75
76
77
78
79
# File 'lib/highland/environment.rb', line 75

def delete_unused(collections, static)
  static.each do |c|
    File.delete(@@db_path + "db/#{c.downcase}.hl") if collections.map(&:downcase).include?(c.downcase) == false
  end      
end

#expander(file) ⇒ Object

Expands the path of input file.



123
124
125
# File 'lib/highland/environment.rb', line 123

def expander(file)
  return File.expand_path(File.dirname(file))
end

#find_root(path) ⇒ Object

Recursive method, which tries to find the application root.



129
130
131
132
133
# File 'lib/highland/environment.rb', line 129

def find_root(path)
  path = File.expand_path('..', path) if root?(path) == false
  find_root(path) if root?(path) == false
  return path + "/highland_db/" if root?(path) == true
end

#get_collectionsObject

Gets names of collections from manifesto.yml.



53
54
55
# File 'lib/highland/environment.rb', line 53

def get_collections
  return YAML.load_file(@@db_path + "manifesto.yml")["collections"]
end

#get_staticObject

Gets all static hashes (collections) from the database folder.



59
60
61
62
63
# File 'lib/highland/environment.rb', line 59

def get_static
  return Dir[@@db_path + "db/*.hl"].map do |file|
    File.basename(file,".hl").downcase
  end
end

#loadObject

Load initializes the class variable @@db_path and loads collections. You shouldn’t call this method. It’s done automatically. If you want to specify the path to your database - write custom path in DB_PATH before “require ‘highland’”.



23
24
25
26
27
# File 'lib/highland/environment.rb', line 23

def load
  @@db_path = find_root(Dir::pwd) if defined?(DB_PATH) == nil
  @@db_path = DB_PATH if defined?(DB_PATH) == "constant"
  load_collections
end

#load_collectionsObject

Gets the list of static collections, compares it with the manifesto.yml. If manifesto.yml doesn’t include some static collections - this collections get deleted. If manifesto.yml has collections which do not exist then these collections are automatically created. After this - virtual hashes are loaded.



42
43
44
45
46
47
48
49
# File 'lib/highland/environment.rb', line 42

def load_collections   
  collections = get_collections
  static = get_static
  create_missing(collections, static)
  delete_unused(collections, static)
  classes = define_classes(collections)
  load_virtuals(classes)
end

#load_virtuals(classes) ⇒ Object

Loads virtual hash and virtual helper for each collection.



98
99
100
101
102
103
# File 'lib/highland/environment.rb', line 98

def load_virtuals(classes)
  classes.each do |klass|
    target = klass.to_s.downcase
    klass.init_collection(@@db_path + "db/#{target}.hl")
  end
end

#root?(path) ⇒ Boolean

Returns true if the input path is the application root.

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/highland/environment.rb', line 107

def root?(path)
  root_objects = ["gemfile", "procfile", "readme", "root"]  
  current_objects = Dir[path + "/*"].map do |file|
    File.basename(file).downcase
  end
  dir = ""
  current_objects.each do |co| 
     dir = (root_objects.include?(co) == true)? "ROOT" : "NOT ROOT"
     break if dir == "ROOT"
  end
  return true if dir == "ROOT"
  return false if dir == "NOT ROOT"  
end