Sha256: 40ff48c06c7c35486feb2b60b1d06f6e6fffdb4345bd78cca62a462a3a461138
Contents?: true
Size: 1.36 KB
Versions: 1
Compression:
Stored size: 1.36 KB
Contents
module Faceted module Model require 'json' require 'active_support/core_ext/hash' # Class methods ============================================================ module ModelClassMethods def build_association_from(field) bare_name = field.gsub(/_id$/, '') if field =~ /_id$/ klass = eval "#{scope}#{bare_name.classify}" define_method :"#{bare_name}" do klass.new(:id => self.send(field)) end end end def create(params={}) obj = self.new(params) obj.save obj end def field(name, args={}) fields << name define_method :"#{name}" do instance_variable_get("@#{name}") || args[:default] end define_method :"#{name}=" do |val| instance_variable_set("@#{name}", val) end build_association_from(name.to_s) if name.to_s.include?("id") && ! args[:skip_association] end def fields @fields ||= [:id] end def materialize(objects=[]) objects.compact.inject([]) do |a, object| interface = self.new interface.send(:object=, object) interface.send(:initialize_with_object) a << interface end end def scope parent.to_s == "Object" ? "::" : "#{parent.to_s}::" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
faceted-0.7.0 | lib/faceted/model.rb |