Class: ActiveTriples::PropertyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/active_triples/property_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options, &block) ⇒ PropertyBuilder

Returns a new instance of PropertyBuilder.



6
7
8
9
# File 'lib/active_triples/property_builder.rb', line 6

def initialize(name, options, &block)
  @name = name
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/active_triples/property_builder.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/active_triples/property_builder.rb', line 4

def options
  @options
end

Class Method Details

.build(model, name, options, &block) ⇒ Object



17
18
19
20
21
22
# File 'lib/active_triples/property_builder.rb', line 17

def self.build(model, name, options, &block)
  builder = create_builder name, options, &block
  reflection = builder.build(&block)
  define_accessors model, reflection, options
  reflection
end

.create_builder(name, options, &block) ⇒ Object

Raises:

  • (ArgumentError)


11
12
13
14
15
# File 'lib/active_triples/property_builder.rb', line 11

def self.create_builder(name, options, &block)
  raise ArgumentError, "property names must be a Symbol" unless name.kind_of?(Symbol)

  new(name, options, &block)
end

.define_accessors(model, reflection, options = {}) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/active_triples/property_builder.rb', line 24

def self.define_accessors(model, reflection, options={})
  mixin = model.generated_property_methods
  name = reflection.term
  define_readers(mixin, name)
  define_id_reader(model, name) unless options[:cast] == false
  define_writers(mixin, name)
end

.define_id_reader(mixin, name) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/active_triples/property_builder.rb', line 40

def self.define_id_reader(mixin, name)
  mixin.class_eval "    def \#{name}_ids(*args)\n      get_values(:\#{name}, :cast => false)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.define_readers(mixin, name) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/active_triples/property_builder.rb', line 32

def self.define_readers(mixin, name)
  mixin.class_eval "    def \#{name}(*args)\n      get_values(:\#{name})\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

.define_writers(mixin, name) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/active_triples/property_builder.rb', line 48

def self.define_writers(mixin, name)
  mixin.class_eval "    def \#{name}=(value)\n      set_value(:\#{name}, value)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1

Instance Method Details

#build(&block) ⇒ Object



56
57
58
59
60
# File 'lib/active_triples/property_builder.rb', line 56

def build(&block)
  NodeConfig.new(name, options[:predicate], options.except(:predicate)) do |config|
    config.with_index(&block) if block_given?
  end
end