Class: WOR::CollectionItem

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ CollectionItem

Returns a new instance of CollectionItem.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/wor/collection_item.rb', line 5

def initialize path
  self.body = Hash.new
  self.layout = "application"
  self.name = "Null"

  if (!File.exists?(path))
    puts 'File not exists'
    return
  end

  if (WOR::Configuration::USE_LOCALIZATION)
    self.locale = WOR::Localization.get_locale
    self.collection_name = WOR::Collection.get_folder_name_by_path(path: path, locale: self.locale)
  else
    self.collection_name = WOR::Collection.get_folder_name_by_path(path: path)
  end

  if (File.file?(path))
    file_initialize path
  else
    directory_initialize path
  end
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



3
4
5
# File 'lib/wor/collection_item.rb', line 3

def body
  @body
end

#collection_nameObject

Returns the value of attribute collection_name.



3
4
5
# File 'lib/wor/collection_item.rb', line 3

def collection_name
  @collection_name
end

#layoutObject

Returns the value of attribute layout.



3
4
5
# File 'lib/wor/collection_item.rb', line 3

def layout
  @layout
end

#localeObject

Returns the value of attribute locale.



3
4
5
# File 'lib/wor/collection_item.rb', line 3

def locale
  @locale
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/wor/collection_item.rb', line 3

def name
  @name
end

Instance Method Details

#add_language(file_data, language) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/wor/collection_item.rb', line 80

def add_language file_data, language
  if (language == "")
    language = "default"
  end

  self.body[language] = file_data
end

#content(language = "default") ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wor/collection_item.rb', line 49

def content (language = "default")
  language = language.to_s
  
  lang_content = self.body[language]

  if (!lang_content.nil?)
    return lang_content
  end

  lang_content = self.body["default"]

  if (!lang_content.nil?)
    return lang_content
  else
    return ""
  end
end

#directory_initialize(path) ⇒ Object



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

def directory_initialize path
  files = Dir.glob(path + "/*")

  files.each do | file |
    extn = File.extname  file
    name = File.basename file, extn
    if (name == "_meta")
      create_custom_properties(file, false)
    else
      self.add_language File.read(file), name
    end
  end
end

#file_initialize(path) ⇒ Object



29
30
31
32
33
# File 'lib/wor/collection_item.rb', line 29

def file_initialize path
  extn = File.extname path 
  self.name = File.basename path, extn
  create_custom_properties(path, true)
end


67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wor/collection_item.rb', line 67

def get_permalink locale
  begin
    permalink = self.send("permalink_" + locale)
    if (permalink.present?)
      return permalink
    else
      return self.permalink
    end
  rescue
    return self.permalink
  end
end

#urlObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/wor/collection_item.rb', line 88

def url
  if (self.collection_name == "pages")
    translated_collection = ""
  else
    translated_collection = "/" + WOR::Localization.translate_path_to_language(path: self.collection_name, locale: self.locale)
  end

  if (self.name == "index")
    translated_name = ""
  else
    translated_name = "/" + WOR::Localization.translate_path_to_language(path: self.name, locale: self.locale)
  end    

  url = "/#{self.locale}#{translated_collection}#{translated_name}"

  return url
end