smoke_monster 0.2.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.
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,12 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smoke_monster.gemspec
4
+ gemspec
5
+
6
+
7
+ gem 'minitest'
8
+ group :development do
9
+ gem 'guard'
10
+ gem 'guard-minitest', :git => 'git://github.com/aspiers/guard-minitest.git'
11
+ gem 'ruby_gntp'
12
+ end
data/Guardfile ADDED
@@ -0,0 +1,12 @@
1
+ guard 'minitest' do
2
+ watch(%r|^test/test_(.*)\.rb|)
3
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "test/#{m[1]}test_#{m[2]}.rb" }
4
+ watch(%r|^test/test_helper\.rb|) { "test" }
5
+ watch(%r|^lib/(.*)\.rb|) { |m| "test/test_#{m[1]}.rb" }
6
+
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch(%r{^lib/smoke_monster/(.+)\.rb$}) { |m| "spec/smoke_monster/#{m[1]}_spec.rb" }
10
+ watch(%r{^spec/models/.+\.rb$}) { ["spec/models", "spec/acceptance"] }
11
+ watch('spec/spec_helper.rb') { "spec" }
12
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Darren Cauthon
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
+ # SmokeMonster
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'smoke_monster'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install smoke_monster
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ task :default => [:test]
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs.push "lib"
8
+ t.test_files = FileList['spec/smoke_monster/*_spec.rb']
9
+ t.verbose = true
10
+ end
@@ -0,0 +1,5 @@
1
+ Dir[File.dirname(__FILE__) + '/smoke_monster/*.rb'].each {|file| require file }
2
+
3
+ module SmokeMonster
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,32 @@
1
+ class Array
2
+
3
+ def to_objects(&blk)
4
+ records = blk.call
5
+ return [] if records.empty?
6
+ records.map{ |record| create_object_for_this_record(record) }
7
+ end
8
+
9
+ private
10
+
11
+ def create_object_for_this_record(record)
12
+ result = Object.new
13
+ self.each_with_index do |property_name, index|
14
+ value = get_the_value(record, index)
15
+ add_reader_for(result, property_name, value)
16
+ end
17
+ result
18
+ end
19
+
20
+ def get_the_value(value, index)
21
+ return value unless value.kind_of?(Array)
22
+ value[index]
23
+ end
24
+
25
+ def add_reader_for(result, property_name, this_value)
26
+ result.instance_variable_set("@#{property_name}", this_value)
27
+ result.instance_eval("
28
+ class << self
29
+ attr_accessor :#{property_name}
30
+ end")
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module SmokeMonster
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ module SmokeMonster
2
+ class ViewModel
3
+ def initialize(subject)
4
+ @subject = subject
5
+ end
6
+
7
+ def method_missing(meth, *args, &blk)
8
+ @subject.send(meth, *args, &blk)
9
+ rescue
10
+ SmokeMonster::ViewModel.new nil
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,17 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/smoke_monster/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Darren Cauthon"]
6
+ gem.email = ["darren@cauthon.com"]
7
+ gem.description = %q{Unexplained, dark magic.}
8
+ gem.summary = %q{Unexplained, dark magic.}
9
+ gem.homepage = "http://www.github.com/darrencauthon/smoke_monster"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "smoke_monster"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SmokeMonster::VERSION
17
+ end
@@ -0,0 +1,99 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe "arrays to objects" do
4
+
5
+ [:name, :street].each do |property|
6
+ describe "with an array with one symbol named #{property.to_s}" do
7
+ describe "and no data" do
8
+ before do
9
+ @values = [property].to_objects { [] }
10
+ end
11
+
12
+ it "should return an empty array" do
13
+ @values.must_be_empty
14
+ end
15
+ end
16
+
17
+ ["Dangy", "Taggart"].each do |name|
18
+ describe "and one record with #{name}" do
19
+ before do
20
+ @values = [property].to_objects { [name] }
21
+ end
22
+
23
+ it "should return one record" do
24
+ @values.count.must_equal 1
25
+ end
26
+
27
+ it "should have a property with the name of Dangy" do
28
+ @values[0].send(property).must_equal name
29
+ end
30
+ end
31
+ end
32
+
33
+ describe "and two records with John and Howard" do
34
+ before do
35
+ @values = [property].to_objects {["John", "Howard"]}
36
+ end
37
+
38
+ it "should return two records" do
39
+ @values.count.must_equal 2
40
+ end
41
+
42
+ it "should set the first to John" do
43
+ @values[0].send(property).must_equal "John"
44
+ end
45
+
46
+ it "should set the second to Howard" do
47
+ @values[1].send(property).must_equal "Howard"
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "with an array with two symbols named first_name and last_name" do
53
+ describe "with 'Ellis' and 'Wyatt'" do
54
+ before do
55
+ @values = [:first_name, :last_name].to_objects { [['Ellis', 'Wyatt']] }
56
+ end
57
+
58
+ it "should return one record" do
59
+ @values.count.must_equal 1
60
+ end
61
+
62
+ it "should set the first name to Ellis" do
63
+ @values[0].first_name.must_equal 'Ellis'
64
+ end
65
+
66
+ it "should set the last name to Wyatt" do
67
+ @values[0].last_name.must_equal 'Wyatt'
68
+ end
69
+ end
70
+
71
+ describe "with 'Ellis' and 'Wyatt', then 'Dagny' and 'Taggart'" do
72
+ before do
73
+ @values = [:first_name, :last_name].to_objects { [['Ellis', 'Wyatt'], ['Dagny', 'Taggart']] }
74
+ end
75
+
76
+ it "should return one record" do
77
+ @values.count.must_equal 2
78
+ end
79
+
80
+ it "should set the first name to Ellis on the first record" do
81
+ @values[0].first_name.must_equal 'Ellis'
82
+ end
83
+
84
+ it "should set the last name to Wyatt on the first record" do
85
+ @values[0].last_name.must_equal 'Wyatt'
86
+ end
87
+
88
+ it "should set the first name to Dagny on the second record" do
89
+ @values[1].first_name.must_equal 'Dagny'
90
+ end
91
+
92
+ it "should set the last name to Wyatt on the second record" do
93
+ @values[1].last_name.must_equal 'Taggart'
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ end
@@ -0,0 +1,82 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe SmokeMonster::ViewModel do
4
+ before do
5
+ end
6
+
7
+ describe "when given an Object" do
8
+ before do
9
+ @view_model = SmokeMonster::ViewModel.new Object.new
10
+ end
11
+
12
+ it "should not throw an exception on methods that do not exist" do
13
+ @view_model.not_this
14
+ @view_model.or_this_either
15
+ end
16
+
17
+ it "should return a view model if the method does not exist" do
18
+ @view_model.should_be_a_view_model.must_be_instance_of SmokeMonster::ViewModel
19
+ @view_model.another_test.must_be_instance_of SmokeMonster::ViewModel
20
+ end
21
+
22
+ it "should return new view models each time on methods that do not exist" do
23
+ first = @view_model.one_call
24
+ second = @view_model.second_call
25
+
26
+ first.object_id.wont_equal @view_model.object_id
27
+ second.object_id.wont_equal @view_model.object_id
28
+ first.object_id.wont_equal second.object_id
29
+ end
30
+ end
31
+
32
+ describe "when given a Person object" do
33
+ class Person
34
+ attr_accessor :first_name, :last_name
35
+ end
36
+
37
+ before do
38
+ @person = Person.new
39
+ @view_model = SmokeMonster::ViewModel.new @person
40
+ end
41
+
42
+ [["John", "Galt"], ["Howard", "Roark"]].each do |name|
43
+ describe "with name of #{name[0]} #{name[1]}" do
44
+ before do
45
+ @person.first_name = name[0]
46
+ @person.last_name = name[1]
47
+ end
48
+
49
+ it "should return John as the first name" do
50
+ @view_model.first_name.must_equal name[0]
51
+ end
52
+
53
+ it "should return Galt as the last name" do
54
+ @view_model.last_name.must_equal name[1]
55
+ end
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "when given an object that uses a block" do
61
+ class Thing
62
+ class << self
63
+ attr_accessor :test_value
64
+ end
65
+ def test(&blk)
66
+ blk.call
67
+ end
68
+ end
69
+
70
+ before do
71
+ @thing = Thing.new
72
+ @view_model = SmokeMonster::ViewModel.new @thing
73
+ end
74
+
75
+ it "should call the block" do
76
+ @view_model.test do
77
+ Thing.test_value = true
78
+ end
79
+ Thing.test_value.must_equal true
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+ require './lib/smoke_monster.rb'
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smoke_monster
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Darren Cauthon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Unexplained, dark magic.
15
+ email:
16
+ - darren@cauthon.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Guardfile
24
+ - LICENSE
25
+ - README.md
26
+ - Rakefile
27
+ - lib/smoke_monster.rb
28
+ - lib/smoke_monster/array_to_object.rb
29
+ - lib/smoke_monster/version.rb
30
+ - lib/smoke_monster/view_model.rb
31
+ - smoke_monster.gemspec
32
+ - spec/smoke_monster/array_to_object_spec.rb
33
+ - spec/smoke_monster/view_model_spec.rb
34
+ - spec/spec_helper.rb
35
+ homepage: http://www.github.com/darrencauthon/smoke_monster
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.10
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Unexplained, dark magic.
59
+ test_files:
60
+ - spec/smoke_monster/array_to_object_spec.rb
61
+ - spec/smoke_monster/view_model_spec.rb
62
+ - spec/spec_helper.rb