Module: Locomotive::Presentable::ClassMethods

Defined in:
lib/locomotive/presentable.rb

Instance Method Summary collapse

Instance Method Details

#alias_of(alias_name) ⇒ String

Get the name of the property for which the property passed in parameter is an alias.



201
202
203
204
205
206
# File 'lib/locomotive/presentable.rb', line 201

def alias_of(alias_name)
  self.setters.find do |name|
    list = [*(self.property_options[name] || {})[:alias]] || []
    list.include?(alias_name.to_sym)
  end
end

#collection(name, options = {}) ⇒ Object

Add a collection to the current instance. It creates getter/setter mapped to the collection of the source object.



156
157
158
# File 'lib/locomotive/presentable.rb', line 156

def collection(name, options = {})
  property(name, options.merge(:collection => true))
end

#define_getter(name, collection = false) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/locomotive/presentable.rb', line 160

def define_getter(name, collection = false)
  (@getters ||= []) << name.to_s

  class_eval "    def \#{name}\n      if \#{collection.to_s}\n        list = self.source.send(:\#{name})\n        list ? list.map(&:as_json) : []\n      else\n        self.source.send(:\#{name})\n      end\n    end\n  EOV\nend\n"

#define_setter(name, aliases = []) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/locomotive/presentable.rb', line 175

def define_setter(name, aliases = [])
  (@setters ||= []) << name.to_s
  @setters += aliases.map(&:to_s)

  class_eval "    def \#{name}=(value)\n      self.source.send(:\#{name}=, value)\n    end\n  EOV\n\n  aliases.each do |_name|\n    class_eval <<-EOV\n      def \#{_name}=(value)\n        self.\#{name} = value\n      end\n    EOV\n  end\nend\n"

#inherited(subclass) ⇒ Object

Override inherited in order to copy the parent list of getters, setters and property options.



106
107
108
109
110
111
112
# File 'lib/locomotive/presentable.rb', line 106

def inherited(subclass)
  subclass.getters = getters.clone if getters
  subclass.setters = setters.clone if setters
  subclass.property_options = property_options.clone if property_options

  super
end

#properties(*names) ⇒ Object

Add multiple properties all in once. If th last property name is a hash, then it will be used as the options for all the other properties.



120
121
122
123
124
125
126
# File 'lib/locomotive/presentable.rb', line 120

def properties(*names)
  options = names.last.is_a?(Hash) ? names.pop : {}

  names.each do |name|
    property(name, options)
  end
end

#property(name, options = {}) ⇒ Object

Add a property to the current instance. It creates getter/setter methods related to that property. By default, the getter and setter are bound to the source object.



135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/locomotive/presentable.rb', line 135

def property(name, options = {})
  aliases     = [*options[:alias]]
  collection  = options[:collection] == true

  (@property_options ||= {})[name.to_s] = options

  unless options[:only_setter] == true
    define_getter(name, collection)
  end

  unless options[:only_getter] == true
    define_setter(name, aliases)
  end
end