Class: Gemmy

Inherits:
Object
  • Object
show all
Defined in:
lib/gemmy.rb,
lib/gemmy/version.rb

Overview

Container class for all the functionality.

Direct Known Subclasses

G

Defined Under Namespace

Modules: Components, Constants, Patches, Tasks, Tests Classes: CLI

Constant Summary collapse

Coffee =
<<-COFFEE

#!/usr/bin/env coffee
puts = console.log

# Gemmy.init reads from argv and starts command
Gemmy =
  validCommands: (key) ->
    {
      'pos': this.partOfSpeech
    }[key]

  init: ->
    [cmd, args...] = process.argv.slice -2
    component = this.validCommands(cmd)
    unless component
      throw('missing or invalid argv command')
    component.run(args...).then component.callback

# Part of speech detector
pos = Gemmy.partOfSpeech = {}
pos.run = (sentence) ->
  WordPos = require 'wordpos'
  wordpos = new WordPos(profile: true)
  new Promise (resolve, reject) ->
    wordpos.getPOS sentence, resolve
pos.callback = (result) ->
  puts JSON.stringify result

Gemmy.init()

COFFEE
VERSION =
'0.1.6'

Class Method Summary collapse

Class Method Details

.component(path) ⇒ Object

Slightly friendlier way to reference component classes Gemmy.component(“word_speaker/sentence”)



18
19
20
21
22
# File 'lib/gemmy.rb', line 18

def self.component(path)
  Gemmy::Components.const_get(
    path.split("/").map { |part| part.camelcase }.join("::")
  )
end

.const(const_name_abbrev) ⇒ Object

Get a constant, lookup on Gemmy::Constants it gsubs ‘/’ to ‘::’ and if the given constant names are lowercase, then it camelcases them.



61
62
63
64
65
66
# File 'lib/gemmy.rb', line 61

def self.const(const_name_abbrev)
  const_name = const_name_abbrev.split("/").map do |x|
    x.chars.all? { |char| char.in? 'a'.upto('z') } ? x.camelcase : x
  end.join("::")
  Gemmy::Constants.const_get const_name
end

.load_globallyObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/gemmy.rb', line 68

def self.load_globally
  core_patches = Patches.core_patches.map do |core_klass_name, patch_klass|
    core_klass = core_klass_name.to_s.constantize
    instance_method_class = patch_klass.const_get("InstanceMethods")
    class_method_class = patch_klass.const_get("ClassMethods")
    instance_classes = instance_method_class.constants.map do |klass_sym|
      klass = instance_method_class.const_get klass_sym
      core_klass.include klass
      klass
    end
    class_classes = class_method_class.constants.map do |klass_sym|
      klass = class_method_class.const_get klass_sym
      core_klass.extend klass
      klass
    end
    [instance_classes, class_classes]
  end
  components = Components.list.map do |patch_klass|
    Object.include patch_klass
    Object.extend patch_klass
    patch_klass
  end
  [components, core_patches].flatten
end

.patch(string) ⇒ Object

Used by patches to get a reference to static patch classes Without this there would be long, unqualified constant names such as Gemmy::Patches::SymbolPatch::InstanceMethods::Call

Usage is to pass a string like so: “<CoreClass>/<Context>/<MethodName>” Core class could be “symbol” for example Context is either “i” for instance or “c” for class Method name is underscored, i.e. “call” in this example:

klass = Gemmy.patch “symbol/i/call”

Now I can call any class methods on the klass.

The utility of this might not be obvious, but it is useful when using another library’s refinements in Gemmy’s own

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
46
47
# File 'lib/gemmy.rb', line 39

def self.patch(string)
  parts = string.split("/")
  raise ArgumentError unless parts.length == 3
  core_class, context, method_name = parts
  context_classname = context.eql?("i") ? "InstanceMethods" : "ClassMethods"
  Gemmy::Patches.const_get("#{core_class.capitalize}Patch")
                .const_get(context_classname)
                .const_get method_name.camelcase
end

.patches(*args) ⇒ Object



51
52
53
# File 'lib/gemmy.rb', line 51

def self.patches(*args)
  Gemmy::Patches.class_refinements *args
end