maskara 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 312ad3120985a3b155234751c812c546877f7c32
4
+ data.tar.gz: 095b5ba4d5fc4f0be2f43d883e4f0993209c42b1
5
+ SHA512:
6
+ metadata.gz: 1d815270f0ac3935cabeb4830d0ca220e042dae24ed5e1cadfb11dd35730513ac5cb3511dd3663cd7ece3921a00240d19c6585ab02e593dfbbae4ab1915ebecc
7
+ data.tar.gz: 8b4585e19742398ea7cb993794f9243ad283ad2bed0e9b05500413a52302ce651f23dfec63def293e97c23f6ed2e1fba7b4dc27b38f997d0951ca1eaf340213c
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /spec/dummy/tmp/*
11
+ /spec/dummy/log/*
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ maskara
@@ -0,0 +1 @@
1
+ ruby-2.2.1
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in maskara.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Fairfax Media
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Maskara
2
+
3
+ Maskara is designed to take the pain out of frontend development (primarily for Rails, but it tries to be platform agnostic.)
4
+
5
+ It encourages developers and frontend designers to collaboratively mock out actions with fixture data, and then to use special URLs to access those actions - this allows easier testing of heavy (or incomplete) actions, and quick prototyping, with the entire Rails view stack still available.
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'maskara'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install maskara
23
+
24
+
25
+ ## Usage
26
+
27
+ Generate a default YAML fixture file with the rake command:
28
+
29
+ ```
30
+ rake maskara:generate
31
+ ```
32
+
33
+ (The file defaults to RAILS_ROOT/db/fixtures/maskara.yml)
34
+
35
+ Edit the file with information about the objects your template needs (and filters you wish to disable). ([The example in the test directory](spec/dummy/db/fixtures/maskara.yml) demonstrates the sort of options that are available.)
36
+
37
+ Maskara can use Rack middleware to identify maskara requests (this is the easiest way to employ it) so here's how you include it in your application.rb:
38
+
39
+ ```
40
+ config.middleware.insert_after Rails::Rack::Logger, Maskara::Middleware
41
+ ```
42
+
43
+ Then, after restarting Rails, prepend `/maskara/` to the path you'd normally use to access the view: eg.
44
+
45
+ ```http://localhost:3000/member/edit/1```
46
+
47
+ would become...
48
+
49
+ ```http://localhost:3000/maskara/member/edit/1```
50
+
51
+ Instead of running the controller action, the data form the fixture file should be loaded into the controller and a normal render call executed.
52
+
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://bitbucket.org/simon_hildebrandt/maskara.
63
+
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require 'rails/dummy/tasks'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "maskara"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+ require "maskara/data"
2
+ require "maskara/middleware"
3
+ require "maskara/version"
4
+
5
+ require 'maskara/railtie' if defined?(Rails)
6
+
7
+ module Maskara
8
+ class Configuration
9
+ attr_accessor :data_path
10
+
11
+ def data_path
12
+ @data_path or fail NotConfiguredError.new("Maskara needs to be configured with a data_path to find #{data_file_name}")
13
+ end
14
+
15
+ def data_file
16
+ File.join(data_path, data_file_name)
17
+ end
18
+
19
+ def data_file_name
20
+ 'maskara.yml'
21
+ end
22
+ end
23
+
24
+ class << self
25
+ attr_writer :configuration
26
+
27
+ def configuration
28
+ @configuration ||= Configuration.new
29
+ end
30
+
31
+ def configure
32
+ yield configuration
33
+ end
34
+
35
+ def path_stub
36
+ 'maskara'
37
+ end
38
+ end
39
+
40
+ class NotConfiguredError < StandardError; end
41
+ end
@@ -0,0 +1,44 @@
1
+ module Maskara
2
+ class Data
3
+ class << self
4
+ def data_file
5
+ Maskara.configuration.data_file
6
+ end
7
+
8
+ def data
9
+ @data ||= load_data || {}
10
+ end
11
+
12
+ def load_data
13
+ begin
14
+ return YAML.load(open(data_file).read)
15
+ rescue Errno::ENOENT
16
+ puts "Coundn't find Maskara data file at #{data_file}"
17
+ rescue Psych::SyntaxError
18
+ puts "Invalid YAML found in Maskara data file at #{data_file}"
19
+ end
20
+ {}
21
+ end
22
+
23
+ def controllers_data
24
+ data.fetch('controllers', {})
25
+ end
26
+
27
+ def controller_data(controller)
28
+ controllers_data.fetch(controller, {})
29
+ end
30
+
31
+ def action_data(action, controller)
32
+ controller_data(controller).fetch('actions', {}).fetch(action, {})
33
+ end
34
+
35
+ def skip_filters
36
+ controllers_data.collect do |controller, config|
37
+ unless config['skip_filters'].blank?
38
+ ["#{controller.classify}Controller".constantize, config['skip_filters'].map(&:to_sym)]
39
+ end
40
+ end.compact
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,59 @@
1
+ module Maskara
2
+ class Generator
3
+ class << self
4
+ def run
5
+ fail("#{data_file} already exists.") if File.exists?(data_file)
6
+
7
+ # In development Rails doesn't load controllers until they're referenced
8
+ eager_load_controllers
9
+ write_example_data_file
10
+ end
11
+
12
+ private
13
+
14
+ def data_file
15
+ Maskara.configuration.data_file
16
+ end
17
+
18
+ def write_example_data_file
19
+ FileUtils.mkdir_p(Maskara.configuration.data_path)
20
+
21
+ File.open(data_file, 'w') do |file|
22
+ YAML.dump(clean_controller_data, file)
23
+ end
24
+ end
25
+
26
+ def clean_controller_data
27
+ { 'controllers' => dump_controllers }
28
+ end
29
+
30
+ def dump_controllers
31
+ Hash[ controller_list ]
32
+ end
33
+
34
+ def controller_list
35
+ ActionController::Base.descendants.collect do |controller|
36
+ catch(:empty_controller) do
37
+ [ controller.controller_path, action_data(controller) ]
38
+ end
39
+ end.compact
40
+ end
41
+
42
+ def action_data(controller)
43
+ { 'actions' => Hash[ action_list(controller) ] }
44
+ end
45
+
46
+ def action_list(controller)
47
+ throw :empty_controller if controller.action_methods.empty?
48
+
49
+ controller.action_methods.to_a.collect{ |action| [ action, {} ] }
50
+ end
51
+
52
+ def eager_load_controllers
53
+ Dir.glob(Rails.root.join "app/controllers/**/*_controller.rb").each do |file|
54
+ require_dependency file
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,35 @@
1
+ module Maskara
2
+ class Middleware
3
+ def initialize(app)
4
+ @app = app
5
+ end
6
+
7
+ def call(env)
8
+ new_env = env
9
+ if maskara_path? env['PATH_INFO']
10
+ log "Maskara path: #{env['PATH_INFO']}"
11
+ new_env = env.dup
12
+ new_env['MASKARA_REQUEST'] = true
13
+ new_env['REQUEST_URI'] = new_env['PATH_INFO'] = env['PATH_INFO'].sub(maskara_regex, '/')
14
+ new_env['REQUEST_URI'] += "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].nil? || env['QUERY_STRING'].empty?
15
+ end
16
+ @app.call(new_env)
17
+ end
18
+
19
+ def maskara_path? path
20
+ path =~ maskara_regex
21
+ end
22
+
23
+ def maskara_regex
24
+ /\A(\/#{Maskara.path_stub}\/)/
25
+ end
26
+
27
+ def log msg
28
+ if (defined? Rails)
29
+ Rails.logger.debug msg
30
+ else
31
+ puts msg
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,67 @@
1
+ require 'recursive-open-struct'
2
+ require 'maskara/generator'
3
+ require 'maskara/data'
4
+
5
+ # Support adding /maskara into generated routes
6
+ if Rails::VERSION::MAJOR >= 4
7
+ module ActionDispatch::Routing
8
+ class RouteSet::NamedRouteCollection::UrlHelper::OptimizedUrlHelper
9
+ alias_method :call_without_maskara_check, :call
10
+
11
+ def call(t, args, inner_options)
12
+ maskara_prefix(t.controller) + call_without_maskara_check(t, args, inner_options)
13
+ end
14
+
15
+ def maskara_prefix(controller)
16
+ controller.maskara_request? ? "/#{Maskara.path_stub}" : ''
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ # Populate controllers with instance variables for maskara requests
23
+ class AbstractController::Base
24
+ alias_method :process_action_without_maskara_check, :process_action
25
+
26
+ def process_action(method_name, *args)
27
+ if request.env['MASKARA_REQUEST']
28
+ maskara_populate_data(method_name)
29
+ render
30
+ else
31
+ process_action_without_maskara_check(method_name, *args)
32
+ end
33
+ end
34
+
35
+ def maskara_request?
36
+ request.env['MASKARA_REQUEST'] == true
37
+ end
38
+
39
+ def maskara_populate_data(action)
40
+ Maskara::Data.action_data(action, controller_path).each do |key, data|
41
+ instance_variable_set("@#{key}", maskara_presentable_data(data))
42
+ end
43
+ end
44
+
45
+ def maskara_presentable_data(data)
46
+ RecursiveOpenStruct.new(data, :recurse_over_arrays => true)
47
+ end
48
+ end
49
+
50
+ # Initialise maskara and disabled filters
51
+ module Maskara
52
+ class Railtie < Rails::Railtie
53
+ initializer "maskara_railtie.configure_rails_initialization" do
54
+ Maskara.configure do |config|
55
+ config.data_path = Rails.root.join('db', 'fixtures')
56
+ end
57
+
58
+ Maskara::Data.skip_filters.each do |klass, filters|
59
+ filters.each { |filter| klass.skip_filter filter, if: :maskara_request? }
60
+ end
61
+ end
62
+
63
+ rake_tasks do
64
+ load 'maskara/tasks.rake'
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,10 @@
1
+ require 'maskara/generator'
2
+
3
+ namespace :maskara do
4
+
5
+ desc 'Generate a default maskara data file based on the existing controllers'
6
+ task :generate => :environment do
7
+ Maskara::Generator.run
8
+ end
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ module Maskara
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'maskara/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "maskara"
8
+ spec.version = Maskara::VERSION
9
+ spec.authors = ["Simon Hildebrandt"]
10
+ spec.email = ["simon.hildebrandt@fairfaxmedia.com.au"]
11
+
12
+ spec.summary = %q{Mask out your controller logic with mock data to make building views easier.}
13
+ spec.description = %q{Mask out your controller logic with mock data to make building views easier.}
14
+ spec.homepage = "https://bitbucket.org/fairfax/maskara"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'recursive-open-struct'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.10"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "rails-dummy"
28
+ spec.add_development_dependency "sqlite3"
29
+ spec.add_development_dependency "byebug"
30
+ spec.add_development_dependency "rails", "> 3"
31
+
32
+ end
metadata ADDED
@@ -0,0 +1,176 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maskara
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Simon Hildebrandt
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: recursive-open-struct
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails-dummy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">"
116
+ - !ruby/object:Gem::Version
117
+ version: '3'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">"
123
+ - !ruby/object:Gem::Version
124
+ version: '3'
125
+ description: Mask out your controller logic with mock data to make building views
126
+ easier.
127
+ email:
128
+ - simon.hildebrandt@fairfaxmedia.com.au
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".ruby-gemset"
136
+ - ".ruby-version"
137
+ - ".travis.yml"
138
+ - Gemfile
139
+ - LICENSE.txt
140
+ - README.md
141
+ - Rakefile
142
+ - bin/console
143
+ - bin/setup
144
+ - lib/maskara.rb
145
+ - lib/maskara/data.rb
146
+ - lib/maskara/generator.rb
147
+ - lib/maskara/middleware.rb
148
+ - lib/maskara/railtie.rb
149
+ - lib/maskara/tasks.rake
150
+ - lib/maskara/version.rb
151
+ - maskara.gemspec
152
+ homepage: https://bitbucket.org/fairfax/maskara
153
+ licenses:
154
+ - MIT
155
+ metadata: {}
156
+ post_install_message:
157
+ rdoc_options: []
158
+ require_paths:
159
+ - lib
160
+ required_ruby_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ required_rubygems_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ requirements: []
171
+ rubyforge_project:
172
+ rubygems_version: 2.4.6
173
+ signing_key:
174
+ specification_version: 4
175
+ summary: Mask out your controller logic with mock data to make building views easier.
176
+ test_files: []