Class: RDF::SAK::Transform::Partial
- Inherits:
-
Object
- Object
- RDF::SAK::Transform::Partial
- Defined in:
- lib/rdf/sak/transform.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
Class Method Summary collapse
-
.resolve(harness, subject: nil, transform: nil, params: {}) ⇒ Object
Resolve a partial function application with the given parameters.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #===(other) ⇒ Object
- #[](key) ⇒ Object
-
#initialize(subject, transform, params = {}) ⇒ Partial
constructor
A new instance of Partial.
- #keys ⇒ Object
- #matches?(params) ⇒ Boolean
- #params ⇒ Object
Constructor Details
#initialize(subject, transform, params = {}) ⇒ Partial
Returns a new instance of Partial.
715 716 717 718 719 720 721 722 |
# File 'lib/rdf/sak/transform.rb', line 715 def initialize subject, transform, params = {} raise ArgumentError, 'transform must be a Transform' unless transform.is_a? RDF::SAK::Transform @subject = subject @transform = transform @params = transform.validate params unless params.is_a? RDF::SAK::Transform::Partial end |
Instance Attribute Details
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
713 714 715 |
# File 'lib/rdf/sak/transform.rb', line 713 def subject @subject end |
#transform ⇒ Object (readonly)
Returns the value of attribute transform.
713 714 715 |
# File 'lib/rdf/sak/transform.rb', line 713 def transform @transform end |
Class Method Details
.resolve(harness, subject: nil, transform: nil, params: {}) ⇒ Object
Resolve a partial function application with the given parameters.
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'lib/rdf/sak/transform.rb', line 651 def self.resolve harness, subject: nil, transform: nil, params: {} raise ArgumentError, 'Must supply either a subject or a transform' unless subject or transform repo = harness.repo # coerce the transform to a Transform object if it isn't already if transform transform = harness.resolve(transform) or return unless transform.is_a?(RDF::SAK::Transform) elsif subject.is_a? RDF::URI # locate the transform if given the subject transform = RDF::SAK::Util.objects_for(repo, subject, RDF::SAK::TFO.transform, only: :resource).first or return transform = harness.resolve(transform) or return warn transform end # obtain the subject for the given parameters if subject params = {} transform.keys.each do |p| o = repo.query([subject, p, nil]).objects.uniq.sort params[p] = o unless o.empty? end else params = transform.validate params, symbols: false, defaults: false candidates = RDF::Query.new do # XXX we should sort parameters by longest value since # longer values will probably be less common; anyway this is # gonna all need to be rethought params.each { |p, objs| objs.each { |o| pattern [:s, p, o] } } pattern [:s, RDF.type, RDF::SAK::TFO.Partial] pattern [:s, RDF::SAK::TFO.transform, transform.subject] # add any remaining parameters # XXX this actually messes up; we don't want this # (transform.keys - params.keys.sort).each { |r| pattern [:s, r, nil] } end.execute(repo).map { |sol| [sol[:s], {}] }.to_h # warn "yo #{transform.subject} #{params} #{candidates}" # this is ruby being cheeky candidates.select! do |s, ps| transform.keys.each do |p| o = repo.query([s, p, nil]).objects.uniq.sort ps[p] = o unless o.empty? end ps == params end return if candidates.empty? # sort it so we always get the same thing subject = candidates.keys.sort.first params = candidates[subject] end self.new subject, transform, params end |
Instance Method Details
#==(other) ⇒ Object
745 746 747 |
# File 'lib/rdf/sak/transform.rb', line 745 def ==(other) self === other and subject == other.subject end |
#===(other) ⇒ Object
740 741 742 743 |
# File 'lib/rdf/sak/transform.rb', line 740 def ===(other) return false unless other.is_a? RDF::SAK::Transform::Partial transform == other.transform and matches? other.params end |
#[](key) ⇒ Object
724 725 726 |
# File 'lib/rdf/sak/transform.rb', line 724 def [](key) @params[key] end |
#keys ⇒ Object
728 729 730 |
# File 'lib/rdf/sak/transform.rb', line 728 def keys @params.keys end |
#matches?(params) ⇒ Boolean
736 737 738 |
# File 'lib/rdf/sak/transform.rb', line 736 def matches? params @params == @transform.validate(params) end |
#params ⇒ Object
732 733 734 |
# File 'lib/rdf/sak/transform.rb', line 732 def params @params.dup end |