middleman-data_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,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in middleman-data_model.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Franklin Webber
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,80 @@
1
+ # Middleman::DataModel
2
+
3
+ Middelman allows you to define [local data](http://middlemanapp.com/advanced/local-data/). DataModel allows you to define a model class which will be mapped with that data. This is useful:
4
+
5
+ * If you hate hashes in your view code and want to call methods
6
+ * Want to eventually move to another system that uses an ORM
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'middleman-data_model'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ ```bash
19
+ $ bundle
20
+ ```
21
+
22
+ Or install it yourself as:
23
+
24
+ ```bash
25
+ $ gem install middleman-data_model
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ * Add the gem to your gemfile within your middleman project
31
+
32
+ ```ruby
33
+ gem 'middleman-data_model'
34
+ ```
35
+
36
+ * Add the following line to your **config.rb**
37
+
38
+ ```ruby
39
+ activate :data_models
40
+ ```
41
+
42
+ * Define a model class within your **data** directory which is a sub-class of `Middleman::DataModel::SimpleDataModel`.
43
+
44
+ Here I define an Event model which will automatically match itself to any data
45
+ stored in `data.events`.
46
+
47
+ ```ruby
48
+ class Event < Middleman::DataModel::SimpleDataModel
49
+
50
+ end
51
+ ```
52
+
53
+ * Use the model within your view:
54
+
55
+ ```erb
56
+ <div class="events">
57
+ <ul class="events-list">
58
+ <%= Event.all.each do |event| %>
59
+ <li class="vevent">
60
+ <div class="img">
61
+ <img src="<%= event.image_url" alt="<%= event.image_alt %>">
62
+ </div>
63
+ <div>
64
+ <h3><a href="/"><%= event.title %></a></h3>
65
+ <p class="location"><a href="/"><%= event.location %></a></p>
66
+ <p class="details"><%= event.details %></p>
67
+ </div>
68
+ </li>
69
+ <% end %>
70
+ </ul>
71
+ </div>
72
+ ```
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Push to the branch (`git push origin my-new-feature`)
80
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,27 @@
1
+ module Middleman
2
+ module DataModel
3
+
4
+ class SimpleDataModel < OpenStruct
5
+ def self.all
6
+ @data.map {|item| new item }
7
+ end
8
+
9
+ def self.data_name
10
+ @data_name ? @data_name : self.to_s.downcase.pluralize
11
+ end
12
+
13
+ def self.data=(data)
14
+ @data = data
15
+ end
16
+
17
+ def self.inherited(base)
18
+ models << base
19
+ end
20
+
21
+ def self.models
22
+ @models ||= []
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Middleman
2
+ module DataModel
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,41 @@
1
+ require "middleman-data_model/version"
2
+ require 'ostruct'
3
+ require 'middleman-data_model/simple_data_model'
4
+
5
+ module Middleman
6
+ module DataModel
7
+ extend self
8
+
9
+ def registered(app, options_hash = {})
10
+ options_hash = default_options.merge options_hash
11
+
12
+ require_models(options_hash[:model_dir])
13
+ load_models_with_app_data_hook(app)
14
+ end
15
+
16
+ def default_options
17
+ { model_dir: "data" }
18
+ end
19
+
20
+ def require_models(path)
21
+ model_paths(path).each {|file| require file }
22
+ end
23
+
24
+ def model_paths(path)
25
+ Dir["#{path}/**/*.rb"]
26
+ end
27
+
28
+ def load_models_with_app_data_hook(app)
29
+ app.before do
30
+ SimpleDataModel.models.each do |model|
31
+ model.data = data.send(model.data_name)
32
+ end
33
+ end
34
+ end
35
+
36
+ def included ; registered ; end
37
+
38
+ ::Middleman::Extensions.register(:data_models,DataModel)
39
+
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'middleman-data_model/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "middleman-data_model"
8
+ gem.version = Middleman::DataModel::VERSION
9
+ gem.authors = ["Franklin Webber"]
10
+ gem.email = ["franklin.webber@gmail.com"]
11
+ gem.description = %q{Adds simple model support to Middleman.}
12
+ gem.summary = %q{Middleman has data, but it isn't modeled. Modeling it
13
+ should be easy. This gem endeavors to make it easy.}
14
+ gem.homepage = "https://github.com/JumpstartLab/middleman-data_model"
15
+ gem.license = "MIT"
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: middleman-data_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Franklin Webber
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-25 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds simple model support to Middleman.
15
+ email:
16
+ - franklin.webber@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - lib/middleman-data_model.rb
27
+ - lib/middleman-data_model/simple_data_model.rb
28
+ - lib/middleman-data_model/version.rb
29
+ - middleman-datamodel.gemspec
30
+ homepage: https://github.com/JumpstartLab/middleman-data_model
31
+ licenses:
32
+ - MIT
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Middleman has data, but it isn't modeled. Modeling it should be easy. This
55
+ gem endeavors to make it easy.
56
+ test_files: []