Class: SoupCMS::CLI::Model::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/soupcms/cli/model/base.rb

Direct Known Subclasses

Markdown, Yaml

Constant Summary collapse

SEVERITY_COLOR_MAP =
{ 'INFO' => :green,'DEBUG' => :yellow}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Base

Returns a new instance of Base.



35
36
37
38
39
40
41
42
# File 'lib/soupcms/cli/model/base.rb', line 35

def initialize(file);
  @file = file;
  @logger = Logger.new(STDOUT)
  @logger.level = ENV['verbose'] == 'true' ? Logger::DEBUG : Logger::INFO
  @logger.formatter = proc do |severity, datetime, progname, msg|
    "#{severity}: #{msg}\n".colorize(SEVERITY_COLOR_MAP[severity] || :red)
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



44
45
46
# File 'lib/soupcms/cli/model/base.rb', line 44

def file
  @file
end

Class Method Details

.create_model(file) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/soupcms/cli/model/base.rb', line 12

def self.create_model(file)
  type = File.basename(file).split('.').last
  model = file.path.split('/')[2]
  case type
    when 'json'
      SoupCMS::CLI::Model::Base.new(file).create
    when 'yml'
      SoupCMS::CLI::Model::Yaml.new(file).create
    when 'md'
      case model
        when 'posts'
          SoupCMS::CLI::Model::Post.new(file).create
        when 'chapters'
          SoupCMS::CLI::Model::Chapter.new(file).create
        when 'pages'
          SoupCMS::CLI::Model::Page.new(file).create
        else
          SoupCMS::CLI::Model::Markdown.new(file).create
      end
  end
end

Instance Method Details

#app_nameObject



68
69
70
# File 'lib/soupcms/cli/model/base.rb', line 68

def app_name;
  file.path.split('/')[1]
end

#buildObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/soupcms/cli/model/base.rb', line 103

def build
  doc['doc_id'] = doc_name unless doc['doc_id']

  timestamp = file.mtime.to_i

  doc['publish_datetime'] = doc['publish_datetime'].to_i || timestamp
  doc['version'] = timestamp unless doc['version']
  doc['locale'] = 'en_US' unless doc['locale']
  doc['update_datetime'] = timestamp
  doc['create_datetime'] = (old_doc.empty? ? timestamp : old_doc['create_datetime'])
  doc['create_by'] = 'seed' unless doc['create_by']

  doc['state'] = publish_in_future? ? 'draft' : 'published' unless doc['state']
  doc['latest'] = true unless doc['latest']

  doc['slug'] = slug unless doc['slug']
  doc['hero_image'] = {'url' => hero_image} if hero_image
end

#collObject



76
77
78
# File 'lib/soupcms/cli/model/base.rb', line 76

def coll;
  db[model]
end

#connObject



46
47
48
49
50
# File 'lib/soupcms/cli/model/base.rb', line 46

def conn
  return @conn if @conn
  mongo_uri = ENV["MONGODB_URI_#{app_name}"] || ENV["MONGOLAB_URI"] || "mongodb://localhost:27017/#{app_name}"
  @conn = Mongo::MongoClient.from_uri(mongo_uri)
end

#createObject



126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/soupcms/cli/model/base.rb', line 126

def create
  build
  if doc['update_datetime'] == old_doc['update_datetime']
    @logger.debug "Skipping document '#{file.path}' since no changes"
  else
    @logger.info "Inserting document '#{file.path}'"
    @logger.debug "\n #{JSON.pretty_generate(doc)}"
    coll.insert(doc)
    update_old_doc
  end
  conn.close
end

#dbObject



72
73
74
# File 'lib/soupcms/cli/model/base.rb', line 72

def db;
  conn.db
end

#docObject



86
87
88
# File 'lib/soupcms/cli/model/base.rb', line 86

def doc;
  @doc ||= parse_file
end

#doc_nameObject



52
53
54
# File 'lib/soupcms/cli/model/base.rb', line 52

def doc_name;
  File.basename(file).split('.').first
end

#hero_imageObject



80
81
82
83
84
# File 'lib/soupcms/cli/model/base.rb', line 80

def hero_image
  image_path = File.join('public', app_name, model, "images/#{doc_name}.*")
  hero_image = Dir.glob(image_path).to_a
  return File.join('/assets', app_name, model, 'images', File.basename(hero_image[0])) unless hero_image.empty?
end

#modelObject



64
65
66
# File 'lib/soupcms/cli/model/base.rb', line 64

def model;
  file.path.split('/')[2]
end

#old_docObject



95
96
97
# File 'lib/soupcms/cli/model/base.rb', line 95

def old_doc
  @old_doc ||= (coll.find({'doc_id' => doc['doc_id'], 'latest' => true}).to_a[0] || {})
end

#parse_fileObject



90
91
92
93
# File 'lib/soupcms/cli/model/base.rb', line 90

def parse_file
  document_hash = JSON.parse(file.read)
  SoupCMS::CLI::ResolveFileReference.new(File.dirname(file)).parse(document_hash)
end

#publish_in_future?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/soupcms/cli/model/base.rb', line 122

def publish_in_future?
  doc['publish_datetime'] > Time.now.to_i
end

#slugObject



56
57
58
# File 'lib/soupcms/cli/model/base.rb', line 56

def slug;
  doc['slug'] || doc_name
end

#typeObject



60
61
62
# File 'lib/soupcms/cli/model/base.rb', line 60

def type;
  File.basename(file).split('.').last
end

#update_old_docObject



99
100
101
# File 'lib/soupcms/cli/model/base.rb', line 99

def update_old_doc
  coll.update({'_id' => old_doc['_id']}, {'$set' => {'latest' => false, 'state' => 'published_archive'}}) unless old_doc.empty?
end