Skip to content

Commit

Permalink
rubocop: fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ayastreb committed Jun 2, 2016
1 parent e97fd34 commit 597e8ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 40 deletions.
1 change: 0 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ AllCops:
- lib/jekyll/deprecator.rb
- lib/jekyll/document.rb
- lib/jekyll/filters.rb
- lib/jekyll/frontmatter_defaults.rb
- lib/jekyll/regenerator.rb
- lib/jekyll/renderer.rb
- lib/jekyll/site.rb
Expand Down
83 changes: 44 additions & 39 deletions lib/jekyll/frontmatter_defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,52 @@ def initialize(site)
end

def update_deprecated_types(set)
return set unless set.key?('scope') && set['scope'].key?('type')

set['scope']['type'] =
case set['scope']['type']
when 'page'
Deprecator.defaults_deprecate_type('page', 'pages')
'pages'
when 'post'
Deprecator.defaults_deprecate_type('post', 'posts')
'posts'
when 'draft'
Deprecator.defaults_deprecate_type('draft', 'drafts')
'drafts'
return set unless set.key?("scope") && set["scope"].key?("type")

set["scope"]["type"] =
case set["scope"]["type"]
when "page"
Deprecator.defaults_deprecate_type("page", "pages")
"pages"
when "post"
Deprecator.defaults_deprecate_type("post", "posts")
"posts"
when "draft"
Deprecator.defaults_deprecate_type("draft", "drafts")
"drafts"
else
set['scope']['type']
set["scope"]["type"]
end

set
end

def ensure_time!(set)
return set unless set.key?('values') && set['values'].key?('date')
return set if set['values']['date'].is_a?(Time)
set['values']['date'] = Utils.parse_date(set['values']['date'], "An invalid date format was found in a front-matter default set: #{set}")
return set unless set.key?("values") && set["values"].key?("date")
return set if set["values"]["date"].is_a?(Time)
set["values"]["date"] = Utils.parse_date(
set["values"]["date"],
"An invalid date format was found in a front-matter default set: #{set}"
)
set
end

# Finds a default value for a given setting, filtered by path and type
#
# path - the path (relative to the source) of the page, post or :draft the default is used in
# type - a symbol indicating whether a :page, a :post or a :draft calls this method
# path - the path (relative to the source) of the page,
# post or :draft the default is used in
# type - a symbol indicating whether a :page,
# a :post or a :draft calls this method
#
# Returns the default value or nil if none was found
def find(path, type, setting)
value = nil
old_scope = nil

matching_sets(path, type).each do |set|
if set['values'].key?(setting) && has_precedence?(old_scope, set['scope'])
value = set['values'][setting]
old_scope = set['scope']
if set["values"].key?(setting) && precedence?(old_scope, set["scope"])
value = set["values"][setting]
old_scope = set["scope"]
end
end
value
Expand All @@ -67,11 +72,11 @@ def all(path, type)
defaults = {}
old_scope = nil
matching_sets(path, type).each do |set|
if has_precedence?(old_scope, set['scope'])
defaults = Utils.deep_merge_hashes(defaults, set['values'])
old_scope = set['scope']
if precedence?(old_scope, set["scope"])
defaults = Utils.deep_merge_hashes(defaults, set["values"])
old_scope = set["scope"]
else
defaults = Utils.deep_merge_hashes(set['values'], defaults)
defaults = Utils.deep_merge_hashes(set["values"], defaults)
end
end
defaults
Expand All @@ -91,9 +96,9 @@ def applies?(scope, path, type)
end

def applies_path?(scope, path)
return true if !scope.key?('path') || scope['path'].empty?
return true if !scope.key?("path") || scope["path"].empty?

scope_path = Pathname.new(scope['path'])
scope_path = Pathname.new(scope["path"])
Pathname.new(sanitize_path(path)).ascend do |ascended_path|
if ascended_path.to_s == scope_path.to_s
return true
Expand All @@ -113,7 +118,7 @@ def applies_path?(scope, path)
# Returns true if either of the above conditions are satisfied,
# otherwise returns false
def applies_type?(scope, type)
!scope.key?('type') || scope['type'].eql?(type.to_s)
!scope.key?("type") || scope["type"].eql?(type.to_s)
end

# Checks if a given set of default values is valid
Expand All @@ -122,7 +127,7 @@ def applies_type?(scope, type)
#
# Returns true if the set is valid and can be used in this class
def valid?(set)
set.is_a?(Hash) && set['values'].is_a?(Hash)
set.is_a?(Hash) && set["values"].is_a?(Hash)
end

# Determines if a new scope has precedence over an old one
Expand All @@ -131,18 +136,18 @@ def valid?(set)
# new_scope - the new scope hash
#
# Returns true if the new scope has precedence over the older
def has_precedence?(old_scope, new_scope)
def precedence?(old_scope, new_scope)
return true if old_scope.nil?

new_path = sanitize_path(new_scope['path'])
old_path = sanitize_path(old_scope['path'])
new_path = sanitize_path(new_scope["path"])
old_path = sanitize_path(old_scope["path"])

if new_path.length != old_path.length
new_path.length >= old_path.length
elsif new_scope.key? 'type'
elsif new_scope.key? "type"
true
else
!old_scope.key? 'type'
!old_scope.key? "type"
end
end

Expand All @@ -151,7 +156,7 @@ def has_precedence?(old_scope, new_scope)
# Returns an array of hashes
def matching_sets(path, type)
valid_sets.select do |set|
!set.key?('scope') || applies?(set['scope'], path, type)
!set.key?("scope") || applies?(set["scope"], path, type)
end
end

Expand All @@ -162,15 +167,15 @@ def matching_sets(path, type)
#
# Returns an array of hashes
def valid_sets
sets = @site.config['defaults']
sets = @site.config["defaults"]
return [] unless sets.is_a?(Array)

sets.map do |set|
if valid?(set)
ensure_time!(update_deprecated_types(set))
else
Jekyll.logger.warn "Defaults:", "An invalid front-matter default set was found:"
Jekyll.logger.warn "#{set}"
Jekyll.logger.warn set.to_s
nil
end
end.compact
Expand All @@ -181,7 +186,7 @@ def sanitize_path(path)
if path.nil? || path.empty?
""
else
path.gsub(/\A\//, '').gsub(/([^\/])\z/, '\1')
path.gsub(%r!\A/!, "").gsub(%r!([^/])\z!, '\1')
end
end
end
Expand Down

0 comments on commit 597e8ee

Please sign in to comment.