characterize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 10c960deb342856cf94ab657c4ba6836ff586e04
4
+ data.tar.gz: 7d64cdc96d3994ea7d8ef8cb29d3f38c5c791030
5
+ SHA512:
6
+ metadata.gz: ecaf97dbfeb7db99663c858993b89c62c3eb2c29eaa1e4866c4626fbe11fc2083fc11ef654f8bc474e115f0c53f2d8aae5a40f8005368efc320d3d2e5b851370
7
+ data.tar.gz: 8981402f20455d18a3d44a1f0654384520b650eac8a09179510e1dbda9131cc2c1f467b2ebe45b941ef51329d2ec44c3a323d01ff46b91a2cc47f61e47549bee
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/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in characterize.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'minitest'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 'Jim Gay'
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,61 @@
1
+ # Characterize
2
+
3
+ Make your models behave in special ways.
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ class UsersController < ApplicationController
9
+ characterize :user
10
+
11
+ def show
12
+ end
13
+ end
14
+ # the above sets a helper_method of 'user' and loads UserCharacter
15
+
16
+ class UsersController < ApplicationController
17
+ def show
18
+ characterize(user, display_module)
19
+ end
20
+
21
+ def display_module
22
+ current_user.can_edit?(user) ? AdministratedUser : StandardUser
23
+ end
24
+ end
25
+
26
+ # use a standard interface in your views but change the character of the object
27
+
28
+ module AdministratedUser
29
+ def edit_link
30
+ view.link_to('Edit', admin_user_path)
31
+ end
32
+ end
33
+
34
+ module StandardUser
35
+ def edit_link
36
+ ""
37
+ end
38
+ end
39
+ ```
40
+
41
+ ## Installation
42
+
43
+ Add this line to your application's Gemfile:
44
+
45
+ gem 'characterize'
46
+
47
+ And then execute:
48
+
49
+ $ bundle
50
+
51
+ Or install it yourself as:
52
+
53
+ $ gem install characterize
54
+
55
+ ## Contributing
56
+
57
+ 1. Fork it
58
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
59
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
60
+ 4. Push to the branch (`git push origin my-new-feature`)
61
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'characterize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "characterize"
8
+ spec.version = Characterize::VERSION
9
+ spec.authors = ["'Jim Gay'"]
10
+ spec.email = ["jim@saturnflyer.com"]
11
+ spec.description = %q{Use plain modules like presenters}
12
+ spec.summary = %q{Use plain modules like presenters}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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.add_dependency "casting", "~> 0.6.8"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,73 @@
1
+ require 'characterize/feature_controls'
2
+
3
+ module Characterize
4
+ module Controller
5
+ def self.included(klass)
6
+ klass.extend(::Characterize::ControllerMacros)
7
+ end
8
+
9
+ private
10
+ def characterize(obj, *mods)
11
+ obj.__set_view__(view_context).cast_as(*mods)
12
+ obj
13
+ end
14
+ end
15
+
16
+ module ControllerMacros
17
+
18
+ private
19
+
20
+ def characterize(*symbols_strings_or_classes)
21
+ options = symbols_strings_or_classes.empty? ? [self] : symbols_strings_or_classes
22
+ options.map do |identifier|
23
+ characterize_item = Converter.new(identifier)
24
+ object_name = characterize_item.to_object_name
25
+ mod = Module.new
26
+ mod.module_eval %{
27
+ def #{object_name}
28
+ @#{object_name} ||= characterize(load_#{object_name}, *characterize_#{object_name}_modules)
29
+ end
30
+
31
+ def load_#{object_name}
32
+ #{characterize_item.to_constant_name}.find(params[:id])
33
+ end
34
+
35
+ def characterize_#{object_name}_modules
36
+ [::Characterize::FeatureControls] + #{object_name}_modules
37
+ end
38
+
39
+ def #{object_name}_modules
40
+ [#{object_name}_default_character]
41
+ end
42
+
43
+ def #{object_name}_default_character
44
+ #{Converter.new(object_name).to_constant_name}Character
45
+ end
46
+ }
47
+ self.const_set(characterize_item.to_constant_name + 'ControllerMethods', mod)
48
+ include mod
49
+ self.send(:helper_method, object_name)
50
+ end
51
+ end
52
+
53
+ class Converter
54
+ def initialize(string)
55
+ @string = string.to_s
56
+ end
57
+
58
+ def to_object_name
59
+ to_path.split('/').last
60
+ end
61
+
62
+ def to_path
63
+ # TODO: get platform-specific path delimiter
64
+ @string.gsub('::','/').gsub(/([A-Z])/){ "_#{$1.downcase}" }.gsub(/^_|\/_/,'/').sub(/^\//,'')
65
+ end
66
+
67
+ def to_constant_name
68
+ @string.gsub(/(?:^|_)([a-z])/){ $1.upcase }.gsub('/','::')
69
+ # TODO: get platform-specific path delimiter
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,83 @@
1
+ module Characterize
2
+ module FeatureControls
3
+
4
+ # Enumerate a collection with the given modules and block casting
5
+ # each object with the modules and uncasting the object afterward.
6
+ #
7
+ # Examples:
8
+ #
9
+ # def each_favorite(&block)
10
+ # each_with_feature(favorites, FavoriteMod, &block)
11
+ # end
12
+ #
13
+ # <%- user.each_favorite do |favorite| %>
14
+ # <%= favorite.special_feature %>
15
+ # <%- end -%>
16
+ #
17
+ def each_with_features(collection, *mods, &block)
18
+ collection.lazy.each do |obj|
19
+ obj.cast_as(*mods)
20
+ block.call(obj)
21
+ obj.uncast(mods.size)
22
+ end
23
+ end
24
+
25
+ # Conditionally render content for the object.
26
+ #
27
+ # Pass in a method name for content and either of:
28
+ # 1. options for Rails' content_tag
29
+ # 2. a block to render
30
+ #
31
+ # Examples:
32
+ #
33
+ # <%= user.with(:favorites, :p, class: 'favorites-details') %>
34
+ # <%- user.with(:favorites) do %>
35
+ # <p class="favorites-details">
36
+ # My Favorite Things: <%= user.favorites.join(', ') %>
37
+ # </p>
38
+ # <%- end -%>
39
+ #
40
+ def with(method_name, *tag_name_or_options)
41
+ value = self.public_send(method_name)
42
+ if with_conditions(method_name, value)
43
+ if block_given?
44
+ yield
45
+ else
46
+ tag_name, options = *tag_name_or_options
47
+ view.content_tag(tag_name, value, options)
48
+ end
49
+ else
50
+ without_option = tag_name_or_options.last.fetch(:without){ '' }
51
+ if without_option.respond_to?(:call)
52
+ without(method_name, &without_option)
53
+ else
54
+ view.concat(without_option)
55
+ end
56
+ end
57
+ end
58
+
59
+ # Conditionally render content for the object when the attribute is NOT present.
60
+ #
61
+ # Pass in a method name and a block to render.
62
+ #
63
+ # Examples:
64
+ #
65
+ # <%- user.without(:favorites) do %>
66
+ # <p class="favorites-details none">
67
+ # There are no favorites here.
68
+ # </p>
69
+ # <%- end -%>
70
+ #
71
+ def without(method_name, &block)
72
+ value = self.public_send(method_name)
73
+ if !with_conditions(method_name, value)
74
+ yield
75
+ end
76
+ end
77
+
78
+ # Used to override behavior of with for the case of special attributes
79
+ def with_conditions(method_name, computed_value)
80
+ !computed_value.nil? && computed_value != '' && computed_value != false
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module Characterize
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'forwardable'
2
+ module Characterize
3
+ module ViewForwards
4
+ extend Forwardable
5
+
6
+ delegate [*ActionView::Helpers.constants.map{ |name|
7
+ ActionView::Helpers.const_get(name)
8
+ }.map(&:instance_methods).flatten] => :__view__
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ require "characterize/version"
2
+ require "characterize/controller"
3
+ require "casting"
4
+
5
+ module Characterize
6
+ def self.included(klass)
7
+ klass.class_eval {
8
+ include Casting::Client
9
+ delegate_missing_methods
10
+ }
11
+ end
12
+
13
+ def viewable?
14
+ !!view
15
+ end
16
+
17
+ def view
18
+ @view
19
+ end
20
+
21
+ def __set_view__(obj)
22
+ @view = obj
23
+ self
24
+ end
25
+ end
26
+
27
+ require 'active_record'
28
+ class ActiveRecord::Base
29
+ include Characterize
30
+ end
31
+
32
+ require 'action_controller'
33
+ class ActionController::Base
34
+ include Characterize::Controller
35
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: characterize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "'Jim Gay'"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: casting
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.6.8
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.6.8
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.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Use plain modules like presenters
56
+ email:
57
+ - jim@saturnflyer.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".ruby-version"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - characterize.gemspec
69
+ - lib/characterize.rb
70
+ - lib/characterize/controller.rb
71
+ - lib/characterize/feature_controls.rb
72
+ - lib/characterize/version.rb
73
+ - lib/characterize/view_forwards.rb
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.2.0
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Use plain modules like presenters
98
+ test_files: []