Class: Iated::EditSession

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

Overview

An Edit Session

A single session for editing a textarea. It tracks the editor, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ EditSession

Returns a new instance of EditSession.

Parameters:

  • options (Hash) (defaults to: nil)

    Various optional arguments (:url, :tid, :extension)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/iated/edit_session.rb', line 24

def initialize options=nil
  normalized_options = normalize_keys options
  @url               = normalized_options[:url]
  @tid               = normalized_options[:tid]
  @extension         = normalized_options[:extension]
  @change_id         = 0
  @sid               = self.class.calculate_sid normalized_options
  @last_checksum     = :nochecksum

  # Save session.
  Iated::sessions[@sid] = self

  # Save the text, if passed in the original options
  self.text = options[:text] if options.key?(:text)
end

Instance Attribute Details

#extensionString? (readonly)

Returns The extension the text file should use.

Returns:

  • (String, nil)

    The extension the text file should use



18
19
20
# File 'lib/iated/edit_session.rb', line 18

def extension
  @extension
end

#sidString (readonly)

Returns The session id.

Returns:

  • (String)

    The session id



20
21
22
# File 'lib/iated/edit_session.rb', line 20

def sid
  @sid
end

#tidString? (readonly)

Returns The textarea id.

Returns:

  • (String, nil)

    The textarea id



16
17
18
# File 'lib/iated/edit_session.rb', line 16

def tid
  @tid
end

#urlString? (readonly)

Returns The URL the textarea came from.

Returns:

  • (String, nil)

    The URL the textarea came from



14
15
16
# File 'lib/iated/edit_session.rb', line 14

def url
  @url
end

Class Method Details

.calculate_sid(options) ⇒ String

Calculate a sid based on url, id, ext

Returns:

  • (String)

    A session id



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/iated/edit_session.rb', line 61

def self.calculate_sid options
  options = normalize_keys options

  # TODO Should retrieve sid from datastore if it exists.
  # TODO The sid calculation needs a random number
  # TODO The sid calculation needs the current time or date
  digest = Digest::MD5.new
  digest << "url: #{options[:url]}"
  digest << "tid: #{options[:tid]}"
  digest << "extension: #{options[:extension]}"
  return digest.hexdigest
end

.find(search = nil) ⇒ EditSession

Finds an existing session

Returns:



42
43
44
45
46
47
48
49
50
# File 'lib/iated/edit_session.rb', line 42

def self.find search=nil
  # TODO Once the sid is random, then find needs to work via a data store.
  if search.is_a? String
    tok = search
  else
    tok = calculate_sid search
  end
  Iated::sessions[tok]
end

.find_or_create(search = nil) ⇒ EditSession

Finds an existing session or creates a new one

Returns:



54
55
56
57
# File 'lib/iated/edit_session.rb', line 54

def self.find_or_create search=nil
  sess = find search
  sess.nil? ? self.new(search) : sess
end

.normalize_keys(hash = nil) ⇒ Hash

Normalizes the search options (‘:url`, `:tid`, `:extension`)

Used by other functions

Returns:

  • (Hash)


196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/iated/edit_session.rb', line 196

def self.normalize_keys hash=nil
  norm_hash = {}
  hash = {} if hash.nil?

  [:url, :tid, :extension].each do |key|
    norm_hash[key] = hash[key] || hash[key.to_s] || (:extension == key ? '.txt' : nil)
  end

  # Verify the keys are sane. We ignore :text.
  accepted_keys = Set.new((hash.keys + [:text]).map {|x| [x, x.to_s]}.flatten)
  unexpected_keys = Set.new(hash.keys) - accepted_keys
  raise "Invalid keys: #{unexpected_keys.inspect}" if unexpected_keys.count > 0
  return norm_hash
end

Instance Method Details

#change_idObject

Return the change_id



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

def change_id
  if filename.exist?
    new_checksum = filename.open('r') do |f|
      Digest::MD5.hexdigest(f.read)
    end

    if @last_checksum != new_checksum
      increment_change_id
      @last_checksum = new_checksum
    end
  end
  @change_id
end

#editObject

Opens the user’s configured editor.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/iated/edit_session.rb', line 104

def edit
  editor = Iated.mcp.prefs.editor.to_s
  if Iated::environment == :test
    # We don't want to fire up real editors in testing mode.
    #$stderr.puts "I would have edited #{filename.to_s.inspect} with #{editor.inspect}"
    return
  else
    if RUBY_ENGINE == "jruby"
      edit_jruby
    else
      edit_ruby
    end
  end
end

#edit_jrubyObject

The JRuby version of edit



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

def edit_jruby
  editor = Iated.mcp.prefs.editor.to_s

  cmd = nil # We will store the CommandLine object here.
  java_import org.apache.commons.exec.OS
  java_import org.apache.commons.exec.CommandLine
  java_import org.apache.commons.exec.DefaultExecutor

  if OS.is_family_mac && editor =~ /\.app$/
    # It's a Mac .app
    cmd = CommandLine.new "/usr/bin/open"
    cmd.add_argument("-a").add_argument(editor)
  else
    cmd = CommandLine.new editor
  end
  cmd.add_argument(filename.to_s)

  executor = DefaultExecutor.new
  executor.execute(cmd)
end

#edit_rubyObject

The Ruby version of edit



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/iated/edit_session.rb', line 120

def edit_ruby
  editor = Iated.mcp.prefs.editor.to_s
  cmd = []
  if RUBY_PLATFORM =~ /darwin/ && editor =~ /\.app$/
    cmd << "/usr/bin/open"
    cmd << "-a"
  end
  cmd << editor
  cmd << filename.to_s
  # TODO This doesn't get the exit code...
  system *cmd
end

#filenamePathname

Returns the file where the session is saved.

Returns:

  • (Pathname)

    The filename and path the text was saved to.



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/iated/edit_session.rb', line 157

def filename
  if @filename.nil?
    bad_chars = /[^a-zA-Z0-9._-]+/
    url = Addressable::URI.parse(@url)
    config_dir = Iated::mcp.prefs.config_dir
    if url.host
      # TODO Handle case where url is the root and filename is empty.
      @filename = config_dir + url.host.gsub(bad_chars, '') +
        "#{url.basename.gsub(bad_chars, '')}#{@extension}"
    else
      @filename = config_dir + "#{@url.gsub(bad_chars, '')}#{@extension}"
    end
  end
  return @filename
end

#increment_change_idInteger

Increment the change_id

Returns:

  • (Integer)

    The new number of changes



76
77
78
# File 'lib/iated/edit_session.rb', line 76

def increment_change_id
  @change_id = @change_id + 1
end

#normalize_keys(hash) ⇒ Hash

Alias for the class-method ‘normalize_keys`

Returns:

  • (Hash)

See Also:



214
215
216
# File 'lib/iated/edit_session.rb', line 214

def normalize_keys hash
  self.class.normalize_keys hash
end

#running?Boolean

Returns true if the editor is running.

Returns:

  • (Boolean)

    True if the editor is running



98
99
100
101
# File 'lib/iated/edit_session.rb', line 98

def running?
  # TODO This should check to see if a process is running or not.
  return true
end

#textObject

Returns the text of the filename



174
175
176
177
178
179
180
# File 'lib/iated/edit_session.rb', line 174

def text
  if filename.exist?
    filename.read
  else
    nil
  end
end

#text=(value) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/iated/edit_session.rb', line 182

def text= value
  increment_change_id if filename.exist?
  filename.dirname.mkpath
  # TODO: locking
  filename.open('w') do |f|
    f.write value
  end
  @last_checksum = Digest::MD5.hexdigest(value)
end