Class: HmxClient::Command::Fn

Inherits:
Base
  • Object
show all
Defined in:
lib/hmx/command/fn.rb

Overview

Load and Save functions in hmx

Constant Summary

Constants inherited from Base

Base::FILE

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

#hmx, #initialize, #loadConfig!, namespace, #removeConfig, #storeConfig, #writeConfig

Methods included from Helpers

#display, #display_row, #display_tab, #display_table, #error, #getFromUser, #longest

Constructor Details

This class inherits a constructor from HmxClient::Command::Base

Instance Method Details

#exportObject

fn:export

Exports the functions to a series of files (and perhaps) folders, grounded in the folder specified by the first parameter. The file extension of the function is determined by the language of the function



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hmx/command/fn.rb', line 48

def export
  unless args.size > 0
     raise CommandFailed, "Usage: hmx fn:export <folder>"
  end
  rootFolder = args.shift
  Dir.mkdir(rootFolder) if !Dir.exist?(rootFolder)
  displayNames = hmx.doQuery(['sys.fn', nil])
  displayNames.each { | displayName |
                          # Just get function
                          fnInfo = hmx.doGetData([displayName])
                          fnName = fnInfo['document']['MXFunction']['fnName']
                          fnLang = fnInfo['document']['MXFunction']['fnLang']
                          fnContent = fnInfo['document']['MXFunction']['content']
                          outFile = fileName(rootFolder, fnName, fnLang)
                          puts "Writing #{outFile}" 
                          File.open(outFile, 'w') { | f | f.write(fnContent) }
                    }
end

#importObject

fn:import The opposite of export, takes files (and folders) in the root folder given and constructs and updates the functions back into hmx. The extension of the file determines the language of the function.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hmx/command/fn.rb', line 72

def import
  unless args.size > 0
     raise CommandFailed, "Usage: hmx fn:import <folder>"
  end
  rootFolder = args.shift
  Dir.foreach(rootFolder) { | f |
                        display "Loading #{ f } "
                        fullFile = File.expand_path("#{rootFolder}/#{f}")
                        if (File.file?(fullFile)) 
                           content = File.open(fullFile) { | h |
                                 c = ''
                                 while(line = h.gets)
                                       c = c + line
                                 end
                                 c
                           }
                           # Function name is the base of the filename
                           # Function language is based on the extension
                           # content is the content
                           ext = File.extname(f)
                           language = case File.extname(f) 
                               when '.rb' then 'RUBY'
                               when '.clj' then 'CLOJURE'
                               when '.js' then 'JAVASCRIPT'
                               else 'UNKNOWN'
                           end
 
                           document = {}
                           document['document'] = {}
                           document['displayName'] = "sys.fn/#{ File.basename(f, ext) }"
                           fn = {}
                           fn['fnName'] = File.basename(f, ext)
                           fn['fnLang'] = language
                           fn['content'] = content 
                           document['document']['MXFunction'] = fn                                  
                           hmx.doPutData([JSON.generate(document)]) 
                        end
                   }
end

#indexObject

fn

Manipulate HMX functions



14
15
16
17
18
19
# File 'lib/hmx/command/fn.rb', line 14

def index
  unless args.size > 0
    raise CommandFailed, "Usage: hmx fn <fnName>" 
  end
  dout hmx.doGetFn([args.shift])
end

#listObject

fn:list

List all functions in HMX



25
26
27
28
29
30
# File 'lib/hmx/command/fn.rb', line 25

def list
   fns = hmx.doQuery(["sys.fn", nil])
   fns.each { | f |
                display f.rpartition('/')[2]
            }
end

#putObject

fn:put

Create or update a function



36
37
38
39
40
41
# File 'lib/hmx/command/fn.rb', line 36

def put
   unless args.size > 0
     raise CommandFailed, "Usage: hmx fn:put <fnName> <f:function>" 
   end
   dout hmx.doPutFn([args])
end