Great rubocop-ing

This commit is contained in:
Thomas Reynolds 2014-04-28 16:02:18 -07:00
parent 8f75f6516d
commit 04dc48f13d
78 changed files with 457 additions and 607 deletions

View file

@ -1,10 +1,8 @@
module Middleman
module CoreExtensions
# The data extension parses YAML and JSON files in the data/ directory
# and makes them available to config.rb, templates and extensions
module Data
# Extension registered
class << self
# @private
@ -16,7 +14,7 @@ module Middleman
app.config.define_setting :data_dir, 'data', 'The directory data files are stored in'
app.send :include, InstanceMethods
end
alias :included :registered
alias_method :included, :registered
end
# Instance methods
@ -24,12 +22,12 @@ module Middleman
# Setup data files before anything else so they are available when
# parsing config.rb
def initialize
self.files.changed DataStore.matcher do |file|
self.data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
files.changed DataStore.matcher do |file|
data.touch_file(file) if file.start_with?("#{config[:data_dir]}/")
end
self.files.deleted DataStore.matcher do |file|
self.data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
files.deleted DataStore.matcher do |file|
data.remove_file(file) if file.start_with?("#{config[:data_dir]}/")
end
super
@ -45,10 +43,8 @@ module Middleman
# The core logic behind the data extension.
class DataStore
# Static methods
class << self
# The regex which tells Middleman which files are for data
#
# @return [Regexp]
@ -137,7 +133,7 @@ module Middleman
data_branch = data_branch[dir]
end
data_branch.delete(basename) if data_branch.has_key?(basename)
data_branch.delete(basename) if data_branch.key?(basename)
end
# Get a hash from either internal static data or a callback
@ -150,10 +146,10 @@ module Middleman
@@local_sources ||= {}
@@callback_sources ||= {}
if self.store.has_key?(path.to_s)
response = self.store[path.to_s]
elsif self.callbacks.has_key?(path.to_s)
response = self.callbacks[path.to_s].call()
if store.key?(path.to_s)
response = store[path.to_s]
elsif callbacks.key?(path.to_s)
response = callbacks[path.to_s].call
end
response
@ -164,7 +160,7 @@ module Middleman
# @param [String] path The namespace to search for
# @return [Hash, nil]
def method_missing(path)
if @local_data.has_key?(path.to_s)
if @local_data.key?(path.to_s)
return @local_data[path.to_s]
else
result = data_for_path(path)
@ -178,8 +174,8 @@ module Middleman
end
# Needed so that method_missing makes sense
def respond_to?(method, include_private = false)
super || has_key?(method)
def respond_to?(method, include_private=false)
super || key?(method)
end
# Make DataStore act like a hash. Return requested data, or
@ -188,24 +184,26 @@ module Middleman
# @param [String, Symbol] key The name of the data namespace
# @return [Hash, nil]
def [](key)
__send__(key) if has_key?(key)
__send__(key) if key?(key)
end
def has_key?(key)
@local_data.has_key?(key.to_s) || !!(data_for_path(key))
@local_data.key?(key.to_s) || !!(data_for_path(key))
end
alias_method :key?, :has_key?
# Convert all the data into a static hash
#
# @return [Hash]
def to_h
data = {}
self.store.each do |k, v|
store.each do |k, _|
data[k] = data_for_path(k)
end
self.callbacks.each do |k, v|
callbacks.each do |k, _|
data[k] = data_for_path(k)
end