dome 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: 80ed0f4bad448edf1fbdb8b684d76e9d2e12297a
4
+ data.tar.gz: 4383ce30c9dacbba4d4b9664f9e659085250323e
5
+ SHA512:
6
+ metadata.gz: ae640440aa3d1a12cba2139e655d27d72c725a79732c3d1034f37085b3525ff085bd3384c2f0bd9333f156191ceb12bbb5546c99fc0c5afaa8dec2e6b39a11bb
7
+ data.tar.gz: bd7cc6acab9fb1c365281d66563d963ea96ebe11f16412917df17b27e8884e528f3508d686def80754843479c59ac418bf2f54dd0675526a27bdf71a8cef8c67
data/.gitignore ADDED
@@ -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
+
24
+ .ruby-version
25
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dome.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dmitry Novikov
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
+ # Dome
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'dome'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install dome
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/[my-github-username]/dome/fork )
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 a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/dome.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dome/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dome"
8
+ spec.version = Dome::VERSION
9
+ spec.authors = ["Dmitry Novikov"]
10
+ spec.email = ["glowinglens@gmail.com"]
11
+ spec.summary = %q{View models, presenters and exhibits.}
12
+ spec.description = %q{}
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.add_dependency "activesupport"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end
@@ -0,0 +1,7 @@
1
+ module Dome
2
+ class PresenterClassNotFound < RuntimeError
3
+ end
4
+
5
+ class ExhibitClassNotFound < RuntimeError
6
+ end
7
+ end
@@ -0,0 +1,23 @@
1
+ module Dome
2
+ class Exhibit < SimpleDelegator
3
+ attr_reader :model
4
+ delegate :id, to: :model
5
+
6
+ def initialize(model)
7
+ @model = model.freeze
8
+ super(model)
9
+ end
10
+
11
+ def self.view_path
12
+ "app/exhibits"
13
+ end
14
+
15
+ def class
16
+ model.class
17
+ end
18
+
19
+ def to_model
20
+ self
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ module Dome
2
+ class Presenter
3
+ attr_reader :values
4
+
5
+ class_attribute :presenter_value_names
6
+ self.presenter_value_names = []
7
+
8
+ def self.assign(*names)
9
+ self.presenter_value_names = names
10
+ end
11
+
12
+ def initialize(*vals)
13
+ @values = vals.freeze
14
+ presenter_value_names.each_with_index do |name, i|
15
+ self.class.define_method(name) { @values[i] }
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ module Dome
2
+ class Railtie < Rails::Railtie
3
+ initializer "action_view.initialize_dome" do
4
+ ActiveSupport.on_load(:action_view) do
5
+ ActionView::Base.send :include, Dome::ViewMethods
6
+ end
7
+ ActiveSupport.on_load(:action_controller) do
8
+ ActionView::Base.send :extend, Dome::ControllerClassMethods
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module Dome
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require 'active_support/inflector'
2
+
3
+ module Dome
4
+ module ViewMethods
5
+ # view_model(:var_name)
6
+ # view_model(:dashboard) { UnitDashboard.new(@meeting) }
7
+ def view_model(name, to: dome_view_context, &block)
8
+ if block_given?
9
+ to.class.send(:define_method, name, &block)
10
+ else
11
+ to.class.send(:define_method, name) do
12
+ to.instance_variable_get("@#{name}")
13
+ end
14
+ end
15
+ to.send(name)
16
+ end
17
+
18
+ # present(:pair, @one, @two)
19
+ def present(name, *vals, to: dome_view_context)
20
+ class_name = "#{name.to_s.camelize}Presenter"
21
+ klass = class_name.safe_constantize
22
+ klass || raise(
23
+ Dome::PresenterClassNotFound,
24
+ "Presenter class '#{class_name}' for '#{name}' not found."
25
+ )
26
+
27
+ to.class.send(:define_method, name) do
28
+ to.instance_variable_get("@#{name}") || begin
29
+ presented = klass.new(*vals)
30
+ presented.instance_variable_set("@view_context", to)
31
+ to.instance_variable_set("@#{name}", presented)
32
+ end
33
+ end
34
+ to.send(name)
35
+ end
36
+
37
+ # exhibit(@one)
38
+ # exhibit(@one, by: SomeEx)
39
+ def exhibit(thing, by: nil, to: dome_view_context)
40
+ klass = by || begin
41
+ class_name = "#{thing.class.name}Exhibit"
42
+ klass = class_name.safe_constantize
43
+ klass || raise(
44
+ Dome::ExhibitClassNotFound,
45
+ "Exhibit class '#{class_name}' for '#{thing.inspect}' not found."
46
+ )
47
+ end
48
+
49
+ exhibited = klass.new(thing)
50
+ exhibited.instance_variable_set("@view_context", to)
51
+ exhibited
52
+ end
53
+
54
+ # exhibit_enum(@ones)
55
+ # exhibit_enum(@ones, by: SomeEx)
56
+ def exhibit_enum(things, by: nil, to: dome_view_context)
57
+ klass = by || begin
58
+ class_name = "#{things.first.class.name}Exhibit"
59
+ klass = class_name.safe_constantize
60
+ klass || raise(
61
+ Dome::ExhibitClassNotFound,
62
+ "Exhibit class '#{class_name}' for '#{things.first.inspect}' not found."
63
+ )
64
+ end
65
+
66
+ exhibited_enum = things.map do |x|
67
+ exhibited = klass.new(x)
68
+ exhibited.instance_variable_set("@view_context", to)
69
+ exhibited
70
+ end
71
+ exhibited_enum
72
+ end
73
+
74
+ private
75
+
76
+ def dome_view_context
77
+ @dome_view_context ||= respond_to?(:view_context) ? view_context : self
78
+ end
79
+ end
80
+ end
data/lib/dome.rb ADDED
@@ -0,0 +1,12 @@
1
+ require "dome/version"
2
+
3
+ require 'active_support/core_ext'
4
+
5
+ require 'dome/presenter'
6
+ require 'dome/exhibit'
7
+
8
+ require 'dome/railtie' if defined?(Rails)
9
+
10
+ module Dome
11
+ # Your code goes here...
12
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'dome'
@@ -0,0 +1,99 @@
1
+ require 'spec_helper'
2
+
3
+ require 'dome/view_methods'
4
+ require 'dome/errors'
5
+
6
+ describe 'ViewMethods' do
7
+ subject do
8
+ Class.new do
9
+ include Dome::ViewMethods
10
+
11
+ def initialize
12
+ @some_var = 123
13
+ @words = ["abc", "xyz"]
14
+ end
15
+ end.new
16
+ end
17
+
18
+ describe "#view_model" do
19
+ it "wraps an instance variable" do
20
+ subject.view_model(:some_var)
21
+ expect(subject.some_var).to eq 123
22
+ end
23
+
24
+ it "defines method from a block" do
25
+ subject.view_model(:some_var) { @some_var * 100 }
26
+ expect(subject.some_var).to eq 12300
27
+ end
28
+
29
+ it "calls a defined method" do
30
+ expect(subject.view_model(:some_var) { @some_var * 100 }).to eq 12300
31
+ end
32
+ end
33
+
34
+ describe "#present" do
35
+ it "raises error if presenter class is absent" do
36
+ expect {
37
+ subject.instance_eval { present(:thing, @some_var) }
38
+ }.to raise_error(Dome::PresenterClassNotFound)
39
+ end
40
+
41
+ it "defines a method returning a ThingPresenter instance" do
42
+ class ThingPresenter < Dome::Presenter; end
43
+ subject.instance_eval { present(:thing, @some_var) }
44
+ expect(subject.thing).to be_a ThingPresenter
45
+ end
46
+
47
+ it "sets @view_context variable to subject" do
48
+ class ThingPresenter < Dome::Presenter; end
49
+ subject.instance_eval { present(:thing, @some_var) }
50
+ expect(
51
+ subject.thing.instance_variable_get(:@view_context)
52
+ ).to eq subject
53
+ end
54
+
55
+ it "calls a defined method" do
56
+ class ThingPresenter < Dome::Presenter; end
57
+ expect(
58
+ subject.instance_eval { present(:thing, @some_var) }
59
+ ).to be_a ThingPresenter
60
+ end
61
+ end
62
+
63
+ describe "#exhibit" do
64
+ it "raises error if exhibit class is absent" do
65
+ expect {
66
+ subject.instance_eval { exhibit(@some_var) }
67
+ }.to raise_error(Dome::ExhibitClassNotFound)
68
+ end
69
+
70
+ it "returns a FixnumExhibit instance" do
71
+ class FixnumExhibit < Dome::Exhibit; end
72
+ expect(
73
+ subject.instance_eval { exhibit(@some_var) }
74
+ ).to be_a FixnumExhibit
75
+ end
76
+
77
+ it "sets @view_context variable to subject" do
78
+ class FixnumExhibit < Dome::Exhibit; end
79
+ expect(
80
+ subject.instance_eval { exhibit(@some_var) }.instance_variable_get(:@view_context)
81
+ ).to eq subject
82
+ end
83
+ end
84
+
85
+ describe "#exhibit_enum" do
86
+ it "raises error if exhibit class is absent" do
87
+ expect {
88
+ subject.instance_eval { exhibit_enum(@words) }
89
+ }.to raise_error(Dome::ExhibitClassNotFound)
90
+ end
91
+
92
+ it "returns a FixnumExhibit instances" do
93
+ class StringExhibit < Dome::Exhibit; end
94
+ ary = subject.instance_eval { exhibit_enum(@words) }
95
+ expect(ary.first).to be_a StringExhibit
96
+ expect(ary.second).to be_a StringExhibit
97
+ end
98
+ end
99
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dmitry Novikov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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.6'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
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
+ - !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
+ description: ''
70
+ email:
71
+ - glowinglens@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - dome.gemspec
83
+ - lib/dome.rb
84
+ - lib/dome/errors.rb
85
+ - lib/dome/exhibit.rb
86
+ - lib/dome/presenter.rb
87
+ - lib/dome/railtie.rb
88
+ - lib/dome/version.rb
89
+ - lib/dome/view_methods.rb
90
+ - spec/spec_helper.rb
91
+ - spec/view_methods_spec.rb
92
+ homepage: ''
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.2.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: View models, presenters and exhibits.
116
+ test_files:
117
+ - spec/spec_helper.rb
118
+ - spec/view_methods_spec.rb