Class: Chop::Create

Inherits:
Struct
  • Object
show all
Defined in:
lib/chop/create.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&other_block) ⇒ Create

Returns a new instance of Create.



33
34
35
36
37
38
39
40
# File 'lib/chop/create.rb', line 33

def initialize(*, &other_block)
  super
  self.transformations = []
  self.deferred_attributes = HashWithIndifferentAccess.new
  self.after_hooks = []
  instance_eval &block if block.respond_to?(:call)
  instance_eval &other_block if block_given?
end

Instance Attribute Details

#after_hooksObject

Returns the value of attribute after_hooks.



31
32
33
# File 'lib/chop/create.rb', line 31

def after_hooks
  @after_hooks
end

#blockObject

Returns the value of attribute block

Returns:

  • (Object)

    the current value of block



7
8
9
# File 'lib/chop/create.rb', line 7

def block
  @block
end

#deferred_attributesObject

Returns the value of attribute deferred_attributes.



31
32
33
# File 'lib/chop/create.rb', line 31

def deferred_attributes
  @deferred_attributes
end

#klassObject

Returns the value of attribute klass

Returns:

  • (Object)

    the current value of klass



7
8
9
# File 'lib/chop/create.rb', line 7

def klass
  @klass
end

#tableObject

Returns the value of attribute table

Returns:

  • (Object)

    the current value of table



7
8
9
# File 'lib/chop/create.rb', line 7

def table
  @table
end

#transformationsObject

Returns the value of attribute transformations.



31
32
33
# File 'lib/chop/create.rb', line 31

def transformations
  @transformations
end

Class Method Details

.create!(klass, table, &block) ⇒ Object



8
9
10
# File 'lib/chop/create.rb', line 8

def self.create! klass, table, &block
  new(klass, table, block).create!
end

.register_creation_strategy(key, &block) ⇒ Object



15
16
17
# File 'lib/chop/create.rb', line 15

def self.register_creation_strategy key, &block
  creation_strategies[key] = block
end

Instance Method Details

#after(*keys, &block) ⇒ Object



177
178
179
180
# File 'lib/chop/create.rb', line 177

def after *keys, &block
  defer *keys
  after_hooks << block
end

#copy(mappings) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/chop/create.rb', line 77

def copy mappings
  transformation do |attributes|
    mappings.each do |from, to|
      attributes[to] = attributes[from]
    end
    attributes
  end
end

#create(&block) ⇒ Object



60
61
62
# File 'lib/chop/create.rb', line 60

def create &block
  self.creation_strategies = Proc.new { block }
end

#create!(cucumber_table = table) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chop/create.rb', line 42

def create! cucumber_table = table
  cucumber_table.hashes.map do |attributes|
    attributes = HashWithIndifferentAccess.new(attributes)
    attributes = transformations.reduce(attributes) do |attrs, transformation|
      transformation.call(attrs)
    end

    strategy, factory = klass.is_a?(Hash) ? klass.to_a.first : [nil, klass]
    args = [factory, attributes]
    record = creation_strategies[strategy].call(*args.compact)

    after_hooks.each do |after_hook|
      after_hook.call(record, attributes.merge(deferred_attributes))
    end
    record
  end
end

#default(key, default_value = nil) ⇒ Object



105
106
107
# File 'lib/chop/create.rb', line 105

def default key, default_value = nil
  field(key, default: nil) { |value| value || default_value || yield }
end

#defer(*keys) ⇒ Object



182
183
184
185
186
187
188
189
# File 'lib/chop/create.rb', line 182

def defer *keys
  transformation do |attributes|
    keys.each do |key|
      self.deferred_attributes[key] = attributes.delete(key)
    end
    attributes
  end
end

#delete(*keys) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/chop/create.rb', line 68

def delete *keys
  transformation do |attributes|
    keys.reduce(attributes) do |attrs, key|
      attributes.delete(key)
      attributes
    end
  end
end

#field(attribute, default: "") ⇒ Object



95
96
97
98
99
100
101
102
103
# File 'lib/chop/create.rb', line 95

def field attribute, default: ""
  if attribute.is_a?(Hash)
    rename attribute
    attribute = attribute.values.first
  end
  transformation do |attributes|
    attributes.merge attribute => yield(attributes.fetch(attribute, default))
  end
end

#file(*keys) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/chop/create.rb', line 117

def file *keys
  options = extract_options!(keys)
  path = options.fetch(:path, "features/support/fixtures")
  upload = options.fetch(:upload, false)

  handle_renames! keys

  keys.each do |key|
    field key do |file|
      if file.present?
        file_path = File.join(path, file)
        if upload
          Rack::Test::UploadedFile.new(file_path)
        else
          File.open(file_path)
        end
      end
    end
  end
end

#files(*keys) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/chop/create.rb', line 138

def files *keys
  options = extract_options!(keys)
  path = options.fetch(:path, "features/support/fixtures")
  upload = options.fetch(:upload, false)
  delimiter = options.fetch(:delimiter, " ")

  handle_renames! keys

  keys.each do |key|
    field key do |paths|
      paths.split(delimiter).map do |file|
        file_path = File.join(path, file)
        if upload
          Rack::Test::UploadedFile.new(file_path)
        else
          File.open(file_path)
        end
      end
    end
  end
end

#has_many(key, klass = nil, delimiter: ", ", name_field: :name) ⇒ Object



160
161
162
163
164
165
166
167
# File 'lib/chop/create.rb', line 160

def has_many key, klass=nil, delimiter: ", ", name_field: :name
  klass ||= key.to_s.classify.constantize
  field key do |names|
    names.split(delimiter).map do |name|
      klass.find_by!(name_field => name)
    end
  end
end

#has_one(key, klass = nil, name_field: :name) ⇒ Object Also known as: belongs_to



169
170
171
172
173
174
# File 'lib/chop/create.rb', line 169

def has_one key, klass=nil, name_field: :name
  klass ||= key.to_s.classify.constantize
  field key do |name|
    klass.find_by!(name_field => name) if name.present?
  end
end

#rename(mappings) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/chop/create.rb', line 86

def rename mappings
  transformation do |attributes|
    mappings.reduce(attributes) do |attrs, (old, new)|
      attrs[new] = attrs.delete(old) if attrs.key?(old)
      attrs
    end
  end
end

#transformation(&block) ⇒ Object



64
65
66
# File 'lib/chop/create.rb', line 64

def transformation &block
  transformations << block
end

#underscore_keysObject



109
110
111
112
113
114
115
# File 'lib/chop/create.rb', line 109

def underscore_keys
  transformation do |attributes|
    attributes.reduce(HashWithIndifferentAccess.new) do |hash, (key, value)|
      hash.merge key.parameterize.underscore => value
    end
  end
end