Module: Entity::Property

Defined in:
lib/scout/entity/property.rb

Defined Under Namespace

Classes: MultipleEntityProperty

Constant Summary collapse

DEFAULT_PROPERTY_TYPE =
:both

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#persisted_methodsObject

Returns the value of attribute persisted_methods.



14
15
16
# File 'lib/scout/entity/property.rb', line 14

def persisted_methods
  @persisted_methods
end

#propertiesObject

Returns the value of attribute properties.



14
15
16
# File 'lib/scout/entity/property.rb', line 14

def properties
  @properties
end

Class Method Details

.array_method(name) ⇒ Object



31
32
33
# File 'lib/scout/entity/property.rb', line 31

def self.array_method(name)
  ("_ary_" + name.to_s).to_sym
end

.multi_method(name) ⇒ Object



35
36
37
# File 'lib/scout/entity/property.rb', line 35

def self.multi_method(name)
  ("_multi_" + name.to_s).to_sym
end

.persist(name, obj, type, options, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/scout/entity/property.rb', line 16

def self.persist(name, obj, type, options, &block)
  return yield unless options[:persist]
  if (type == :annotation || type == :annotations) && options[:annotation_repo]
    repo = options[:annotation_repo]
    Persist.annotation_repo_persist(repo, [name, obj.id] * ":", &block)
  else

    Persist.persist([name, obj.id] * ":", type, options.dup, &block)
  end
end

.single_method(name) ⇒ Object



27
28
29
# File 'lib/scout/entity/property.rb', line 27

def self.single_method(name)
  ("_single_" + name.to_s).to_sym
end

Instance Method Details

#persist(name, type = :marshal, options = {}) ⇒ Object



148
149
150
151
152
153
# File 'lib/scout/entity/property.rb', line 148

def persist(name, type = :marshal, options = {})
  options = IndiferentHash.add_defaults options, persist: true,
    dir: File.join(Entity.entity_property_cache, self.to_s, name.to_s)
  @persisted_methods ||= {}
  @persisted_methods[name] = [type, options]
end

#persisted?(name) ⇒ Boolean

Returns:

  • (Boolean)


155
156
157
158
# File 'lib/scout/entity/property.rb', line 155

def persisted?(name)
  @persisted_methods ||= {}
  @persisted_methods.include?(name)
end

#property(name_and_type, &block) ⇒ Object



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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/scout/entity/property.rb', line 40

def property(name_and_type, &block)
  name, type = case name_and_type
               when Symbol, String
                 [name_and_type.to_sym, DEFAULT_PROPERTY_TYPE]
               else
                 name_and_type.collect.first
               end

  real_method = case type
                when :single, :single2array
                  Entity::Property.single_method(name)
                when :array, :array2single
                  Entity::Property.array_method(name)
                when :multiple
                  Entity::Property.multi_method(name)
                when :both
                  name
                else
                  raise "Type of property unknown #{type}"
                end

  properties.push name


  entity_class = self
  if type == :multiple
    self.define_method(real_method) do |*args,**kwargs|
      if entity_class.persisted_methods && entity_class.persisted_methods[name]
        type, options = entity_class.persisted_methods[name]
      else
        type, options = nil, {persist: false}
      end

      missing = []
      responses = {}
      self.each do |item|
        begin
          responses[item] = Entity::Property.persist(name, item, type, options) do
            raise MultipleEntityProperty 
          end
        rescue MultipleEntityProperty
          missing << item
        end
      end

      if missing.any?
        self.annotate(missing)

        new_responses = missing.instance_exec(*args, **kwargs, &block)

        missing.each do |item,i|
          responses[item] = Entity::Property.persist(name, item, type, options) do
            Array === new_responses ? new_responses[item.container_index] : new_responses[item]
          end
        end
      end

      responses.values_at(*self)
    end
  else
    self.define_method(real_method) do |*args,**kwargs|
      if entity_class.persisted_methods && entity_class.persisted_methods[name]
        type, options = entity_class.persisted_methods[name]
      else
        type, options = nil, {persist: false}
      end

      Entity::Property.persist(name, self, type, options) do
        self.instance_exec(*args, **kwargs, &block)
      end
    end
  end

  return if type == :both

  self.define_method(name) do |*args,**kwargs|

    method_type = %w(single_method array_method multi_method).select do |method_type|
      self.methods.include?(Entity::Property.send(method_type, name))
    end.first

    real_method = Entity::Property.send(method_type, name)

    if Array === self
      case method_type
      when 'single_method'
        self.collect{|item| item.send(real_method, *args, **kwargs) }
      when 'array_method', 'multi_method'
        self.send(real_method, *args, **kwargs)
      end
    else
      case method_type
      when 'single_method'
        self.send(real_method, *args, **kwargs)
      when 'array_method', 'multi_method'
        if AnnotatedArray.is_contained?(self)
          cache_code = Misc.digest({:name => name, :args => args})
          res = (self.container._ary_property_cache[cache_code] ||= self.container.send(real_method, *args, **kwargs))
          Array === res ? res[self.container_index] : res[self]
        else
          res = self.make_array.send(real_method)
          Array === res ? res[0] : res[self]
        end
      end
    end
  end
end

#unpersist(name) ⇒ Object



160
161
162
163
# File 'lib/scout/entity/property.rb', line 160

def unpersist(name)
  @persisted_methods ||= {}
  @persisted_methods.delete(name)
end