liquidize 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/.rspec +3 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +13 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +85 -0
  10. data/Rakefile +7 -0
  11. data/lib/liquidize.rb +6 -0
  12. data/lib/liquidize/helper.rb +14 -0
  13. data/lib/liquidize/model.rb +100 -0
  14. data/lib/liquidize/version.rb +3 -0
  15. data/liquidize.gemspec +30 -0
  16. data/spec/dummy/README.rdoc +28 -0
  17. data/spec/dummy/Rakefile +6 -0
  18. data/spec/dummy/app/assets/images/.keep +0 -0
  19. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  20. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  21. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  22. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/mailers/.keep +0 -0
  25. data/spec/dummy/app/models/concerns/.keep +0 -0
  26. data/spec/dummy/app/models/non_active_record_page.rb +5 -0
  27. data/spec/dummy/app/models/page.rb +4 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/config.ru +4 -0
  33. data/spec/dummy/config/application.rb +23 -0
  34. data/spec/dummy/config/boot.rb +5 -0
  35. data/spec/dummy/config/database.yml +25 -0
  36. data/spec/dummy/config/environment.rb +5 -0
  37. data/spec/dummy/config/environments/development.rb +37 -0
  38. data/spec/dummy/config/environments/production.rb +82 -0
  39. data/spec/dummy/config/environments/test.rb +39 -0
  40. data/spec/dummy/config/initializers/assets.rb +8 -0
  41. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  43. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/spec/dummy/config/initializers/inflections.rb +16 -0
  45. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  46. data/spec/dummy/config/initializers/session_store.rb +3 -0
  47. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/spec/dummy/config/locales/en.yml +23 -0
  49. data/spec/dummy/config/routes.rb +2 -0
  50. data/spec/dummy/config/secrets.yml +22 -0
  51. data/spec/dummy/db/migrate/20140904151853_create_pages.rb +8 -0
  52. data/spec/dummy/db/schema.rb +21 -0
  53. data/spec/dummy/lib/assets/.keep +0 -0
  54. data/spec/dummy/public/404.html +67 -0
  55. data/spec/dummy/public/422.html +67 -0
  56. data/spec/dummy/public/500.html +66 -0
  57. data/spec/dummy/public/favicon.ico +0 -0
  58. data/spec/lib/liquidize/helper_spec.rb +12 -0
  59. data/spec/lib/liquidize/model_spec.rb +134 -0
  60. data/spec/lib/liquidize_spec.rb +7 -0
  61. data/spec/spec_helper.rb +84 -0
  62. metadata +236 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c380dada73395e35a3dd46636f6b52dabfe91b10
