markup_model 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in markup_model.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,8 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :cli => "--format documentation --colour", :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Brian Alexander
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MarkupModel
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'markup_model'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install markup_model
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new do |t|
5
+ t.rspec_opts = %w(--format documentation --colour)
6
+ end
7
+
8
+ task default: 'spec'
@@ -0,0 +1,49 @@
1
+ require "active_support/concern"
2
+ require "markup_model/config"
3
+ require "markup_model/model_file"
4
+ require "markup_model/version"
5
+
6
+ require "markup_model/engine" if defined?(Rails)
7
+
8
+ module MarkupModel
9
+ extend ActiveSupport::Concern
10
+
11
+ @@config = Config.new
12
+
13
+ def self.config
14
+ @@config
15
+ end
16
+
17
+ module ClassMethods
18
+ def markup_model_cleanup
19
+ ids = Set.new markup_model_files.map(&:id)
20
+
21
+ all.each do |model|
22
+ model.destroy unless ids.include?(model.id.to_s)
23
+ end
24
+ end
25
+
26
+ def markup_model_files
27
+ Dir.glob("#{MarkupModel.config.content_directory}/#{model_name.plural}/*.md").map do |file|
28
+ ModelFile.new(file)
29
+ end
30
+ end
31
+
32
+ def markup_model_reload
33
+ markup_model_cleanup
34
+ markup_model_update
35
+ end
36
+
37
+ def markup_model_update
38
+ markup_model_files.each do |file|
39
+ model = find_by_id(file.id) || new.tap { |m| m.id = file.id }
40
+ model.id = file.id
41
+ model.content = file.content
42
+ file.attributes.each do |k,v|
43
+ model.public_send("#{k}=", v)
44
+ end
45
+ model.save!
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,5 @@
1
+ module MarkupModel
2
+ class Config
3
+ attr_accessor :content_directory
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ module MarkupModel
2
+ class Engine < Rails::Engine
3
+ initializer "markup_model.config" do
4
+ MarkupModel.config.content_directory ||= Rails.root + "content"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,36 @@
1
+ module MarkupModel
2
+ class ModelFile
3
+ attr_reader :path
4
+
5
+ def initialize(path)
6
+ @attributes = nil
7
+ @path = path
8
+ end
9
+
10
+ def attributes
11
+ read_attributes
12
+ @attributes
13
+ end
14
+
15
+ def content
16
+ read_attributes
17
+ @content
18
+ end
19
+
20
+ def id
21
+ File.basename(path).sub(/\.md$/i, "").sub(/^0*/, "")
22
+ end
23
+
24
+ private
25
+ def read_attributes
26
+ unless @attributes
27
+ if File.read(path) =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
28
+ @content = $'
29
+ @attributes = YAML.load($1) || {}
30
+ else
31
+ raise "File '#{path}' has the wrong format. It should contain Markdown with a YAML header."
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module MarkupModel
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'markup_model/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "markup_model"
8
+ gem.version = MarkupModel::VERSION
9
+ gem.authors = ["Brian Alexander"]
10
+ gem.email = ["balexand@gmail.com"]
11
+ gem.description = %q{Under construction}
12
+ gem.summary = %q{Under construction}
13
+ gem.homepage = "https://github.com/balexand/markup_model"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'guard-rspec'
21
+ gem.add_development_dependency 'pry'
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'rspec'
24
+ gem.add_development_dependency 'supermodel'
25
+ end
@@ -0,0 +1,4 @@
1
+ ---
2
+ title: Foo Bar
3
+ ---
4
+ Content for number 1
@@ -0,0 +1,3 @@
1
+ ---
2
+ ---
3
+ Content for number 2
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ class Post < SuperModel::Base
4
+ include MarkupModel
5
+ end
6
+
7
+ describe MarkupModel do
8
+ before(:each) do
9
+ Post.delete_all
10
+ end
11
+
12
+ it "should cleanup models" do
13
+ ["1", 2, 3].each { |id| Post.create id: id }
14
+ Post.all.map(&:id).should == ["1", 2, 3]
15
+ Post.markup_model_cleanup
16
+ Post.all.map(&:id).should == ["1", 2]
17
+ end
18
+
19
+ it "should update models" do
20
+ Post.markup_model_update
21
+ Post.count.should == 2
22
+
23
+ first = Post.first
24
+ first.id.should == "1"
25
+ first.content.should == "Content for number 1"
26
+ first.title.should == "Foo Bar"
27
+
28
+ second = Post.last
29
+ second.id.should == "2"
30
+ second.content.should == "Content for number 2"
31
+ end
32
+
33
+ it "should not define a config method on the model" do
34
+ expect do
35
+ Post.config
36
+ end.to raise_exception(NoMethodError)
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ require 'pry'
2
+ require 'markup_model'
3
+ require 'rspec'
4
+ require 'supermodel'
5
+
6
+ RSpec.configure do |config|
7
+ config.before(:each) do
8
+ MarkupModel.config.content_directory = File.expand_path("../content", __FILE__)
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: markup_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Alexander
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: guard-rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: pry
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: supermodel
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ description: Under construction
95
+ email:
96
+ - balexand@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - Gemfile
103
+ - Guardfile
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - lib/markup_model.rb
108
+ - lib/markup_model/config.rb
109
+ - lib/markup_model/engine.rb
110
+ - lib/markup_model/model_file.rb
111
+ - lib/markup_model/version.rb
112
+ - markup_model.gemspec
113
+ - spec/content/posts/001.md
114
+ - spec/content/posts/002.md
115
+ - spec/markup_model_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: https://github.com/balexand/markup_model
118
+ licenses: []
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ! '>='
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 1.8.24
138
+ signing_key:
139
+ specification_version: 3
140
+ summary: Under construction
141
+ test_files:
142
+ - spec/content/posts/001.md
143
+ - spec/content/posts/002.md
144
+ - spec/markup_model_spec.rb
145
+ - spec/spec_helper.rb