active_hash 0.9.14 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +3 -0
- data/README.md +41 -1
- data/active_hash.gemspec +2 -1
- data/lib/active_hash.rb +1 -0
- data/lib/active_hash/version.rb +1 -1
- data/lib/active_yaml/aliases.rb +27 -0
- data/spec/active_yaml/base_spec.rb +6 -11
- metadata +4 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
2013-06-24 (v0.10.0)
|
2
|
+
- added ActiveYaml::Aliases module so you can DRY up your repetitive yaml (brett-richardson)
|
3
|
+
|
1
4
|
2013-05-23 (v0.9.14)
|
2
5
|
- enum_accessor can now take multiple field names when generating the constant
|
3
6
|
- temporarily disabled rails edge specs since there's an appraisal issue with minitest
|
data/README.md
CHANGED
@@ -16,7 +16,7 @@ ActiveHash also ships with:
|
|
16
16
|
## Installation
|
17
17
|
|
18
18
|
Make sure gemcutter.org is one of your gem sources, then run:
|
19
|
-
|
19
|
+
|
20
20
|
sudo gem install active_hash
|
21
21
|
|
22
22
|
## Usage
|
@@ -315,6 +315,46 @@ Since ActiveYaml just creates a hash from the YAML file, you will have all field
|
|
315
315
|
id: 3
|
316
316
|
name: Mexico
|
317
317
|
|
318
|
+
### Using aliases in YAML
|
319
|
+
|
320
|
+
Aliases can be used in ActiveYaml using either array or hash style by including `ActiveYaml::Aliases`.
|
321
|
+
With that module included, keys beginning with a '/' character can be safely added, and will be ignored, allowing you to add aliases anywhere in your code:
|
322
|
+
|
323
|
+
# Array Style
|
324
|
+
- /aliases:
|
325
|
+
soda_flavor: &soda_flavor
|
326
|
+
sweet
|
327
|
+
soda_price: &soda_price
|
328
|
+
1.0
|
329
|
+
|
330
|
+
- id: 1
|
331
|
+
name: Coke
|
332
|
+
flavor: *soda_flavor
|
333
|
+
price: *soda_price
|
334
|
+
|
335
|
+
|
336
|
+
# Key style
|
337
|
+
/aliases:
|
338
|
+
soda_flavor: &soda_flavor
|
339
|
+
sweet
|
340
|
+
soda_price: &soda_price
|
341
|
+
1.0
|
342
|
+
|
343
|
+
coke:
|
344
|
+
id: 1
|
345
|
+
name: Coke
|
346
|
+
flavor: *soda_flavor
|
347
|
+
price: *soda_price
|
348
|
+
|
349
|
+
class Soda < ActiveYaml::Base
|
350
|
+
include ActiveYaml::Aliases
|
351
|
+
end
|
352
|
+
|
353
|
+
Soda.length # => 1
|
354
|
+
Soda.first.flavor # => sweet
|
355
|
+
Soda.first.price # => 1.0
|
356
|
+
|
357
|
+
|
318
358
|
## ActiveFile
|
319
359
|
|
320
360
|
If you store encrypted data, or you'd like to store your flat files as CSV or XML or any other format, you can easily include ActiveHash to parse and load your file. Just add a custom ::load_file method, and define the extension you want the file to use:
|
data/active_hash.gemspec
CHANGED
data/lib/active_hash.rb
CHANGED
data/lib/active_hash/version.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
module ActiveYaml
|
2
|
+
|
3
|
+
module Aliases
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def insert(record)
|
11
|
+
super if record.attributes.present?
|
12
|
+
end
|
13
|
+
|
14
|
+
def raw_data
|
15
|
+
YAML.load_file(full_path).reject do |k, v|
|
16
|
+
v.kind_of? Hash and k.match /^\//i
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize(attributes={})
|
23
|
+
super unless attributes.keys.index { |k| k.match /^\//i }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
@@ -5,14 +5,11 @@ describe ActiveYaml::Base do
|
|
5
5
|
before do
|
6
6
|
ActiveYaml::Base.set_root_path File.expand_path(File.dirname(__FILE__) + "/../fixtures")
|
7
7
|
|
8
|
-
class ArrayRow
|
9
|
-
end
|
10
|
-
|
11
|
-
class
|
12
|
-
end
|
13
|
-
|
14
|
-
class State < ActiveYaml::Base
|
15
|
-
end
|
8
|
+
class ArrayRow < ActiveYaml::Base ; end
|
9
|
+
class City < ActiveYaml::Base ; end
|
10
|
+
class State < ActiveYaml::Base ; end
|
11
|
+
class ArrayProduct < ActiveYaml::Base ; end # Contain YAML aliases
|
12
|
+
class KeyProduct < ActiveYaml::Base ; end # Contain YAML aliases
|
16
13
|
end
|
17
14
|
|
18
15
|
after do
|
@@ -22,14 +19,12 @@ describe ActiveYaml::Base do
|
|
22
19
|
end
|
23
20
|
|
24
21
|
describe ".all" do
|
25
|
-
|
26
22
|
context "before the file is loaded" do
|
27
23
|
it "reads from the file" do
|
28
24
|
State.all.should_not be_empty
|
29
25
|
State.count.should > 0
|
30
26
|
end
|
31
27
|
end
|
32
|
-
|
33
28
|
end
|
34
29
|
|
35
30
|
describe ".delete_all" do
|
@@ -76,7 +71,7 @@ describe ActiveYaml::Base do
|
|
76
71
|
City.load_file.should be_kind_of(Array)
|
77
72
|
City.load_file.should include({"state" => :new_york, "name" => "Albany", "id" => 1})
|
78
73
|
City.reload
|
79
|
-
City.all.should include(
|
74
|
+
City.all.should include(City.new(:id => 1))
|
80
75
|
end
|
81
76
|
end
|
82
77
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_hash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -24,6 +24,7 @@ authors:
|
|
24
24
|
- Keenan Brock
|
25
25
|
- Desmond Bowe
|
26
26
|
- Matthew O'Riordan
|
27
|
+
- Brett Richardson
|
27
28
|
autorequire:
|
28
29
|
bindir: bin
|
29
30
|
cert_chain: []
|
@@ -125,6 +126,7 @@ files:
|
|
125
126
|
- lib/active_hash/base.rb
|
126
127
|
- lib/active_hash/version.rb
|
127
128
|
- lib/active_hash.rb
|
129
|
+
- lib/active_yaml/aliases.rb
|
128
130
|
- lib/active_yaml/base.rb
|
129
131
|
- lib/associations/associations.rb
|
130
132
|
- lib/enum/enum.rb
|
@@ -150,7 +152,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
150
152
|
version: '0'
|
151
153
|
segments:
|
152
154
|
- 0
|
153
|
-
hash:
|
155
|
+
hash: 514643025697724371
|
154
156
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
157
|
none: false
|
156
158
|
requirements:
|