icecream 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +7 -0
- data/README.md +31 -0
- data/Rakefile +7 -0
- data/lib/icecream/icecream.rb +25 -0
- data/lib/icecream/parser.rb +67 -0
- data/lib/icecream/version.rb +1 -1
- data/spec/flavors/apple.flavor +4 -0
- data/spec/flavors/chocolate.flavor +4 -0
- data/spec/flavors/cream.flavor +4 -0
- data/spec/icecream_spec.rb +18 -0
- data/spec/parser_spec.rb +39 -0
- data/spec/spec_helper.rb +6 -0
- metadata +13 -3
- data/travis.yml +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9b3534515a5e8814a831e969d9da2226f8964846
|
4
|
+
data.tar.gz: 9bf457669bfa3b8656ed6781e3852c1b544e7182
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe432a91b66eff74e6fbe3deee61b2cd384f9e806d632bf92ce7e93869a3557567e4a66f501f731b5425bd18a71a5497fb03b861d0c274b69ff88c119fa9da98
|
7
|
+
data.tar.gz: a5cee9c390ae04aadd69e43671872502b122afd4b408dbce3bcdb9d697ea79a1c6d78ffbad8134bfe260a68d02d411c380006e0fb53b9cba38038432cca6bec1
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,37 @@
|
|
1
1
|
IceCream
|
2
2
|
==============
|
3
3
|
|
4
|
+
[](http://travis-ci.org/camiloribeiro/icecream)
|
5
|
+
|
6
|
+
!!!WORK IN PROGRESS!!!
|
7
|
+
|
8
|
+
I was looking for a really simple factory gem, such as factory_grill, but without any magic to use with rails. I was imcompetent at that, so I built this really simple Factory called IceCream.
|
9
|
+
|
10
|
+
It is a IceCream factory. It means that you can fabricate any IceCream flavor that you have in your fridge :D And, as any Factory, you can change the properties of an object that you got from the fridge.
|
11
|
+
|
12
|
+
The idea
|
13
|
+
-------
|
14
|
+
The central idea is to have a very simple file to describe what you want to fabricate. When I say simple, I mean SIMPLE. The file is just a file with two collums: variable and default value.
|
15
|
+
|
16
|
+
This gem goes to the factory directory and for each file, it is going to create a class (with the file name) and fill this class with instance variables for each one of the variables, and, of course set the value for the variables :)
|
17
|
+
|
18
|
+
Imagine a file called chocolate.flavor that has the following content
|
19
|
+
|
20
|
+
name = "Chocolate"
|
21
|
+
collor = :brown
|
22
|
+
calories = 150
|
23
|
+
price = 15.5
|
24
|
+
|
25
|
+
At the end, you can call the IceCream factory using the following commands:
|
26
|
+
|
27
|
+
$ IceCream.flavor :chocolate
|
28
|
+
|
29
|
+
and it is going to return a Chocolate (class) object called chocolate, with a string, a symble, a float and an integer. Easy ;)
|
30
|
+
|
31
|
+
And it is going to work in your non-rails as good as in your rails projects :D It is just ruby code, there is not one single dependency (so far).
|
32
|
+
|
33
|
+
|
34
|
+
|
4
35
|
LICENCE
|
5
36
|
=======
|
6
37
|
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),"./parser")
|
2
|
+
|
3
|
+
module IceCream
|
4
|
+
class IceCream
|
5
|
+
|
6
|
+
def initialize(path)
|
7
|
+
Dir.glob("#{path}/*.flavor") do |flavor_file_path|
|
8
|
+
flavor = Parser.get_flavor flavor_file_path
|
9
|
+
instance_name = flavor.class.to_s.downcase
|
10
|
+
instance_variable_set("@"+instance_name, flavor)
|
11
|
+
define_singleton_method(instance_name) do
|
12
|
+
"@"+instance_name
|
13
|
+
end
|
14
|
+
define_singleton_method(instance_name+"=") do |new_value|
|
15
|
+
"@"+instance_name = new_value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def flavor flavor
|
20
|
+
instance_variable_get ("@" + flavor.to_s).to_sym
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
|
2
|
+
module IceCream
|
3
|
+
class Parser
|
4
|
+
|
5
|
+
def self.get_flavor path
|
6
|
+
all_particularities = File.read(path).split("\n").map
|
7
|
+
objectify get_flavor_name(path), all_particularities
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def self.get_flavor_name path
|
13
|
+
title = slice_between_strings(path, "/flavors/", ".flavor")
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.parse_variables particularity
|
17
|
+
particularity.split("=").first.strip.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.parse_values particularity
|
21
|
+
particularity.split("=").last.strip
|
22
|
+
end
|
23
|
+
def self.slice_between_strings(string, str_start, str_end)
|
24
|
+
start_at = string.index(str_start).to_i + str_start.size
|
25
|
+
end_at = string.index(str_end)
|
26
|
+
string = string.slice start_at..end_at-1
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.fix_value value
|
30
|
+
#require "pry"; binding.pry
|
31
|
+
if value[0] == ":"
|
32
|
+
final = value.gsub(":","").to_sym
|
33
|
+
elsif !value.slice("\"").nil?
|
34
|
+
final = value.gsub("\"","")
|
35
|
+
elsif !value.slice(".").nil?
|
36
|
+
final = value.to_f if Float(value) rescue false
|
37
|
+
elsif !((Integer(value) rescue nil) == nil)
|
38
|
+
final = value.to_i
|
39
|
+
else
|
40
|
+
final = ""
|
41
|
+
end
|
42
|
+
final
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
|
47
|
+
def self.objectify flavor, all_particularities
|
48
|
+
class_name = flavor.capitalize
|
49
|
+
klass = Object.const_set(class_name,Class.new)
|
50
|
+
variables = all_particularities.each { | particularity | parse_variables particularity }
|
51
|
+
values = all_particularities.each { | particularity | parse_values particularity }
|
52
|
+
|
53
|
+
klass.class_eval do
|
54
|
+
attr_accessor *variables
|
55
|
+
|
56
|
+
define_method(:initialize) do
|
57
|
+
variables.each_with_index do |variable_name,i|
|
58
|
+
instance_variable_set("@"+variable_name, values[i])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
obj = klass.new
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
data/lib/icecream/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),"../lib/icecream/icecream")
|
2
|
+
|
3
|
+
describe "Icecream" do
|
4
|
+
describe "Gets all the objects to factory directory" do
|
5
|
+
it "has objects for each file in the factory filder" do
|
6
|
+
@fridge = IceCream::IceCream.new File.join(File.dirname(__FILE__),"flavors")
|
7
|
+
|
8
|
+
chocolate = @fridge.flavor :chocolate
|
9
|
+
chocolate.class.should be Chocolate
|
10
|
+
|
11
|
+
cream = @fridge.flavor :cream
|
12
|
+
cream.class.should be Cream
|
13
|
+
|
14
|
+
apple = @fridge.flavor :apple
|
15
|
+
apple.class.should be Apple
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/spec/parser_spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),"../lib/icecream/parser")
|
2
|
+
|
3
|
+
describe "Parser" do
|
4
|
+
describe "Parsing a file into an array" do
|
5
|
+
before(:all) do
|
6
|
+
@path = File.join(File.dirname(__FILE__),"../spec/flavors/chocolate.flavor")
|
7
|
+
end
|
8
|
+
it "Gets the object" do
|
9
|
+
obj = IceCream::Parser.get_flavor @path
|
10
|
+
obj.class.should be Chocolate
|
11
|
+
end
|
12
|
+
it "Gets the string between two strings" do
|
13
|
+
IceCream::Parser.slice_between_strings("/super/flavors/chocolate.flavor", "/flavors/", ".flavor")
|
14
|
+
.should == "chocolate"
|
15
|
+
end
|
16
|
+
it "Gets the name of the future object" do
|
17
|
+
IceCream::Parser.get_flavor_name(@path).should == "chocolate"
|
18
|
+
end
|
19
|
+
it "Returns the right type of the value" do
|
20
|
+
|
21
|
+
# Strings
|
22
|
+
IceCream::Parser.fix_value("\"chocolate\"").should == "chocolate"
|
23
|
+
IceCream::Parser.fix_value("\"15\"").should == "15"
|
24
|
+
|
25
|
+
# Symbles
|
26
|
+
IceCream::Parser.fix_value(":i15").should == :i15
|
27
|
+
|
28
|
+
# Float
|
29
|
+
IceCream::Parser.fix_value("15.5").should == 15.5
|
30
|
+
|
31
|
+
# Int
|
32
|
+
IceCream::Parser.fix_value("15").should == 15
|
33
|
+
|
34
|
+
# Invalid conversions
|
35
|
+
IceCream::Parser.fix_value("15s").should == ""
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: icecream
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Camilo Ribeiro
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -89,13 +89,23 @@ extensions: []
|
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
91
|
- .gitignore
|
92
|
+
- .rspec
|
92
93
|
- .ruby-version
|
94
|
+
- .travis.yml
|
93
95
|
- Gemfile
|
94
96
|
- README.md
|
97
|
+
- Rakefile
|
95
98
|
- icecream.gemspec
|
96
99
|
- lib/icecream.rb
|
100
|
+
- lib/icecream/icecream.rb
|
101
|
+
- lib/icecream/parser.rb
|
97
102
|
- lib/icecream/version.rb
|
98
|
-
-
|
103
|
+
- spec/flavors/apple.flavor
|
104
|
+
- spec/flavors/chocolate.flavor
|
105
|
+
- spec/flavors/cream.flavor
|
106
|
+
- spec/icecream_spec.rb
|
107
|
+
- spec/parser_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
99
109
|
homepage: http://github.com/camiloribeiro/icecream
|
100
110
|
licenses: []
|
101
111
|
metadata: {}
|