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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1607aaaa8d21bc440dc77b88d623ec91912f4af6
4
- data.tar.gz: 4e0132787951c45e1b8666df186869af8f282475
3
+ metadata.gz: 9b3534515a5e8814a831e969d9da2226f8964846
4
+ data.tar.gz: 9bf457669bfa3b8656ed6781e3852c1b544e7182
5
5
  SHA512:
6
- metadata.gz: a4bcfdb1c40b98237751715f46ef32a43f78d4f81fce04db3b777ecdf7a1898f526d05174d901c4c589241b5adb7fceb0bc6e6fc1d3da8ddc788f17928e76310
7
- data.tar.gz: 69502e5c0bc16d6ec1818bd44730229d30fabfd841ee913c1baa16cab6c5570f6f68cb58406ed82979987e327bbe7535d848362eca3095d4028615d17731699f
6
+ metadata.gz: fe432a91b66eff74e6fbe3deee61b2cd384f9e806d632bf92ce7e93869a3557567e4a66f501f731b5425bd18a71a5497fb03b861d0c274b69ff88c119fa9da98
7
+ data.tar.gz: a5cee9c390ae04aadd69e43671872502b122afd4b408dbce3bcdb9d697ea79a1c6d78ffbad8134bfe260a68d02d411c380006e0fb53b9cba38038432cca6bec1
data/.gitignore CHANGED
@@ -1,2 +1,3 @@
1
1
  *.sw*
2
+ Gemfile.lock
2
3
  *.gem
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile CHANGED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem 'pry'
4
+ gem 'rake'
5
+ gem 'rspec'
6
+
7
+ gemspec
data/README.md CHANGED
@@ -1,6 +1,37 @@
1
1
  IceCream
2
2
  ==============
3
3
 
4
+ [![Build Status](https://secure.travis-ci.org/camiloribeiro/icecream.png)](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,7 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ task :default => [:spec]
4
+
5
+ RSpec::Core::RakeTask .new(:spec) do |task|
6
+ task.rspec_opts = ["--format documentation"]
7
+ end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module IceCream
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,4 @@
1
+ name = "apple"
2
+ calories = 1500
3
+ color = :red
4
+ price = 1.5
@@ -0,0 +1,4 @@
1
+ name = "chocolate"
2
+ calories = 150
3
+ color = :brown
4
+ price = 15.5
@@ -0,0 +1,4 @@
1
+ name = "cream"
2
+ calories = 200
3
+ color = :yellow
4
+ price = 17.5
@@ -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
@@ -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
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+
3
+ RSpec.configure do |config|
4
+ config.color_enabled = true
5
+ config.formatter = 'documentation'
6
+ end
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.1
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-20 00:00:00.000000000 Z
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
- - travis.yml
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: {}
data/travis.yml DELETED
@@ -1,8 +0,0 @@
1
- language: ruby
2
- rvm:
3
- - 1.8.7
4
- - 1.9.3
5
- - 2.0.0
6
-
7
- before_install:
8
- - gem update --system