Skip to content

Commit

Permalink
Put classes into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pfleidi committed Oct 10, 2013
1 parent 350ef4f commit 1117bae
Show file tree
Hide file tree
Showing 13 changed files with 613 additions and 757 deletions.
62 changes: 33 additions & 29 deletions lib/mlk/document.rb
@@ -1,45 +1,49 @@
# encoding: utf-8

class Document
module Mlk

attr_reader :content, :data
class Document

def initialize(raw_document, path = '')
split_text = split_text_and_meta(raw_document, path)
attr_reader :content, :data

@content = split_text[:content]
@data = split_text[:data]
end

def serialize
"#{ self.data.to_yaml }\n---\n#{ self.content }"
end
def initialize(raw_document, path = '')
split_text = split_text_and_meta(raw_document, path)

def ==(other_document)
meta_equals = self.data == other_document.data
content_equals = self.content == other_document.content
@content = split_text[:content]
@data = split_text[:data]
end

meta_equals && content_equals
end
def serialize
"#{ self.data.to_yaml }\n---\n#{ self.content }"
end

private
def ==(other_document)
meta_equals = self.data == other_document.data
content_equals = self.content == other_document.content

def split_text_and_meta(raw_document, path)
content = ''
data = { }
meta_equals && content_equals
end

begin
if match = raw_document.match(/^(---\s*\n(.*?)\n?)^(---\s*$\n?)(.*)/m)
data = YAML.load(match[2])
content = match[4].strip
else
raise "No metadata header available in file: #{ path }! Document:\n#{ raw_document }"
private

def split_text_and_meta(raw_document, path)
content = ''
data = { }

begin
if match = raw_document.match(/^(---\s*\n(.*?)\n?)^(---\s*$\n?)(.*)/m)
data = YAML.load(match[2])
content = match[4].strip
else
raise "No metadata header available in file: #{ path }! Document:\n#{ raw_document }"
end
rescue Psych::SyntaxError => e
raise "YAML error while reading #{ path }: #{ e.message }"
end
rescue Psych::SyntaxError => e
raise "YAML error while reading #{ path }: #{ e.message }"

{ content: content, data: data }
end

{ content: content, data: data }
end

end
Expand Down
250 changes: 127 additions & 123 deletions lib/mlk/model.rb
Expand Up @@ -2,176 +2,180 @@

require 'scrivener'

class Model
include Scrivener::Validations
module Mlk

class << self
attr_accessor :storage_engine
end

def self.defined_models
@all_models ||= [ ]
end
class Model
include Scrivener::Validations

def self.inherited(subclass)
defined_models << subclass
end
class << self
attr_accessor :storage_engine
end

def self.storage
ref = Utils.pluralize(to_reference)
Model.storage_engine.new(ref)
end
def self.defined_models
@all_models ||= [ ]
end

def self.default_sort_by
:name
end
def self.inherited(subclass)
defined_models << subclass
end

def [](name)
all.first(:name => name)
end
def self.storage
ref = Utils.pluralize(to_reference)
Model.storage_engine.new(ref)
end

# convenience wrappers around all()
def self.default_sort_by
:name
end

def self.first(filters = { })
all.first(filters)
end
def [](name)
all.first(:name => name)
end

def self.find(filters)
all.find(filters)
end
# convenience wrappers around all()

def self.find_match(filters)
all.find_match(filters)
end
def self.first(filters = { })
all.first(filters)
end

def self.all
results = storage.all.map do |path, raw_document|
document = Document.new(raw_document)
self.new(document, { :path => path })
def self.find(filters)
all.find(filters)
end

ResultSet.new(results)
end
def self.find_match(filters)
all.find_match(filters)
end

# Manage relations between models
def self.all
results = storage.all.map do |path, raw_document|
document = Document.new(raw_document)
self.new(document, { :path => path })
end

def self.attribute(name)
define_method(name) do
@data[name.to_s]
ResultSet.new(results)
end

add_attribute(name)
end

def self.add_attribute(attr)
@attrs ||= [ ]
# Manage relations between models

@attrs << attr
end
def self.attribute(name)
define_method(name) do
@data[name.to_s]
end