4
+ data.tar.gz: de63760111e7b5f07d487aeb392fba096f38a189
5
+ SHA512:
6
+ metadata.gz: 77078eb95e284b675ede95bf29299bee697d0c063d21f853e7738620699a23495c2f70314f0d6c9738b37b1dfc290823c40376bc4b484c674b3d2bed0821de7e
7
+ data.tar.gz: a7c0f01e368c60ca8713b1231f87d7614b756239a817acac67a656bf5aa581fe0c06b9493f8f9426bb106cf90a5f6c15eefa70f5a8ad4d6adce49b54e2b30081
@@ -0,0 +1,25 @@
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
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ .DS_Store
24
+ spec/dummy/log
25
+ spec/dummy/db/*.sqlite3
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ liquidize
@@ -0,0 +1 @@
1
+ 2.1.2
@@ -0,0 +1,13 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - ruby-head
7
+ env:
8
+ - DB=sqlite
9
+ script:
10
+ - cd spec/dummy/
11
+ - RAILS_ENV=test bundle exec rake db:migrate
12
+ - cd ../../
13
+ - bundle exec rspec spec/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in liquidize.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Alexander Borovykh
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.
@@ -0,0 +1,85 @@
1
+ # Liquidize
2
+
3
+ [![Build Status](https://travis-ci.org/ImmaculatePine/liquidize.svg?branch=master)](https://travis-ci.org/ImmaculatePine/liquidize)
4
+ [![Code Climate](https://codeclimate.com/github/ImmaculatePine/liquidize/badges/gpa.svg)](https://codeclimate.com/github/ImmaculatePine/liquidize)
5
+
6
+ Ruby library that adds [Liquid](http://liquidmarkup.org) template language support to your project.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'liquidize'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ ## Usage
19
+
20
+ Include `Liquidize::Model` mixin to your model and specify liquidized attribute:
21
+
22
+ class Page
23
+ include Liquidize::Model
24
+ attr_accessor :body
25
+ liquidize :body
26
+ end
27
+
28
+ Now you can set the body and render it with any options:
29
+
30
+ page = Page.new
31
+ page.body = "Hello, {{username}}!"
32
+ page.render_body(username: 'John') # => "Hello, John!"
33
+
34
+ Liquid works much faster if once parsed template is cached. Just add `liquid_*` pair attribute and Liquidize will use it to store dump of parsed template.
35
+
36
+ class Page
37
+ # ...
38
+ attr_accessor :body, :liquid_body
39
+ # ...
40
+ end
41
+
42
+ ### With ActiveRecord
43
+
44
+ Liquidize works the same way with ActiveRecord models. The only difference is that parsed template dump will be automatically saved before rendering if your model responds to `liquid_*` attribute.
45
+
46
+ rails g model Email message:text liquid_message:text
47
+ rake db:migrate
48
+
49
+ Liquidize it:
50
+
51
+ class Email < ActiveRecord::Base
52
+ include Liquidize::Model
53
+ liquidize :message
54
+ end
55
+
56
+ Use it the same way:
57
+
58
+ email = Email.create(message: 'How are you doing, {{who}}?')
59
+ email.render_message(who: 'friend') # parses template before rendering
60
+ # => "How are you doing, friend?"
61
+
62
+ # Now it won't parse template until message will be changed
63
+ reloaded_email = Email.find(email.id)
64
+ reloaded_email.render_message(who: 'sir') # does not parse it. Even after reload.
65
+ # => "How are you doing, sir?"
66
+
67
+ reloaded_email.message = 'Oops, I changed the message!'
68
+ reloaded_email.render_message # parses template again
69
+ # => "Oops, I changed the message!"
70
+
71
+ It makes record invalid if there is syntax error in your liquid template:
72
+
73
+ # email.message = "Hey, {{username, I think, there is an error."
74
+ # email.valid? # => false
75
+
76
+ # email.message = "Hey, {{username}}, everything is ok now."
77
+ # email.valid? # => true
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it ( https://github.com/[my-github-username]/liquidize/fork )
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,6 @@
1
+ require 'liquidize/version'
2
+ require 'liquidize/helper'
3
+ require 'liquidize/model'
4
+
5
+ module Liquidize
6
+ end
@@ -0,0 +1,14 @@
1
+ module Liquidize
2
+ module Helper
3
+ # Converts all keys to strings
4
+ # @params options [Hash] hash which keys should be stringified
5
+ # @return [Hash] the same hash with stringified keys
6
+ def self.recursive_stringify_keys(options)
7
+ if options.is_a?(Hash)
8
+ options.stringify_keys!
9
+ options.each { |k, v| recursive_stringify_keys(v) }
10
+ end
11
+ options
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,100 @@
1
+ require 'liquid'
2
+
3
+ module Liquidize
4
+ module Model
5
+
6
+ def self.included(base)
7
+ base.extend ClassMethods
8
+ end
9
+
10
+ module ClassMethods
11
+
12
+ # Adds Liquid support to the following attribute
13
+ # @param attribute [String, Symbol] attribute to be liquidized
14
+ def liquidize(attribute)
15
+ define_liquid_template_method(attribute)
16
+ define_parse_liquid_method(attribute)
17
+ define_render_method(attribute)
18
+ override_setter(attribute)
19
+ if defined?(ActiveRecord) && self.ancestors.include?(ActiveRecord::Base)
20
+ define_validator(attribute)
21
+ validate "validate_#{attribute}_liquid_syntax"
22
+ end
23
+ end
24
+
25
+ private
26
+
27
+ def define_parse_liquid_method(attribute)
28
+ define_method "parse_liquid_#{attribute}!" do
29
+ begin
30
+ parsed_value = Liquid::Template.parse(body)
31
+ instance_variable_set("@liquid_#{attribute}_template", parsed_value)
32
+ if activerecord? && respond_to?("liquid_#{attribute}")
33
+ send("liquid_#{attribute}=", Base64.strict_encode64(Marshal.dump(parsed_value)))
34
+ end
35
+ rescue Liquid::SyntaxError => error
36
+ instance_variable_set("@#{attribute}_syntax_error", error.message)
37
+ end
38
+ end
39
+ end
40
+
41
+ def define_liquid_template_method(attribute)
42
+ define_method "liquid_#{attribute}_template" do
43
+ result = instance_variable_get("@liquid_#{attribute}_template")
44
+ return result unless result.nil?
45
+
46
+ if respond_to?("liquid_#{attribute}") && send("liquid_#{attribute}").present?
47
+ dump = send("liquid_#{attribute}")
48
+ decoded_template = Marshal.load(Base64.strict_decode64(dump))
49
+ instance_variable_set("@liquid_#{attribute}_template", decoded_template)
50
+ else
51
+ send("parse_#{attribute}_template!")
52
+ save if activerecord?
53
+ end
54
+
55
+ instance_variable_get("@liquid_#{attribute}_template")
56
+ end
57
+ end
58
+
59
+ def define_render_method(attribute)
60
+ define_method "render_#{attribute}" do |options = {}|
61
+ send("liquid_#{attribute}_template").render(Liquidize::Helper.recursive_stringify_keys(options))
62
+ end
63
+ end
64
+
65
+ def define_validator(attribute)
66
+ define_method("validate_#{attribute}_liquid_syntax") do
67
+ syntax_error = instance_variable_get("@#{attribute}_syntax_error")
68
+ errors.add(attribute, syntax_error) if syntax_error.present?
69
+ end
70
+ end
71
+
72
+ def override_setter(attribute)
73
+ # Undefine old method to prevent warning
74
+ undef_method "#{attribute}=".to_sym if method_defined? "#{attribute}="
75
+
76
+ # Define new method
77
+ define_method "#{attribute}=" do |value|
78
+ # Set *_syntax_error instance variable to nil because:
79
+ # * old value could be invalid, but the new one is valid
80
+ # * it prevents warning
81
+ instance_variable_set("@#{attribute}_syntax_error", nil)
82
+ if activerecord?
83
+ write_attribute(attribute, value)
84
+ else
85
+ instance_variable_set("@#{attribute}", value)
86
+ end
87
+ send("parse_liquid_#{attribute}!")
88
+ value
89
+ end
90
+ end
91
+ end
92
+
93
+ private
94
+
95
+ def activerecord?
96
+ defined?(ActiveRecord) && self.class.ancestors.include?(ActiveRecord::Base)
97
+ end
98
+
99
+ end
100
+ end
@@ -0,0 +1,3 @@
1
+ module Liquidize
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'liquidize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'liquidize'
8
+ spec.version = Liquidize::VERSION
9
+ spec.authors = ['Alexander Borovykh']
10
+ spec.email = ['immaculate.pine@gmail.com']
11
+ spec.summary = 'Ruby library that adds Liquid template language support to your project.'
12
+ spec.description = 'Ruby library that adds Liquid template language support to your project.'
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.required_ruby_version = '>= 1.9.3'
22
+
23
+ spec.add_dependency 'liquid'
24
+
25
+ spec.add_development_dependency 'bundler', "~> 1.6"
26
+ spec.add_development_dependency 'rake'
27
+ spec.add_development_dependency 'rails', '>= 4.0.0'
28
+ spec.add_development_dependency 'sqlite3'
29
+ spec.add_development_dependency 'rspec'
30
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
File without changes
File without changes
@@ -0,0 +1,5 @@
1
+ class NonActiveRecordPage
2
+ include Liquidize::Model
3
+ attr_accessor :body
4
+ liquidize :body
5
+ end
@@ -0,0 +1,4 @@
1
+ class Page < ActiveRecord::Base
2
+ include Liquidize::Model
3
+ liquidize :body
4
+ end