-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bd27dec
Showing
29 changed files
with
753 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
*.gem | ||
*.rbc | ||
.bundle | ||
.config | ||
.yardoc | ||
Gemfile.lock | ||
InstalledFiles | ||
_yardoc | ||
coverage | ||
doc/ | ||
lib/bundler/man | ||
pkg | ||
rdoc | ||
spec/reports | ||
test/tmp | ||
test/version_tmp | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--colour | ||
--backtrace |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
rvm use --create 1.9.3@active_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
rvm: | ||
- 1.9.2 | ||
- 1.9.3 | ||
- ruby-head | ||
#- rbx-19mode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
source 'https://rubygems.org' | ||
|
||
# Specify your gem's dependencies in active_data.gemspec | ||
gemspec | ||
|
||
gem 'guard' | ||
gem 'guard-rspec' | ||
|
||
if RUBY_PLATFORM =~ /darwin/i | ||
gem 'rb-fsevent' | ||
else | ||
gem 'rb-inotify' | ||
gem 'libnotify' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# A sample Guardfile | ||
# More info at https://github.com/guard/guard#readme | ||
|
||
guard 'rspec', :version => 2 do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
|
||
# Rails example | ||
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } | ||
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" } | ||
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] } | ||
watch(%r{^spec/support/(.+)\.rb$}) { "spec" } | ||
watch('config/routes.rb') { "spec/routing" } | ||
watch('app/controllers/application_controller.rb') { "spec/controllers" } | ||
# Capybara request specs | ||
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" } | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
Copyright (c) 2012 pyromaniac | ||
|
||
MIT License | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# ActiveData | ||
|
||
TODO: Write a gem description | ||
|
||
## Installation | ||
|
||
Add this line to your application's Gemfile: | ||
|
||
gem 'active_data' | ||
|
||
And then execute: | ||
|
||
$ bundle | ||
|
||
Or install it yourself as: | ||
|
||
$ gem install active_data | ||
|
||
## Usage | ||
|
||
TODO: Write usage instructions here | ||
|
||
## Contributing | ||
|
||
1. Fork it | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'Added some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/usr/bin/env rake | ||
require "bundler/gem_tasks" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# -*- encoding: utf-8 -*- | ||
require File.expand_path('../lib/active_data/version', __FILE__) | ||
|
||
Gem::Specification.new do |gem| | ||
gem.authors = ["pyromaniac"] | ||
gem.email = ["[email protected]"] | ||
gem.description = %q{Making object from any hash or hash array} | ||
gem.summary = %q{Working with hashes in AR style} | ||
gem.homepage = "" | ||
|
||
gem.files = `git ls-files`.split($\) | ||
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } | ||
gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) | ||
gem.name = "active_data" | ||
gem.require_paths = ["lib"] | ||
gem.version = ActiveData::VERSION | ||
|
||
gem.add_development_dependency "rake" | ||
gem.add_development_dependency "rspec" | ||
gem.add_runtime_dependency "activesupport" | ||
gem.add_runtime_dependency "activemodel" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'active_support/concern' | ||
require 'active_support/core_ext' | ||
require 'active_support/hash_with_indifferent_access' | ||
require 'active_model' | ||
require 'active_data/version' | ||
require 'active_data/model' | ||
|
||
module ActiveData | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
require 'active_data/model/collectionizable' | ||
require 'active_data/model/attributable' | ||
require 'active_data/model/extensions' | ||
|
||
module ActiveData | ||
module Model | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include ActiveModel::Conversion | ||
include ActiveModel::Dirty | ||
include ActiveModel::Validations | ||
include ActiveModel::MassAssignmentSecurity | ||
include ActiveModel::Serialization | ||
include ActiveModel::Serializers::JSON | ||
include ActiveModel::Serializers::Xml | ||
|
||
include Attributable | ||
include Collectionizable | ||
extend ActiveModel::Callbacks | ||
extend ActiveModel::Naming | ||
extend ActiveModel::Translation | ||
|
||
self.include_root_in_json = false | ||
|
||
def initialize attributes = {} | ||
@attributes = self.class.initialize_attributes | ||
@new_record = true | ||
assign_attributes attributes | ||
end | ||
|
||
def self.i18n_scope | ||
:active_data | ||
end | ||
end | ||
|
||
module ClassMethods | ||
def instantiate attributes = nil | ||
attributes ||= {} | ||
return attributes if attributes.instance_of? self | ||
|
||
instance = allocate | ||
|
||
instance.instance_variable_set(:@attributes, initialize_attributes) | ||
instance.instance_variable_set(:@new_record, false) | ||
instance.attributes = attributes | ||
|
||
instance | ||
end | ||
end | ||
|
||
def errors | ||
@errors ||= ActiveModel::Errors.new(self) | ||
end | ||
|
||
def persisted? | ||
!@new_record | ||
end | ||
|
||
def == other | ||
other.instance_of?(self.class) && other.attributes == attributes | ||
end | ||
|
||
def assign_attributes(attributes, options = {}) | ||
super(sanitize_for_mass_assignment((attributes.presence || {}), options[:as])) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
require 'active_data/model/serializable' | ||
|
||
module ActiveData | ||
module Model | ||
module Attributable | ||
include Serializable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :_attributes, :instance_reader => false, :instance_writer => false | ||
self._attributes = ActiveSupport::HashWithIndifferentAccess.new | ||
|
||
delegate :attribute_default, :to => 'self.class' | ||
end | ||
|
||
module ClassMethods | ||
def attribute name, options = {}, &block | ||
default = options.is_a?(Hash) ? options[:default] : options | ||
type = options.is_a?(Hash) ? normalize_type(options[:type]) : String | ||
self._attributes = self._attributes.merge(name => { | ||
default: (block || default), | ||
type: type | ||
}) | ||
|
||
define_method name do | ||
read_attribute(name) | ||
end | ||
define_method "#{name}_before_type_cast" do | ||
read_attribute_before_type_cast(name) | ||
end | ||
define_method "#{name}?" do | ||
read_attribute(name).present? | ||
end | ||
define_method "#{name}=" do |value| | ||
write_attribute(name, value) | ||
end | ||
|
||
if options.is_a?(Hash) && options[:in] | ||
define_singleton_method "#{name}_values" do | ||
options[:in].dup | ||
end | ||
end | ||
end | ||
|
||
def normalize_type type | ||
case type | ||
when String, Symbol then | ||
type.to_s.camelize.safe_constantize | ||
when nil then | ||
String | ||
else | ||
type | ||
end | ||
end | ||
|
||
def attribute_default name | ||
default = _attributes[name][:default] | ||
default.respond_to?(:call) ? default.call : default | ||
end | ||
|
||
def initialize_attributes | ||
_attributes.inject(ActiveSupport::HashWithIndifferentAccess.new) do |result, (name, value)| | ||
result[name] = nil | ||
result | ||
end | ||
end | ||
end | ||
|
||
def read_attribute name | ||
@attributes[name].nil? ? attribute_default(name) : @attributes[name] | ||
end | ||
alias_method :[], :read_attribute | ||
|
||
def read_attribute_before_type_cast name | ||
deserialize(send(name)) | ||
end | ||
|
||
def write_attribute name, value | ||
type = self.class._attributes[name][:type] | ||
@attributes[name] = serialize(value, type) | ||
end | ||
alias_method :[]=, :write_attribute | ||
|
||
def attributes | ||
Hash[attribute_names.map { |name| [name, send(name)] }] | ||
end | ||
|
||
def present_attributes | ||
Hash[attribute_names.map do |name| | ||
value = send(name) | ||
[name, value] if value.present? | ||
end] | ||
end | ||
|
||
def attribute_names | ||
@attributes.keys | ||
end | ||
|
||
def attributes= attributes | ||
assign_attributes(attributes) | ||
end | ||
|
||
def update_attributes attributes | ||
self.attributes = attributes | ||
end | ||
|
||
private | ||
|
||
def assign_attributes attributes | ||
(attributes.presence || {}).each do |(name, value)| | ||
send("#{name}=", value) if respond_to?("#{name}=") | ||
end | ||
self.attributes | ||
end | ||
|
||
end | ||
end | ||
end |
Oops, something went wrong.