Class: Reality::Facets::Facet

Inherits:
BaseElement
  • Object
show all
Defined in:
lib/reality/facets/facet.rb

Overview

nodoc

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facet_container, key, options = {}, &block) ⇒ Facet

Returns a new instance of Facet.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/reality/facets/facet.rb', line 25

def initialize(facet_container, key, options = {}, &block)
  options = options.dup
  @key = key
  @facet_container = facet_container
  @description = nil
  @required_facets = []
  @suggested_facets = []
  @model_extension_instances = {}
  facet_container.send :register_facet, self
  super(options, &block)
  facet_container.target_manager.targets.each do |target|
    target.extension_module.class_eval <<-RUBY
      def #{self.key}?
        !!(@#{self.key}_facet_enabled ||= false)
      end

      private

      def _enable_facet_#{self.key}!
        @#{self.key}_facet_enabled = true
        (@enabled_facets ||= []) << :#{self.key}
      end

      def _disable_facet_#{self.key}!
        @#{self.key}_facet_enabled = false
        @facet_#{self.key} = nil
        (@enabled_facets ||= []).delete(:#{self.key})
      end
    RUBY
  end
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



21
22
23
# File 'lib/reality/facets/facet.rb', line 21

def description
  @description
end

#facet_containerObject (readonly)

Returns the value of attribute facet_container.



19
20
21
# File 'lib/reality/facets/facet.rb', line 19

def facet_container
  @facet_container
end

#keyObject (readonly)

Returns the value of attribute key.



20
21
22
# File 'lib/reality/facets/facet.rb', line 20

def key
  @key
end

#required_facetsObject

Returns the value of attribute required_facets.



22
23
24
# File 'lib/reality/facets/facet.rb', line 22

def required_facets
  @required_facets
end

#suggested_facetsObject

Returns the value of attribute suggested_facets.



23
24
25
# File 'lib/reality/facets/facet.rb', line 23

def suggested_facets
  @suggested_facets
end

Instance Method Details

#enhance(model_class, &block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/reality/facets/facet.rb', line 61

def enhance(model_class, &block)
  facet_container.extension_manager.lock!
  target_manager = facet_container.target_manager
  target = target_manager.target_by_model_class(model_class)

  if @model_extension_instances[model_class].nil?
    extension_name = "#{::Reality::Naming.pascal_case(self.key)}#{model_class.name.gsub(/^.*\:\:([^\:]+)/, '\1')}Facet"
    definitions = target_manager.container.facet_definitions
    definitions.class_eval(<<-RUBY)
class #{extension_name} < Reality.base_element(:container_key => :#{target.inverse_access_method}, :pre_config_code => 'pre_init if respond_to?(:pre_init)', :post_config_code => 'post_init if respond_to?(:post_init)')
  def facet_key
    :#{self.key}
  end

  def self.facet_key
    :#{self.key}
  end

  def target_key
    :#{target.key}
  end

  def self.target_key
    :#{target.key}
  end

  def parent
    self.#{target.inverse_access_method}
  end
end
    RUBY
    @model_extension_instances[model_class] = definitions.const_get(extension_name)

    facet_container.extension_manager.instance_extensions.each do |extension|
      @model_extension_instances[model_class].class_eval { include extension }
    end

    facet_container.extension_manager.singleton_extensions.each do |extension|
      @model_extension_instances[model_class].singleton_class.class_eval { include extension }
    end

    target.extension_module.class_eval <<-RUBY
      def #{self.key}
        self.facet_#{self.key}
      end

      def facet_#{self.key}
        Reality::Facets.error("Attempted to access '#{self.key}' facet for model '#{model_class.name}' when facet disabled.") unless #{self.key}?
        @facet_#{self.key} ||= #{@model_extension_instances[model_class].name}.new(self)
      end
    RUBY
  end
  @model_extension_instances[model_class].class_eval(&block) if block_given?
end

#enhanced?(model_class) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/reality/facets/facet.rb', line 57

def enhanced?(model_class)
  !!@model_extension_instances[model_class]
end