Class: Days::Command

Inherits:
Thor
  • Object
show all
Defined in:
lib/days/command.rb

Constant Summary collapse

SKELETON_PATH =
File.expand_path(File.join(__FILE__, '..', '..', '..', 'skeleton', 'days'))

Instance Method Summary collapse

Instance Method Details

#console(env = "development") ⇒ Object



93
94
95
96
97
98
99
# File 'lib/days/command.rb', line 93

def console(env = "development")
  set_env env
  require 'pry'
  require_relative 'models'
  config.establish_db_connection()
  Pry.start(binding)
end

#import(file) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/days/command.rb', line 104

def import(file)
  set_env
  config.establish_db_connection()
  require 'json'
  users = {}
  categories = {}
  open(file, 'r') do |io|
    io.readlines.each do |line|
      line = JSON.parse(line)

      attributes ={}
      if line['id'] && Entry.where(id: line['id']).count.zero?
        new_id = line['id']
      else
        new_id = nil
      end

      if line['user']
        if users.has_key?(line['user'])
          user = users[line['user']]
        else
          user = users[line['user']] = User.where(login_name: line['user']).first
        end


        attributes[:user] = user if user
      end

      if line['category']
        attributes[:categories] = line['category'].map do |category_name|
          categories[category_name] ||= Category.find_or_create_by_name!(category_name)
        end
      end

      attributes[:slug] = line['slug'] if line['slug']
      attributes[:title] = line['title'] if line['title']
      attributes[:body] = line['body'] if line['body']
      attributes[:published_at] = line['published_at'] if line['published_at']
      attributes[:draft] = line['draft'] if line['draft']
      attributes[:old_path] = line['old_path'] if line['old_path']

      p attributes[:title]
      entry = Entry.new(attributes)
      entry.id = new_id if new_id
      entry.save!
    end
  end
end

#init(dir = ".") ⇒ Object



11
12
13
14
# File 'lib/days/command.rb', line 11

def init(dir = ".")
  puts "Initializing new Days environment on #{File.expand_path dir}"
  FileUtils.cp_r Dir["#{SKELETON_PATH}/*"], "#{dir}/"
end

#init_theme(dir = ".") ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/days/command.rb', line 17

def init_theme(dir = ".")
  if File.exists?(File.join(dir, 'views')) || File.exists?(File.join(dir, 'stylesheets'))
    puts "This will override the following:"
    puts "* #{dir}/views/entries.haml"
    puts "* #{dir}/views/entry.haml"
    puts "* #{dir}/views/layout.haml"
    puts "* #{dir}/stylesheets/style.scss"

    print "Continue (y/n)? "

    while _ = $stdin.gets
      case _
      when /^y|yes$/
        break
      when /^n|no$/
        puts 'Cancelled.'
        return
      else
        print "Please answer in 'yes' or 'no' or 'y' or 'n': "
      end
    end
  end

  require 'fileutils'

  root = File.expand_path(File.join(__FILE__, '..', '..', '..', 'app'))
  FileUtils.mkdir_p File.join(dir, 'views')
  FileUtils.mkdir_p File.join(dir, 'stylesheets')

  %w(entries.haml entry.haml layout.haml).each do |file|
    FileUtils.cp(File.join(root, 'views', file),
                 File.join(dir,  'views', file))
  end

  FileUtils.cp(File.join(root, 'stylesheets', 'style.scss'),
               File.join(dir,  'stylesheets', 'style.scss'))
end

#migrate(env = "development") ⇒ Object



86
87
88
89
# File 'lib/days/command.rb', line 86

def migrate(env = "development")
  set_env env
  Days::Migrator.start(config, options)
end

#serverObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/days/command.rb', line 61

def server
  set_env
  App.config = config
  rack_options = {
    app: App.rack,
    Port: options[:port],
    Host: options[:bind],
    daemonize: options[:daemonize],
    environment: options[:environment],
    server: options[:server],
  }
  rack_options.merge!(pid: File.expand_path(options[:pid])) if options[:pid]
  Rack::Server.start(rack_options)
end