def self.attributes
if superclass.respond_to?(:attributes)
superclass.attributes + @attrs
else
@attrs
add_attribute(name)
end
end

def self.belongs_to(model, name)
define_method(name) do
name = name.to_s
model_class = Utils.class_lookup(self.class, model)
model_class.first(:name => self.data[name])
def self.add_attribute(attr)
@attrs ||= [ ]

@attrs << attr
end
end

def self.has_many(model, name, reference = to_reference)
define_method(name) do
model_class = Utils.class_lookup(self.class, model)
if reference.to_s.end_with?("s")
model_class.find_match(:"#{ reference }" => self.name)
def self.attributes
if superclass.respond_to?(:attributes)
superclass.attributes + @attrs
else
model_class.find(:"#{ reference }" => self.name)
@attrs
end
end
end

def self.to_reference
self.name.downcase
end
def self.belongs_to(model, name)
define_method(name) do
name = name.to_s
model_class = Utils.class_lookup(self.class, model)
model_class.first(:name => self.data[name])
end
end

def self.has_many(model, name, reference = to_reference)
define_method(name) do
model_class = Utils.class_lookup(self.class, model)
if reference.to_s.end_with?("s")
model_class.find_match(:"#{ reference }" => self.name)
else
model_class.find(:"#{ reference }" => self.name)
end
end
end

# Inheriting values for methods from other models (mostly connected by belongs_to)
def self.to_reference
self.name.downcase
end

# Inheriting values for methods from other models (mostly connected by belongs_to)

@@inheriting_methods = {}
@@inheriting_methods = {}

def self.inherits_from(parent_method, *args)
args.each do |method_name|
@@inheriting_methods[method_name] = parent_method
def self.inherits_from(parent_method, *args)
args.each do |method_name|
@@inheriting_methods[method_name] = parent_method
end
end
end

def self.inheriting_for_method(name)
if @@inheriting_methods.keys.include? name
parent_method = @@inheriting_methods[name]
@@inheriting_methods.delete(name)
overwrite_inheriting_method(name, parent_method)
def self.inheriting_for_method(name)
if @@inheriting_methods.keys.include? name
parent_method = @@inheriting_methods[name]
@@inheriting_methods.delete(name)
overwrite_inheriting_method(name, parent_method)
end
end
end

def self.method_added(name)
inheriting_for_method(name)
end
def self.method_added(name)
inheriting_for_method(name)
end

def self.overwrite_inheriting_method(name, parent_method)
original_method_name = :"#{ name }_non_inheriting"
alias_method original_method_name, name
def self.overwrite_inheriting_method(name, parent_method)
original_method_name = :"#{ name }_non_inheriting"
alias_method original_method_name, name

define_method(name) do
if eval "#{ original_method_name }.nil?"
eval "#{ parent_method }.#{ name }"
else
eval "#{ original_method_name }"
define_method(name) do
if eval "#{ original_method_name }.nil?"
eval "#{ parent_method }.#{ name }"
else
eval "#{ original_method_name }"
end
end
end
end

attr_reader :document, :content, :data
attr_reader :document, :content, :data

attribute :name
attribute :name

def initialize(document, options = { })
@path = options[:path]
@document = document
@content = @document.content
@data = @document.data
end
def initialize(document, options = { })
@path = options[:path]
@document = document
@content = @document.content
@data = @document.data
end

def attributes
self.class.attributes
end
def attributes
self.class.attributes
end

def validate
assert_present(:name)
end
def validate
assert_present(:name)
end

def template
if @data["template"]
@data["template"].to_sym
else
default_template
end
end

def template
if @data["template"]
@data["template"].to_sym
else
default_template
def default_template
self.class.name.downcase.to_sym
end
end

def default_template
self.class.name.downcase.to_sym
end
def ==(other_model)
self.document == other_model.document
end

def ==(other_model)
self.document == other_model.document
end
def save
self.class.storage.save(self.name, @document.serialize)
end

def save
self.class.storage.save(self.name, @document.serialize)
end

end
Expand Down

0 comments on commit 1117bae

Please sign in to comment.