Class: Ona

Inherits:
Object
  • Object
show all
Defined in:
lib/ona.rb

Class Method Summary collapse

Class Method Details

._create_to_s_method(resource) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ona.rb', line 18

def self._create_to_s_method(resource)
  attribute_sizes = resource[:attributes].map do |attribute|
    attribute.to_s.size
  end
  resource[:class].send(:define_method, :to_s) do
    max_attribute_length = attribute_sizes.sort.last + 4
    resource[:attributes].sort.map do |attribute|
      key = attribute.to_s.rjust(max_attribute_length, ' ')
      "#{key.to_ansi.green.to_s} : #{send(attribute)}"
    end * "\n"
  end
end

._generic_action(hash, &block) ⇒ Object



47
48
49
50
51
# File 'lib/ona.rb', line 47

def self._generic_action(hash, &block)
  hash[:block] = block
  @resources[hash[:resource]][:handlers].push(hash)
  nil
end

._get_max_example_lengthObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ona.rb', line 97

def self._get_max_example_length
  max = 0
  @resources.each do |name, resource|
    resource[:handlers].each do |handler|
      if max < handler[:example].size
        max = handler[:example].size
      end
    end
  end
  max
end

._get_max_name_lengthObject



108
109
110
111
112
113
114
115
116
# File 'lib/ona.rb', line 108

def self._get_max_name_length
  max = 0
  @resources.each do |name, resource|
    if max < name.size
      max = name.size
    end
  end
  max
end

._get_max_regex_lengthObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ona.rb', line 86

def self._get_max_regex_length
  max = 0
  @resources.each do |name, resource|
    resource[:handlers].each do |handler|
      if max < handler[:regex].inspect.size
        max = handler[:regex].inspect.size
      end
    end
  end
  max
end

._single_action(hash, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/ona.rb', line 52

def self._single_action(hash, &block)
  Ona._generic_action(hash) do |resources, command, regex|
    wanted_id = hash[:token].call(command.scan(regex))
    wanted_id = wanted_id.to_i
    resources.each_with_index do |array, id|
      next if id != wanted_id
      block.call(array, id)
    end
  end
end

.action(hash, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ona.rb', line 36

def self.action(hash, &block)
  unless hash[:find]
    _generic_action(hash, &block)
    return
  end
  unless hash[:token]
    $stderr.puts "No token for: #{hash.inspect}"
    exit 1
  end
  _single_action(hash, &block)
end

.confirm(text, expected) ⇒ Object



154
155
156
157
158
159
160
161
162
# File 'lib/ona.rb', line 154

def self.confirm(text, expected)
  puts ''
  puts "# " + " #{text} ".to_ansi.white.red_background.to_s
  puts "# " + '=' * 78
  puts "Type [#{expected.to_ansi.green.to_s}] to continue. or anything else to skip."
  print 'What to do? >'
  input = gets.chomp
  input == expected
end

.defaultsObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ona.rb', line 132

def self.defaults
  Ona.resource(:any, [:empty])

  Ona.action(
    :regex    => /(^)(quit|exit)($)/,
    :resource => :any,
    :text     => 'Exit this program.',
    :example  => 'quit'
  ) do |resource, command, regex|
    puts 'bye.'
    exit 0
  end

  Ona.action(
    :regex    => /^help$/,
    :resource => :any,
    :text     => 'Print help.',
    :example  => 'help'
  ) do |resource, command, regex|
    Ona.help
  end
end

.helpObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ona.rb', line 117

def self.help
  @resources.each do |name, resource|
    pretty_name = "[#{name}]".to_s.rjust(_get_max_name_length + 2, ' ')
    pretty_name = pretty_name.to_ansi.cyan.to_s
  resource[:handlers].sort do |a, b|
    a[:example] <=> b[:example]
  end.each do |handler|
      pretty_regex = handler[:regex].inspect.ljust(_get_max_regex_length, ' ')
      pretty_regex = pretty_regex.to_s.to_ansi.pink.to_s
      pretty_example = handler[:example].ljust(_get_max_example_length, ' ')
      pretty_example = pretty_example.to_s.to_ansi.yellow.to_s
      puts "#{pretty_name} #{pretty_example} #{handler[:text]}"
    end
  end
end

.main(use_defaults = true) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/ona.rb', line 78

def self.main(use_defaults = true)
  defaults if use_defaults
  _prompt = (prompt || 'Ona').to_ansi.cyan.to_s + '> '
  while input = Readline.readline(_prompt, true)
 Ona.run(input)
  end
  puts ''
end

.promptObject



75
76
77
# File 'lib/ona.rb', line 75

def self.prompt
  @prompt
end

.prompt=(string) ⇒ Object



72
73
74
# File 'lib/ona.rb', line 72

def self.prompt=(string)
  @prompt = string
end

.register(name, &block) ⇒ Object



30
31
32
33
34
35
# File 'lib/ona.rb', line 30

def self.register(name, &block)
  object = @resources[name][:class].new
  block.call(object)
  @resources[name][:entries].push(object)
  nil
end

.resource(name, attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/ona.rb', line 8

def self.resource(name, attributes)
  @resources ||= {}
  @resources[name] ||= {}
  @resources[name][:class] = Struct.new(*attributes)
  @resources[name][:entries] = []
  @resources[name][:handlers] = []
  @resources[name][:attributes] = attributes
  _create_to_s_method(@resources[name])
  nil
end

.run(command) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/ona.rb', line 62

def self.run(command)
  @resources.each do |name, resource|
    resource[:handlers].each do |handler|
      next unless handler[:regex].match(command)
      handler[:block].call(resource[:entries], command, handler[:regex])
      return
    end
  end
  nil
end