Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pyromaniac committed Jul 21, 2012
0 parents commit bd27dec
Show file tree
Hide file tree
Showing 29 changed files with 753 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
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
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--colour
--backtrace
1 change: 1 addition & 0 deletions .rvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rvm use --create 1.9.3@active_data
5 changes: 5 additions & 0 deletions .travis.yml
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
14 changes: 14 additions & 0 deletions Gemfile
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
19 changes: 19 additions & 0 deletions Guardfile
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

22 changes: 22 additions & 0 deletions LICENSE
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.
29 changes: 29 additions & 0 deletions README.md
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
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
22 changes: 22 additions & 0 deletions active_data.gemspec
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
9 changes: 9 additions & 0 deletions lib/active_data.rb
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
70 changes: 70 additions & 0 deletions lib/active_data/model.rb
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
118 changes: 118 additions & 0 deletions lib/active_data/model/attributable.rb
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
Loading

0 comments on commit bd27dec

Please sign in to comment.