active_hash 1.0.0 → 1.0.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/CHANGELOG CHANGED
@@ -1,3 +1,7 @@
1
+ 2013-07-15 (v1.0.1)
2
+ - Travis CI for ActiveHash + Ruby 2, 1.8.7, Rubinius and JRuby support (mattheworiordan)
3
+ - no longer need to call .all before executing `find_by_*` or `where` methods (mattheworiordan)
4
+
1
5
  2013-06-24 (v1.0.0)
2
6
  - save is a no-op on existing records, instead of raising an error (issue #63)
3
7
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # ActiveHash
2
2
 
3
+ [![Build Status](https://travis-ci.org/zilkey/active_hash.png?branch=master)](https://travis-ci.org/zilkey/active_hash)
4
+
3
5
  ActiveHash is a simple base class that allows you to use a ruby hash as a readonly datasource for an ActiveRecord-like model.
4
6
 
5
7
  ActiveHash assumes that every hash has an :id key, which is what you would probably store in a database. This allows you to seemlessly upgrade from ActiveHash objects to full ActiveRecord objects without having to change any code in your app, or any foreign keys in your database.
@@ -15,9 +17,13 @@ ActiveHash also ships with:
15
17
 
16
18
  ## Installation
17
19
 
18
- Make sure gemcutter.org is one of your gem sources, then run:
20
+ Bundler:
21
+
22
+ gem 'active_hash'
23
+
24
+ Other:
19
25
 
20
- sudo gem install active_hash
26
+ gem install active_hash
21
27
 
22
28
  ## Usage
23
29
 
@@ -294,7 +300,7 @@ By default, this class will look for a yml file named "countries.yml" in the sam
294
300
 
295
301
  The above example will look for the file "/u/data/sample.yml".
296
302
 
297
- Since ActiveYaml just creates a hash from the YAML file, you will have all fields specified in YAML auto-defined for you once you call all. You can format your YAML as an array, or as a hash:
303
+ Since ActiveYaml just creates a hash from the YAML file, you will have all fields specified in YAML auto-defined for you. You can format your YAML as an array, or as a hash:
298
304
 
299
305
  # array style
300
306
  - id: 1
data/active_hash.gemspec CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  $:.push File.expand_path("../lib", __FILE__)
4
4
  require "active_hash/version"
5
+ require "util/ruby_engine"
6
+ require "util/ruby_version"
5
7
 
6
8
  Gem::Specification.new do |s|
7
9
  s.name = %q{active_hash}
@@ -58,28 +60,46 @@ Gem::Specification.new do |s|
58
60
  "spec/spec_helper.rb"
59
61
  ]
60
62
 
63
+ supported_rails_versions = if RubyVersion < '1.9.3'
64
+ [">= 2.2.2", "< 4"]
65
+ else
66
+ [">= 2.2.2"]
67
+ end
68
+
69
+ sqlite_gem = if RubyEngine.jruby?
70
+ if RubyVersion >= '1.9.3'
71
+ # Until 1.3.0 is released, we need to depend on a Beta version for JRuby and Rails 4
72
+ # https://github.com/jruby/activerecord-jdbc-adapter/issues/419#issuecomment-20567142
73
+ ['activerecord-jdbcsqlite3-adapter', ['>= 1.3.0.beta2']]
74
+ else
75
+ ['activerecord-jdbcsqlite3-adapter']
76
+ end
77
+ else
78
+ ['sqlite3']
79
+ end
80
+
61
81
  if s.respond_to? :specification_version then
62
82
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
63
83
  s.specification_version = 3
64
84
 
65
85
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
66
- s.add_runtime_dependency(%q<activesupport>, [">= 2.2.2"])
86
+ s.add_runtime_dependency(%q<activesupport>, supported_rails_versions)
67
87
  s.add_development_dependency(%q<rspec>, ["~> 2.2.0"])
68
- s.add_development_dependency(%q<sqlite3>, [">= 0"])
69
- s.add_development_dependency(%q<activerecord>, [">= 2.2.2"])
70
- s.add_development_dependency(%q<appraisal>, [">= 0"])
88
+ s.add_development_dependency(*sqlite_gem)
89
+ s.add_development_dependency(%q<activerecord>, supported_rails_versions)
90
+ s.add_development_dependency(%q<appraisal>)
71
91
  else
72
- s.add_dependency(%q<activesupport>, [">= 2.2.2"])
92
+ s.add_dependency(%q<activesupport>, supported_rails_versions)
73
93
  s.add_dependency(%q<rspec>, ["~> 2.2.0"])
74
- s.add_dependency(%q<sqlite3>, [">= 0"])
75
- s.add_dependency(%q<activerecord>, [">= 2.2.2"])
76
- s.add_dependency(%q<appraisal>, [">= 0"])
94
+ s.add_dependency(*sqlite_gem)
95
+ s.add_dependency(%q<activerecord>, supported_rails_versions)
96
+ s.add_dependency(%q<appraisal>)
77
97
  end
78
98
  else
79
- s.add_dependency(%q<activesupport>, [">= 2.2.2"])
99
+ s.add_dependency(%q<activesupport>, supported_rails_versions)
80
100
  s.add_dependency(%q<rspec>, ["~> 2.2.0"])
81
- s.add_dependency(%q<sqlite3>, [">= 0"])
82
- s.add_dependency(%q<activerecord>, [">= 2.2.2"])
83
- s.add_dependency(%q<appraisal>, [">= 0"])
101
+ s.add_dependency(*sqlite_gem)
102
+ s.add_dependency(%q<activerecord>, supported_rails_versions)
103
+ s.add_dependency(%q<appraisal>)
84
104
  end
85
105
  end
@@ -10,16 +10,6 @@ module ActiveFile
10
10
 
11
11
  class << self
12
12
 
13
- def all(options={})
14
- reload unless data_loaded
15
- super
16
- end
17
-
18
- def where(options)
19
- reload unless data_loaded
20
- super
21
- end
22
-
23
13
  def delete_all
24
14
  self.data_loaded = true
25
15
  super
@@ -53,19 +43,15 @@ module ActiveFile
53
43
  def extension
54
44
  raise "Override Me"
55
45
  end
46
+ protected :extension
56
47
 
57
- def find(*args)
58
- reload unless data_loaded
59
- return super
60
- end
61
-
62
- def find_by_id(*args)
63
- reload unless data_loaded
64
- return super
48
+ [:find, :find_by_id, :all, :where, :method_missing].each do |method|
49
+ define_method(method) do |*args|
50
+ reload unless data_loaded
51
+ return super(*args)
52
+ end
65
53
  end
66
54
 
67
- protected :extension
68
-
69
55
  end
70
56
  end
71
57
 
@@ -1,5 +1,5 @@
1
1
  module ActiveHash
2
2
  module Gem
3
- VERSION = "1.0.0"
3
+ VERSION = "1.0.1"
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ module ActiveYaml
20
20
  end
21
21
 
22
22
  def initialize(attributes={})
23
- super unless attributes.keys.index { |k| k.match /^\//i }
23
+ super unless attributes.keys.index { |k| k.to_s.match /^\//i }
24
24
  end
25
25
  end
26
26
 
@@ -0,0 +1,68 @@
1
+ # Sourced from http://rbjl.net/35-how-to-properly-check-for-your-ruby-interpreter-version-and-os
2
+ # Extracted from http://rubyzucker.info/
3
+ # Copyright (c) 2010-2013 Jan Lelis <http://happycode.org> released under the MIT license
4
+
5
+ class RubyEngine
6
+ class << self
7
+ def interpreter
8
+ case
9
+ when RUBY_PLATFORM == 'parrot'
10
+ 'cardinal'
11
+ when Object.constants.include?( :RUBY_ENGINE ) ||
12
+ Object.constants.include?( 'RUBY_ENGINE' )
13
+ if RUBY_ENGINE == 'ruby'
14
+ if RUBY_DESCRIPTION =~ /Enterprise/
15
+ 'ree'
16
+ else
17
+ 'mri'
18
+ end
19
+ else
20
+ RUBY_ENGINE.to_s # jruby, rbx, ironruby, macruby, etc.
21
+ end
22
+ else # probably 1.8
23
+ 'mri'
24
+ end
25
+ end
26
+
27
+ def is?(what)
28
+ what === interpreter
29
+ end
30
+ alias is is?
31
+
32
+ def to_s
33
+ interpreter
34
+ end
35
+
36
+ def mri?
37
+ RubyEngine.is? 'mri'
38
+ end
39
+ alias official_ruby? mri?
40
+ alias ruby? mri?
41
+
42
+ def jruby?
43
+ RubyEngine.is? 'jruby'
44
+ end
45
+ alias java? jruby?
46
+
47
+ def rubinius?
48
+ RubyEngine.is? 'rbx'
49
+ end
50
+ alias rbx? rubinius?
51
+
52
+ def ree?
53
+ RubyEngine.is? 'ree'
54
+ end
55
+ alias enterprise? ree?
56
+
57
+ def ironruby?
58
+ RubyEngine.is? 'ironruby'
59
+ end
60
+ alias iron_ruby? ironruby?
61
+
62
+ def cardinal?
63
+ RubyEngine.is? 'cardinal'
64
+ end
65
+ alias parrot? cardinal?
66
+ alias perl? cardinal?
67
+ end
68
+ end
@@ -0,0 +1,139 @@
1
+ # Sourced from http://rbjl.net/35-how-to-properly-check-for-your-ruby-interpreter-version-and-os
2
+ # Extracted from http://rubyzucker.info/
3
+ # Copyright (c) 2010-2013 Jan Lelis <http://happycode.org> released under the MIT license
4
+
5
+ ### usage examples
6
+ # RubyVersion
7
+ ### check for the main version with a Float
8
+ # RubyVersion.is? 1.8
9
+ ### use strings for exacter checking
10
+ # RubyVersion.is.above '1.8.7'
11
+ # RubyVersion.is.at_least '1.8.7' # or below, at_most, not
12
+ ### you can use the common comparison operators
13
+ # RubyVersion >= '1.8.7'
14
+ # RubyVersion.is.between? '1.8.6', '1.8.7'
15
+ ### relase date checks
16
+ # RubyVersion.is.older_than Date.today
17
+ # RubyVersion.is.newer_than '2009-08-19'
18
+ ### accessors
19
+ # RubyVersion.major # e.g. => 1
20
+ # RubyVersion.minor # e.g. => 8
21
+ # RubyVersion.tiny # e.g. => 7
22
+ # RubyVersion.patchlevel # e.g. => 249
23
+ # RubyVersion.description # e.g. => "ruby 1.8.7 (2010-01-10 patchlevel 249) [i486-linux]"
24
+
25
+ require 'date'
26
+ require 'time'
27
+
28
+ module RubyVersion
29
+ class << self
30
+ def to_s
31
+ RUBY_VERSION
32
+ end
33
+
34
+ # comparable
35
+ def <=>(other)
36
+ value = case other
37
+ when Integer
38
+ RUBY_VERSION.to_i
39
+ when Float
40
+ RUBY_VERSION.to_f
41
+ when String
42
+ RUBY_VERSION
43
+ when Date,Time
44
+ other.class.parse(RUBY_RELEASE_DATE)
45
+ else
46
+ other = other.to_s
47
+ RUBY_VERSION
48
+ end
49
+ value <=> other
50
+ end
51
+ include Comparable
52
+
53
+ # chaining for dsl-like language
54
+ def is?(other = nil)
55
+ if other
56
+ RubyVersion == other
57
+ else
58
+ RubyVersion
59
+ end
60
+ end
61
+ alias is is?
62
+
63
+ # aliases
64
+ alias below <
65
+ alias below? <
66
+ alias at_most <=
67
+ alias at_most? <=
68
+ alias above >
69
+ alias above? >
70
+ alias at_least >=
71
+ alias at_least? >=
72
+ alias exactly ==
73
+ alias exactly? ==
74
+ def not(other)
75
+ self != other
76
+ end
77
+ alias not? not
78
+ alias between between?
79
+
80
+ # compare dates
81
+ def newer_than(other)
82
+ if other.is_a? Date or other.is_a? Time
83
+ RubyVersion > other
84
+ else
85
+ RUBY_RELEASE_DATE > other.to_s
86
+ end
87
+ end
88
+ alias newer_than? newer_than
89
+
90
+ def older_than(other)
91
+ if other.is_a? Date or other.is_a? Time
92
+ RubyVersion < other
93
+ else
94
+ RUBY_RELEASE_DATE < other.to_s
95
+ end
96
+ end
97
+ alias older_than? older_than
98
+
99
+ def released_today
100
+ RubyVersion.date == Date.today
101
+ end
102
+ alias released_today? released_today
103
+
104
+ # accessors
105
+
106
+ def major
107
+ RUBY_VERSION.to_i
108
+ end
109
+ alias main major
110
+
111
+ def minor
112
+ RUBY_VERSION.split('.')[1].to_i
113
+ end
114
+ alias mini minor
115
+
116
+ def tiny
117
+ RUBY_VERSION.split('.')[2].to_i
118
+ end
119
+
120
+ alias teeny tiny
121
+
122
+ def patchlevel
123
+ RUBY_PATCHLEVEL
124
+ end
125
+
126
+ def platform
127
+ RUBY_PLATFORM
128
+ end
129
+
130
+ def release_date
131
+ Date.parse RUBY_RELEASE_DATE
132
+ end
133
+ alias date release_date
134
+
135
+ def description
136
+ RUBY_DESCRIPTION
137
+ end
138
+ end
139
+ end
@@ -511,7 +511,7 @@ describe ActiveHash, "Base" do
511
511
  it "raises a NoMethodError" do
512
512
  lambda {
513
513
  Country.find_by_name_and_shoe_size("US", 10)
514
- }.should raise_error(NoMethodError, "undefined method `find_by_name_and_shoe_size' for Country:Class")
514
+ }.should raise_error(NoMethodError, /undefined method `find_by_name_and_shoe_size' (?:for|on) Country/)
515
515
  end
516
516
  end
517
517
  end
@@ -540,7 +540,7 @@ describe ActiveHash, "Base" do
540
540
  it "raises a NoMethodError" do
541
541
  lambda {
542
542
  Country.find_by_name_and_shoe_size!("US", 10)
543
- }.should raise_error(NoMethodError, "undefined method `find_by_name_and_shoe_size!' for Country:Class")
543
+ }.should raise_error(NoMethodError, /undefined method `find_by_name_and_shoe_size!' (?:for|on) Country/)
544
544
  end
545
545
  end
546
546
  end
@@ -567,7 +567,7 @@ describe ActiveHash, "Base" do
567
567
  it "doesn't blow up if you call a missing dynamic finder when fields haven't been set" do
568
568
  proc do
569
569
  Country.find_by_name("Foo")
570
- end.should raise_error(NoMethodError, "undefined method `find_by_name' for Country:Class")
570
+ end.should raise_error(NoMethodError, /undefined method `find_by_name' (?:for|on) Country/)
571
571
  end
572
572
  end
573
573
 
@@ -27,6 +27,15 @@ describe ActiveYaml::Base do
27
27
  end
28
28
  end
29
29
 
30
+ describe ".where" do
31
+ context "before the file is loaded" do
32
+ it "reads from the file and filters by where statement" do
33
+ State.where(:name => 'Oregon').should_not be_empty
34
+ State.count.should > 0
35
+ end
36
+ end
37
+ end
38
+
30
39
  describe ".delete_all" do
31
40
  context "when called before .all" do
32
41
  it "causes all to not load data" do
@@ -95,4 +104,16 @@ describe ActiveYaml::Base do
95
104
 
96
105
  end
97
106
 
107
+ describe 'meta programmed finders and properties for fields that exist in the YAML' do
108
+
109
+ it 'should have a finder method for each property' do
110
+ City.find_by_state('Oregon').should_not be_nil
111
+ end
112
+
113
+ it 'should have a find all method for each property' do
114
+ City.find_all_by_state('Oregon').should_not be_nil
115
+ end
116
+
117
+ end
118
+
98
119
  end
@@ -7,7 +7,9 @@ describe ActiveHash::Base, "associations" do
7
7
  class Country < ActiveRecord::Base
8
8
  extend ActiveHash::Associations::ActiveRecordExtensions
9
9
  establish_connection :adapter => "sqlite3", :database => ":memory:"
10
- connection.create_table(:countries, :force => true) {}
10
+ connection.create_table(:countries, :force => true) do |t|
11
+ t.string :name
12
+ end
11
13
  end
12
14
 
13
15
  class School < ActiveRecord::Base
@@ -17,7 +17,7 @@ describe ActiveHash::Base, "enum" do
17
17
  enum_accessor :name, :county
18
18
 
19
19
  self.data = [
20
- {name: "Queen Ann", county: "King"}
20
+ {:name => "Queen Ann", :county => "King"}
21
21
  ]
22
22
  end
23
23
  end
data/spec/spec_helper.rb CHANGED
@@ -4,3 +4,5 @@ require 'rspec/autorun'
4
4
  $LOAD_PATH.unshift(File.dirname(__FILE__))
5
5
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
6
  require 'active_hash'
7
+
8
+ require 'util/ruby_version.rb'
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: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -130,6 +130,8 @@ files:
130
130
  - lib/active_yaml/base.rb
131
131
  - lib/associations/associations.rb
132
132
  - lib/enum/enum.rb
133
+ - lib/util/ruby_engine.rb
134
+ - lib/util/ruby_version.rb
133
135
  - Gemfile
134
136
  - spec/active_file/base_spec.rb
135
137
  - spec/active_hash/base_spec.rb
@@ -152,7 +154,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
154
  version: '0'
153
155
  segments:
154
156
  - 0
155
- hash: -1921449152937241746
157
+ hash: 1166789792063015008
156
158
  required_rubygems_version: !ruby/object:Gem::Requirement
157
159
  none: false
158
160
  requirements: