Skip to content

Commit

Permalink
refining schema support a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoward committed Feb 6, 2010
1 parent b04e669 commit 028bddc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 20 deletions.
15 changes: 13 additions & 2 deletions lib/hashish.rb
Expand Up @@ -15,10 +15,9 @@
#require 'orderedhash'
require 'json'

# hashish libs
# alpo libs
#
module Hashish
::H = Hashish unless defined?(::H)
Version = '0.4.2' unless defined?(Version)

def version
Expand Down Expand Up @@ -55,3 +54,15 @@ def libdir(*args, &block)
load 'parameter_parser.rb'
load 'api.rb'
end

unless defined?(H)
H = Hashish

def Hashish(*args, &block)
Hashish.data(*args, &block)
end

def H(*args, &block)
Hashish.data(*args, &block)
end
end
38 changes: 36 additions & 2 deletions lib/hashish/data.rb
Expand Up @@ -9,7 +9,7 @@ class Data < HashWithIndifferentAccess

def initialize(*args, &block)
data = self
options = HashWithIndifferentAccess.new(args.last.is_a?(Hash) ? args.pop : {})
options = args.last.is_a?(Hash) ? args.pop : {}

@key =
case args.size
Expand All @@ -18,7 +18,8 @@ def initialize(*args, &block)
else
args.shift
end
@key ||= 'data'
@key ||= (options.is_a?(Data) ? options.key : 'data')

@errors = Errors.new(data)
@form = Form.new(data)
@status = Status.ok
Expand Down Expand Up @@ -52,6 +53,39 @@ def model_name
def parse(params = {})
Hashish.parse(key, params)
end

def apply(other)
Hashish.merge(other => self)
end

alias_method 'build', 'apply'

class << Data
def merge(*args)
if args.size == 1 and args.first.is_a?(Hash)
params, schema = args.first.to_a.flatten
else
params, schema, *ignored = args
end

params = Hashish.data(params)
result = Hashish.data(schema)

Hashish.depth_first_each(params) do |keys, val|
currently = result.get(keys)
result.set(keys => val) if(currently.nil? or currently.empty?)
end

result
end

def build(*args)
key = args.shift
result = merge(*args)
result.key = key
result
end
end
end

def data(*args, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/hashish/data/form.rb
Expand Up @@ -176,7 +176,7 @@ def id_for(keys)
end

def class_for(keys, klass = nil)
klass = [klass, 'hashish', 'errors'].compact.join(' ') if data.errors.on?(keys)
klass = [klass, 'alpo', 'errors'].compact.join(' ') if data.errors.on?(keys)
klass
end

Expand Down
4 changes: 2 additions & 2 deletions lib/hashish/errors.rb
Expand Up @@ -122,7 +122,7 @@ def Errors.errors_to_html(*args)

at_least_one = false
names = errors.map{|e| e.data._name}
klass = [names, 'hashish errors'].flatten.compact.join(' ')
klass = [names, 'alpo errors'].flatten.compact.join(' ')

html =
table_(:class => klass){
Expand Down Expand Up @@ -161,7 +161,7 @@ def Errors.errors_to_html(*args)

at_least_one = false
names = errors.map{|e| e.data._name}
klass = [names, 'hashish errors'].flatten.compact.join(' ')
klass = [names, 'alpo errors'].flatten.compact.join(' ')

html =
ul_(:class => klass){
Expand Down
17 changes: 4 additions & 13 deletions lib/hashish/support.rb
Expand Up @@ -56,19 +56,10 @@ def form
end

def merge(*args)
if args.size == 1 and args.first.is_a?(Hash)
params, base = args.first.to_a.flatten
else
params, base, *ignored = args
end

params = Hashish.hash_for(params)
base = Hashish.hash_for(base)

Hashish.depth_first_each(params) do |keys, val|
base.set(keys => val) if base.get(keys).nil?
end
Data.merge(*args)
end

base
def build(*args)
Data.build(*args)
end
end

0 comments on commit 028bddc

Please sign in to comment.