Class: Gearbox::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/gearbox/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Attribute

Returns a new instance of Attribute.



9
10
11
12
13
14
# File 'lib/gearbox/attribute.rb', line 9

def initialize(opts={})
  set(opts.delete(:value) || :_value_not_set)
  assert_defaults
  extract_from_statement(opts) if opts.has_key?(:statement)
  assert_options(opts)
end

Instance Attribute Details

#datatypeObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def datatype
  @datatype
end

#indexObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def index
  @index
end

#languageObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def language
  @language
end

#nameObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def name
  @name
end

#predicateObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def predicate
  @predicate
end

#repositoryObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def repository
  @repository
end

#reverseObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def reverse
  @reverse
end

#subject_decoratorObject

Returns the value of attribute subject_decorator.



7
8
9
# File 'lib/gearbox/attribute.rb', line 7

def subject_decorator
  @subject_decorator
end

#valueObject

Options to figure out: more index options, routing, triple pattern



6
7
8
# File 'lib/gearbox/attribute.rb', line 6

def value
  @value
end

Instance Method Details

#literal(opts = {:value => :_value_not_set}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/gearbox/attribute.rb', line 39

def literal(opts={:value => :_value_not_set})
  value = opts.delete(:value)
  value = to_value if value == :_value_not_set
  opts = {:language => self.language, :datatype => self.datatype}.merge(opts)
  value = case opts[:datatype]
  when :boolean
    RDF::Literal::Boolean.new(value)
  when :date
    RDF::Literal::Date.new(value)
  when :date_time
    RDF::Literal::DateTime.new(value)
  when :decimal
    RDF::Literal::Decimal.new(value)
  when :double
    RDF::Literal::Double.new(value)
  when :time
    RDF::Literal::Time.new(value)
  when :token
    RDF::Literal::Token.new(value)
  when :xml
    RDF::Literal::XML.new(value)
  else
    value
  end
    
  RDF::Literal.new(value, opts)
end

#set(value = :_value_not_set, opts = {}) ⇒ Object



67
68
69
70
# File 'lib/gearbox/attribute.rb', line 67

def set(value=:_value_not_set, opts={})
  opts = {:value => value}.merge(opts)
  @value = literal(opts).object
end

#subjectObject

TODO: I’m a little quesy about the relationship between the model class, the model, and the attribute. I’m going to have to rebuild the model DSL and see how this stuff gets built.



22
23
24
25
26
27
28
29
30
31
# File 'lib/gearbox/attribute.rb', line 22

def subject
  return nil unless subject_decorator
  if subject_decorator.respond_to?(:call)
    subject_decorator.call()
  elsif respond_to?(subject_decorator)
    self.send(subject_decorator)
  else
    nil
  end
end

#to_rdf(model, opts = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gearbox/attribute.rb', line 72

def to_rdf(model, opts={})
  opts[:value] = :_value_not_set unless opts.has_key?(:value)
  opts = {
    :reverse => self.reverse, 
    :predicate => self.predicate, 
    :value => self.literal(opts)
  }.merge(opts)
  
  subject = self.subject
  subject ||= model.subject
  subject = RDF::URI.new(subject)
  predicate = opts[:predicate]
  object = opts[:value]
  
  return opts[:reverse] ? RDF::Statement(object, predicate, subject)
                 : RDF::Statement(subject, predicate, object)
end

#to_valueObject Also known as: get



33
34
35
36
# File 'lib/gearbox/attribute.rb', line 33

def to_value
  # Skipping the serialization steps for a moment.
  @value
end