fancydata 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +20 -0
- data/Rakefile +21 -0
- data/VERSION +1 -0
- data/dist/fancydata-1.0.gem +0 -0
- data/lib/fancydata.rb +28 -0
- data/lib/sinatra/fancydata.rb +15 -0
- data/test/test.rb +41 -0
- metadata +61 -0
data/README
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Fancydata
|
2
|
+
=========
|
3
|
+
|
4
|
+
Use in one of two ways, plain old ruby:
|
5
|
+
|
6
|
+
FancyData <<-END
|
7
|
+
person:
|
8
|
+
name: Robert
|
9
|
+
END
|
10
|
+
|
11
|
+
@person.name #=> "Robert"
|
12
|
+
|
13
|
+
and as a haml filter
|
14
|
+
|
15
|
+
:data
|
16
|
+
person:
|
17
|
+
name: Robert
|
18
|
+
END
|
19
|
+
|
20
|
+
%h1= @person.name
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
task :default => :test
|
2
|
+
|
3
|
+
desc "Run tests"
|
4
|
+
task :test do
|
5
|
+
dir = File.dirname(__FILE__)
|
6
|
+
system "cd #{dir} && ruby test/test.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'jeweler'
|
11
|
+
Jeweler::Tasks.new do |gs|
|
12
|
+
gs.name = "fancydata"
|
13
|
+
gs.homepage = "http://github.com/quackingduck/fancydata"
|
14
|
+
gs.summary = "Quickly build objects from yaml"
|
15
|
+
gs.email = "myles@myles.id.au"
|
16
|
+
gs.authors = ["Myles Byrne"]
|
17
|
+
end
|
18
|
+
Jeweler::GemcutterTasks.new
|
19
|
+
rescue LoadError
|
20
|
+
puts "Install jeweler to build gem"
|
21
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.0
|
Binary file
|
data/lib/fancydata.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
# TODO: replace ostruct with something that understands #respond_to?
|
5
|
+
|
6
|
+
def OpenStruct.deep(obj)
|
7
|
+
case obj
|
8
|
+
when Array
|
9
|
+
obj.map {|e| OpenStruct.deep(e)}
|
10
|
+
when Hash
|
11
|
+
OpenStruct.new( obj.each do |k,v|
|
12
|
+
obj[k] = OpenStruct.deep(v)
|
13
|
+
end )
|
14
|
+
else
|
15
|
+
obj
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module FancyData
|
20
|
+
def self.version() File.read(__FILE__.sub('lib/fancydata.rb','VERSION')) end
|
21
|
+
end
|
22
|
+
|
23
|
+
# data must be a hash
|
24
|
+
def FancyData(data)
|
25
|
+
YAML.load(data).each do |key, val|
|
26
|
+
instance_variable_set("@#{key}", OpenStruct.deep(val))
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__),'..','fancydata')
|
2
|
+
|
3
|
+
module FancyData
|
4
|
+
|
5
|
+
module Filter
|
6
|
+
include Haml::Filters::Base
|
7
|
+
|
8
|
+
def compile(precompiler, text)
|
9
|
+
precompiler.send :push_silent, %(FancyData(#{text.inspect}))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
::Haml::Filters.defined['data'] = Filter
|
14
|
+
|
15
|
+
end
|
data/test/test.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'haml'
|
2
|
+
require 'test/unit'
|
3
|
+
|
4
|
+
require File.dirname(__FILE__) + '/../lib/sinatra/fancydata'
|
5
|
+
|
6
|
+
$yaml = <<-END
|
7
|
+
a_string: foo
|
8
|
+
a_num: 1
|
9
|
+
a_array:
|
10
|
+
- a
|
11
|
+
- b
|
12
|
+
- c
|
13
|
+
a_hash:
|
14
|
+
foo: bar
|
15
|
+
array_of_hashes:
|
16
|
+
- bool: true
|
17
|
+
END
|
18
|
+
|
19
|
+
$haml = <<-END
|
20
|
+
:data
|
21
|
+
a_string: foo
|
22
|
+
= @a_string
|
23
|
+
END
|
24
|
+
|
25
|
+
class FancyDataTest < Test::Unit::TestCase
|
26
|
+
|
27
|
+
def test_fancydata
|
28
|
+
FancyData($yaml)
|
29
|
+
assert_equal('foo', @a_string)
|
30
|
+
assert_equal(1, @a_num)
|
31
|
+
assert_equal('a', @a_array.first)
|
32
|
+
assert_equal('bar', @a_hash.foo)
|
33
|
+
assert_equal(true, @array_of_hashes.first.bool)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_haml
|
37
|
+
out = Haml::Engine.new($haml).render
|
38
|
+
assert('foo',out)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fancydata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Myles Byrne
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-16 00:00:00 +11:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: myles@myles.id.au
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- README
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- dist/fancydata-1.0.gem
|
29
|
+
- lib/fancydata.rb
|
30
|
+
- lib/sinatra/fancydata.rb
|
31
|
+
- test/test.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/quackingduck/fancydata
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Quickly build objects from yaml
|
60
|
+
test_files:
|
61
|
+
- test/test.rb
|