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.

Parameters:

  • alias_name (String)

    Name of the alias

Returns:

  • (String)

    The original property


205
206
207
208
209
210
# File 'lib/locomotive/presentable.rb', line 205

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.

Parameters:

  • name (String)

    The name of the collection

  • options (Hash) (defaults to: {})

    The options related to the collection (:alias)


160
161
162
# File 'lib/locomotive/presentable.rb', line 160

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

#define_getter(name, collection = false) ⇒ Object


164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/locomotive/presentable.rb', line 164

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

  class_eval <<-EOV
    def #{name}
      if #{collection.to_s}
        list = self.__source.send(:#{name})
        list ? list.map(&:as_json) : []
      else
        self.__source.send(:#{name})
      end
    end
  EOV
end

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


179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/locomotive/presentable.rb', line 179

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

  class_eval <<-EOV
    def #{name}=(value)
      self.__source.send(:#{name}=, value)
    end
  EOV

  aliases.each do |_name|
    class_eval <<-EOV
      def #{_name}=(value)
        self.#{name} = value
      end
    EOV
  end
end

#inherited(subclass) ⇒ Object

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

Parameters:

  • subclass (Class)

    The subclass inheriting from the current class


110
111
112
113
114
115
116
# File 'lib/locomotive/presentable.rb', line 110

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.

Parameters:

  • args (Array)

    List of property names


124
125
126
127
128
129
130
# File 'lib/locomotive/presentable.rb', line 124

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.

Parameters:

  • name (String)

    The name of the property

  • options (Hash) (defaults to: {})

    The options related to the property.


139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/locomotive/presentable.rb', line 139

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